Use Event-Driven Ansible to notify administrators monitoring a chat room when a network event triggers a rulebook.
Outcomes
Identify event keys when running an activation rulebook.
Create and configure an incoming webhook in Mattermost.
Modify a rulebook to use the post_event action to set variables for event keys.
Create a rule in the rulebook that triggers a job template in automation controller with extra variables.
Modify a playbook to use the extra variables from the rulebook.
As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available:
[student@workstation ~]$ lab start example-notifications
Instructions
Examine the automation controller and Event-Driven Ansible (EDA) controller resources created by the lab command.
Navigate to https://controller.lab.example.com and log in as admin using redhat as the password.
If necessary, click the main menu icon at the upper left of the page to display the main menu.
Go to → and then click the link for the project.
This project uses the git@git.lab.example.com:student/notifications Git repository.
Notice that the project has the option enabled.
Go to → and click the link for the job template.
This job template uses the mattermost_message.yml playbook from the Notifications project.
In a new browser tab, navigate to https://eda-controller.lab.example.com and log in as admin using redhat as the password.
If necessary, click the main menu icon at the upper left of the page to display the main menu.
Go to and then click the link for the project. This EDA controller project uses the same Git repository as the automation controller project.
Go to and click the link for the rulebook activation.
This rulebook activation uses the new_admins.yml rulebook from the Notifications project.
Currently the rulebook activation is stopped.
Using the GitLab web UI, examine the playbook and rulebook created by the lab command.
In a new browser tab, navigate to https://git.lab.example.com and log in as student using Stud3nt123 as the password.
Click the project.
This project contains a rulebooks directory with the new_admins.yml rulebook:
--- - name: Identifying new admin users hosts: eossources: - name: Collect events from kafka ansible.eda.kafka:
host: utility.lab.example.com port: 9092 topic: users
rules: - name: New admin user
condition: > event.body.fields.role is search("SYSTEM_ROLE_ADMIN") and event.body.tags.username != "admin" and event.body.tags.username != "student" action: debug: msg: "The {{ event.body.tags.username }} admin user was created"
The rulebook has a unique ruleset that runs against the | |
The ruleset uses the | |
The | |
The ruleset has a unique rule that displays a message.
The message includes the username of a new user if the user is created with the administration role, and the username is not |
The project contains a playbooks directory with the mattermost_message.yml playbook:
--- - name: Send a message to the chat group hosts: eosgather_facts: false tasks: - name: Send the message ansible.builtin.uri:
url: <put your url here>
method: POST headers: Content-Type: application/json body_format: json body: text: | # Alert #### A network admin user was created delegate_to: workstation.lab.example.com
Test the rulebook activation in EDA controller. Identify the event keys for the created admin user and the managed node where that user was created.
On the EDA controller web browser tab, go to and enable the rulebook activation. Notice that the rulebook activation status changes from to and then to .
In a terminal window, connect to the arista1 managed node.
Enable privilege escalation and switch to the configuration mode for the terminal:
[student@workstation ~]$ssh arista1...output omitted... arista1.lab.example.com>enablearista1.lab.example.com#configure terminalarista1.lab.example.com(config)#
Create a regular user named frank and an admin user named steve, both with S3cr3t as the password:
arista1.lab.example.com(config)#username frank secret S3cr3tarista1.lab.example.com(config)#username steve role network-admin secret S3cr3t
On the EDA controller web browser tab, wait for the number in the column to change from to for the rulebook activation. The increases when it detects a new admin user.
Click the rulebook activation and then click the tab. To see the details for the rulebook activation, click the name that has a status of , and scroll down to the end of the output.
The debug action prints the following message: The steve admin user was created.
** 2024-03-14 17:11:29.786195 [debug] ******************************************
The steve admin user was created
********************************************************************************
********************************************************************************Go to and then click the rule. If you see more than one rule in the rule audit list, then click the most recent one, at the top of the list.
Click the tab, and then click the link to examine the event log.
The following event keys are related to the creation of the new admin user.
| Event key | Value |
|---|---|
body.tags.source
|
arista1.lab.example.com
|
body.tags.username
|
steve
|
body.fields.role
|
openconfig-aaa-types:SYSTEM_ROLE_ADMIN
|
Click to close the window.
In the terminal window connected to the arista1 managed node, remove the frank and steve users:
arista1.lab.example.com(config)#no username frankarista1.lab.example.com(config)#no username steve
This exercise uses Mattermost for the chat room. Configure Mattermost to integrate with incoming webhooks.
In a new browser tab, navigate to http://mattermost.lab.example.com:8065 and click .
Create your account with the information in the table and then click .
| Field | Value |
|---|---|
student@lab.example.com
| |
student
| |
Stud3nt123
|
In the field, enter lab.example.com and then click .
Select as the tool to work with and then click .
To complete the setup process, click .
If you do not see the button, then you might need to increase the display resolution on the workstation machine, decrease the web browser zoom, or both.
Close the window. Click the main menu icon at the upper left of the page and then click . Click .
Click , enter the information from the following table in the appropriate fields, and then click to create an incoming webhook.
| Field | Value |
|---|---|
EDA message
| |
Town Square
|
Copy the URL and click .
Click at the upper left of the page to go to the channel.
Update the mattermost_message.yml playbook with the URL you that you previously copied.
Verify that the job template sends the message to the chat room.
In a new terminal window, change to the ~/git-repos/notifications directory.
This directory is a clone of the git@git.lab.example.com:student/notifications Git repository.
[student@workstation ~]$ cd ~/git-repos/notificationsUse a text editor, such as VS Code, to update the playbooks/mattermost_message.yml playbook.
Enter the value for the url parameter that you copied in the previous step:
...output omitted...
- name: Send the message
ansible.builtin.uri:
url: http://mattermost.lab.example.com:8065/hooks/ummq5nihktgcxezdwik4ctpi
method: POST
...output omitted...Your URL is different from the URL displayed in this example.
Add and commit the local changes, and then push the changes to the remote Git repository:
[student@workstation notifications]$git commit -a -m 'Updating URL'[main 627785a] Updating URL 1 file changed, 1 insertion(+), 1 deletion(-) [student@workstation notifications]$git push...output omitted... [student@workstation notifications]
On the automation controller web browser tab, go to → , and then click the icon to launch the job template.
On the Mattermost web browser tab, verify that you receive the message.
Modify the new_admins.yml rulebook and the mattermost_message.yml playbook.
Instead of displaying a message, the rulebook must launch the job template in automation controller.
The rulebook must also add the admin user that was created and the managed node in which the admin user was created as extra variables in the job template.
The playbook must use the extra variables in the message sent to the Mattermost chat room.
The ~/example-notifications/new_admins.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/notifications/rulebooks directory.
Modify the rulebooks/new_admins.yml rulebook in the ~/git-repos/notifications directory.
Replace the debug action with the post_event action.
Use the new_user and managed_node variables to refer to event data for the event.body.tags.username and event.body.tags.source event keys.
...output omitted... rules: - name: New admin user condition: > event.body.fields.role is search("SYSTEM_ROLE_ADMIN") and event.body.tags.username != "admin" and event.body.tags.username != "student" action:post_event:event:new_user: "{{ event.body.tags.username }}"managed_node: "{{ event.body.tags.source }}"
Add a second rule to the rulebook that runs the job template in automation controller with the extra variables given as additional job arguments:
...output omitted... new_user: "{{ event.body.tags.username }}" managed_node: "{{ event.body.tags.source }}"- name: Send a chat messagecondition: event.new_user is definedaction:run_job_template:name: Send a message to mattermostorganization: Defaultpost_events: truejob_args:extra_vars:admin: "{{ event.new_user }}"node: "{{ event.managed_node }}"
The ~/example-notifications/new_admins.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/notifications/rulebooks directory.
Modify the playbooks/mattermost_message.yml playbook.
Use the node variable added as an extra variable in the rulebook to update the hosts key.
In this way, the play only targets the managed node defined by the event.
---
- name: Send a message to the chat group
hosts: "{{ node | default('eos') }}"
gather_facts: false
...output omitted...Although in this exercise the eos group contains a single managed node, in a production environment it would be useful to restrict the managed node targeted by the play.
By providing the default eos value for the targeted hosts, you prevent the job template from failing when it is run directly from automation controller and the node variable is not defined.
Update the message defined in the body argument for the ansible.builtin.uri module.
Specify the managed node for which the alert applies and the username of the admin user.
Use the node and admin variables added as extra variables in the rulebook:
...output omitted... body: text: | # Alert: {{ node }}#### A network admin user was created: {{ admin }}delegate_to: workstation.lab.example.comwhen:- node is defined- admin is defined
Adding the when condition to the Send the message task can prevent the playbook from failing.
Ansible skips the task unless both the node and admin variables are defined.
Add and commit the local changes, and then push the changes to the remote Git repository in the terminal where you changed to the ~/git-repos/notifications directory:
[student@workstation notifications]$git commit -a -m 'Automation completed'[main 20d8ad4] Automation completed 2 files changed, 22 insertions(+), 5 deletions(-) [student@workstation notifications]$git push...output omitted... [student@workstation notifications]
Update automation controller and EDA controller resources to use the Git repository changes.
On the automation controller web browser tab, go to → , and then click the icon for the job template.
Select the checkbox for the field and then click .
On the EDA controller web browser tab, go to and then click the icon for the project. Wait until the project displays a status of .
Go to . Click the vertical ellipsis icon for the rulebook activation and then click .
Select the checkbox and then click .
When the status changes to , click to close the window. Wait until the number of rules for the rulebook activation increases from 1 to 2.
Create an admin user in the arista1 managed node and verify that EDA controller triggers the sending of a message to the Mattermost chat room.
The message includes the username of the admin user and the managed node where that user was created.
From the terminal window connected to the arista1 managed node, create the steve admin user:
arista1.lab.example.com(config)#username steve role network-admin secret S3cr3tDisconnect from the arista1 managed node:
arista1.lab.example.com(config)#logout
Connection to arista1 closed.
[student@workstation ~]$On the Mattermost web browser tab, wait for the message about the creation of the network admin user. Verify that the message indicates the username and the managed node where the admin user was created.