Use a webhook to trigger an Ansible Rulebook when an administrator enters specific text in a particular chat room.
Outcomes
Create an outgoing webhook in Mattermost.
Trigger a rulebook activation by sending a message in Mattermost.
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-chat
Instructions
Examine the automation resources in automation controller and Event-Driven Ansible (EDA) controller.
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 template.
Note that this job template uses the backup_switch.yml playbook from the Switch Maintenance project.
This project in automation controller uses the playbooks from the switch_maintenance project in GitLab.
Go to → and then click the template.
Note that this job template uses the update_motd.yml playbook from the Switch Maintenance project.
Although not displayed on the page, the job template already has the checkbox selected for the field so that the job template can dynamically import the event as the ansible_eda variable.
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 rulebook activation.
Note that this rulebook activation uses the switch_maintenance.yml rulebook from the Switch Maintenance project.
This project in EDA controller uses the rulebooks from the switch_maintenance project in GitLab.
Create an outgoing webhook in Mattermost to trigger the Switch maintenance rulebook activation.
In a new browser tab, navigate to http://mattermost.lab.example.com:8065 and log in as student using Stud3nt123 as the password.
If necessary, click .
Click the main menu icon at the upper left of the page to display the main menu, and then click .
Go to → .
In the field, enter eda-controller.lab.example.com and then click .
Click at the upper left of the page.
Click the main menu icon at the upper left of the page to display the main menu, and then click . From the page, click the tile and then click .
Create the outgoing webhook with the following information. Make sure to place each trigger word on its own line.
| Key | Value |
|---|---|
EDA controller
| |
application/json
| |
|
| |
http://eda-controller.lab.example.com:5000
|
When finished, click , and then click .
Click at the upper left of the page.
Send a message in Mattermost to trigger the rulebook activation and examine the event data.
On the Mattermost web browser tab, send the following message: backup-network.
On the EDA controller web browser tab, go to and click the rulebook activation. Click the tab and click the name that has a status of . You should see output similar to the following:
'payload': {'channel_id': 'oqtr81eripb6igwgh37wunpoey',
'channel_name': 'town-square',
'file_ids': '',
'post_id': '7h9yycczntfp3px7u89r7xne1e',
'team_domain': 'labexamplecom',
'team_id': 'a35ke44b6bgd587nedo7agf93o',
'text': 'backup-network',
'timestamp': 1710778656711,
'token': 'x65gsatsa7ro3njtbhtg74hyyo',
'trigger_word': 'backup-network',
'user_id': 'wmp3kqkdii8zxk3r985mre9uor',
'user_name': 'student'}}The following are event keys worth examining:
payload.text
The entire message that was sent in Mattermost
payload.trigger_word
The trigger word that triggered the rulebook activation
payload.channel_name
The channel the message was sent in
payload.user_name
The username of the user that sent the message
Create rules in the switch_maintenance.yml rulebook to trigger the job template when the backup-network message is sent in Mattermost, and to trigger the job template when the update-motd message is sent in Mattermost.
The ~/example-chat/switch_maintenance.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/switch_maintenance/rulebooks directory.
In a new terminal window, change to the /home/student/git-repos/switch_maintenance directory.
This directory is a clone of the git@git.lab.example.com:student/switch_maintenance Git repository.
The Switch Maintenance projects in automation controller and EDA controller use playbooks and a rulebook in this repository.
[student@workstation ~]$ cd ~/git-repos/switch_maintenanceUse a text editor, such as VS Code, to add the Back up network devices rule to the rulebooks/switch_maintenance.yml rulebook.
You can see that the existing ruleset uses the ansible.eda.webhook event source plug-in to listen for incoming events from the outgoing webhook in Mattermost on TCP port 5000.
The single rule in the ruleset is triggered if the event.payload key exists in the event data.
Add a new rule before the Print event data rule that runs the job template when the backup-network message is sent to the town-square channel in Mattermost:
---
- name: Capture POST events from Mattermost
hosts: all
sources:
- name: Match events posted to port 5000
ansible.eda.webhook:
host: 0.0.0.0
port: 5000
rules:
# When the message "backup-network" is sent in the "town-square" channel,
# run the "Back up Switch" job template
- name: Back up network devices
condition: >
event.payload.channel_name == "town-square"
and event.payload.trigger_word == "backup-network"
and event.payload.user_name == "student"
action:
run_job_template:
name: Back up Switch
organization: Default
- name: Print event data
condition: event.payload is defined
action:
print_event:
pretty: trueEdit the Update MOTD rule to add the rulebooks/switch_maintenance.yml rulebook.
Add the new rule after the Back up network devices rule and before the Print event data rule:
---
- name: Capture POST events from Mattermost
hosts: all
sources:
- name: Match events posted to port 5000
ansible.eda.webhook:
host: 0.0.0.0
port: 5000
rules:
# When the message "backup-network" is sent in the "town-square" channel,
# run the "Back up Switch" job template
- name: Back up network devices
condition: >
event.payload.channel_name == "town-square"
and event.payload.trigger_word == "backup-network"
and event.payload.user_name == "student"
action:
run_job_template:
name: Back up Switch
organization: Default
# When the message "update-motd" is sent in the "town-square" channel,
# run the "Update MOTD" job template
- name: Update MOTD
condition: >
event.payload.channel_name == "town-square"
and event.payload.trigger_word == "update-motd"
and event.payload.user_name == "student"
action:
run_job_template:
name: Update MOTD
organization: Default
- name: Print event data
condition: event.payload is defined
action:
print_event:
pretty: trueThe ~/example-chat/switch_maintenance.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/switch_maintenance/rulebooks directory.
Add and commit the local changes, and then push the changes to the remote Git repository:
[student@workstation switch_maintenance]$git commit -a -m 'Improve rulebook'[main 4443f12] Improve rulebook 1 file changed, 24 insertions(+) [student@workstation switch_maintenance]$git push...output omitted...
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 click .
Select the checkbox and click .
When the status changes to , click to close the window.
Test the Back up network devices rule in the Switch maintenance rulebook activation.
Inspect the backup_switch.yml playbook in the ~/git-repos/switch_maintenance/playbooks directory.
Note that this playbook backs up network devices in the eos group, and then copies the backup files to the ~/example-chat directory on the workstation machine.
This is the playbook that the Back up Switch job template uses in automation controller.
On the Mattermost web browser tab, ensure that you are in the channel. Send the following message:
backup-network
On the EDA controller web browser tab, go to .
You might have to wait a minute, but you should see a new rule in the rule audit list. Wait until the rule displays a status of .
In the terminal window, verify that the backup file was copied to the ~/example-chat directory:
[student@workstation switch_maintenance]$ls ~/example-chatarista1.lab.example.com.backup.cfgswitch_maintenance.yml
Test the Update MOTD rule in the Switch maintenance rulebook activation.
Inspect the update_motd.yml playbook in the ~/git-repos/switch_maintenance/playbooks directory.
Note that this playbook updates the login banner of network devices in the eos group using the ansible_eda['event']['payload']['text'] variable.
The playbook uses a replace filter to remove the update-motd string from the event.payload.text key.
After the playbook updates the login banner on the network device, the playbook sends a confirmation message back to the Mattermost channel.
On the Mattermost web browser tab, ensure that you are in the channel. Send the following message:
update-motd This is an arista eos managed node
On the EDA controller web browser tab, go to .
You might have to wait a minute, but you should see a new rule in the rule audit list. Wait until the rule displays a status of .
On the Mattermost web browser tab, the following message appears from the student BOT account:
The MOTD for the 'arista1.lab.example.com' managed node has been set to: 'This is an arista eos managed node'.
In a new terminal window, connect to the arista1 managed node to verify that the new login banner has been configured:
[student@workstation ~]$ssh arista1...output omitted...This is an arista eos managed node...output omitted...
Disconnect from the arista1 managed node:
arista1.lab.example.com>logout
Connection to arista1 closed.
[student@workstation ~]$