Use Event-Driven Ansible to react to network events.
Outcomes
Synchronize an Event-Driven Ansible (EDA) controller project and restart an existing rulebook activation.
Use event data as variables in a playbook.
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-netops
Instructions
This exercise uses a container image that runs both Telegraf and Apache Kafka.
Telegraf queries the status of the Ethernet1 interface on the arista1 managed node and sends this information to Apache Kafka in the network topic.
View some 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/netops Git repository.
The option indicates that automation controller automatically synchronizes the project any time a job launches that uses a playbook from the project.
Go to → and click the link for the job template. This job template uses the playbook from the 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 click the link for the project. This project uses the same Git repository as the automation controller project.
Go to and notice that the rulebook activation displays a status of .
This rulebook activation launches an automation controller job template if it receives an event indicating that the Ethernet1 interface is down for any of the Arista managed nodes.
Connect to the arista1 managed node and then shut down the Ethernet1 interface.
In a terminal window, connect to the arista1 managed node:
[student@workstation ~]$ ssh arista1
...output omitted...
arista1.lab.example.com>Shut down the Ethernet1 interface:
arista1.lab.example.com>enablearista1.lab.example.com#
conf tarista1.lab.example.com(config)#
int eth 1arista1.lab.example.com(config-if-Et1)#
shutarista1.lab.example.com(config-if-Et1)#
show running-config...output omitted...
interface Ethernet1shutdown![]()
!...output omitted...
Enable privilege escalation on the managed node. | |
Switch to configuration mode. | |
Select the | |
Shut down (or disable) the selected interface.
You can also run the | |
Display the current state of the managed node. | |
The |
If necessary, press q to exit the command.
Verify that EDA controller acted upon the shutdown event and that the automation controller job enabled the interface.
On the EDA controller web browser tab, go to .
You might have to wait a minute, but you should see the rule in the rule audit list. Click the link from the list. If you see more than one link, then click the most recent link at the top of the list.
Click the tab for the rule and then click the link.
Examine the event log and then click to close the window. The following table lists some relevant event information:
| Event key | Value |
|---|---|
body.tags.name
|
Ethernet1
|
body.tags.source
|
arista1.lab.example.com
|
body.fields.admin_status
|
DOWN
|
Click the tab for the rule, right-click the link for the action, and then select .
Click the tab. The page displays a status of and the output of the playbook.
...output omitted... PLAY [Configure Arista managed nodes] ******************************************TASK [Enable interface]********************************************************changed: [arista1.lab.example.com]PLAY RECAP ********************************************************************* arista1.lab.example.com : ok=1 changed=1 unreachable=0 failed=0 ...
In the terminal window connected to the arista1 managed node, display the state of the Ethernet1 interface:
arista1.lab.example.com(config-if-Et1)#show running-config...output omitted...interface Ethernet1!...output omitted...
The interface no longer displays the shutdown status, which indicates that the interface is up.
If necessary, then press q to exit the command.
Ensure that the Manage Arista network interfaces job template only runs on the affected host rather than on the entire host group targeted by the playbook.
Additionally, use files from a Git repository to configure the Ethernet1 interface before bringing the interface up.
In a new terminal window, change to the /home/student/git-repos/netops directory.
This directory is a clone of the git@git.lab.example.com:student/netops Git repository.
This repository provides a playbook for the NetOps automation controller project and a rulebook for the NetOps EDA controller project:
[student@workstation ~]$ cd ~/git-repos/netopsUse a text editor, such as VS Code, to update the rulebooks/port_status.yml rulebook.
The existing ruleset uses the ansible.eda.kafka event source plug-in to collect network events from the utility.lab.example.com host (or rather a container running on that host).
The single rule in the ruleset is triggered if the admin_status key in the event has a value of DOWN.
Add the highlighted line so that the rule sends event information to the job template:
---
- name: Arista events
hosts: all
sources:
- name: Collect events from Apache Kafka
ansible.eda.kafka:
host: utility.lab.example.com
port: 9092
topic: network
rules:
- name: Port is down
condition: event.body.fields.admin_status == "DOWN"
action:
run_job_template:
name: Manage Arista network interfaces
organization: Default
post_events: trueUpdate the hosts key in the playbooks/configure_interface.yml playbook so that the play only targets the host defined by the event.
You can also use the default filter to define the existing eos inventory group name.
By doing this, you can run the playbook even if the ansible_eda['event']['body']['tags']['source'] variable has not been defined.
The ~/example-netops/configure_interface.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/netops/playbooks directory.
---
- name: Configure Arista managed nodes
hosts: "{{ ansible_eda['event']['body']['tags']['source'] | default('eos') }}"
become: true
gather_facts: false
tasks:
- name: Enable interface
arista.eos.eos_interfaces:
config:
- name: Ethernet1
enabled: trueAlthough this step does not change the result of the exercise (the eos inventory host group contains a single host), restricting the hosts targeted by the play might be useful for large inventory host groups.
Add three tasks to the top of the list of tasks in the configure_interface.yml playbook.
---
- name: Configure Arista interface
hosts: "{{ ansible_eda['event']['body']['tags']['source'] | default('eos') }}"
become: true
gather_facts: false
tasks:
- name: Download network configuration from Git
ansible.builtin.git:
repo: https://git.lab.example.com/student/arista-configuration.git
dest: /tmp/srv
clone: true
- name: Include neworking configuration variables
ansible.builtin.include_vars: /tmp/srv/switch_desired_conf.yml
- name: Merge layer 2 interface configuration
arista.eos.eos_l2_interfaces:
config: "{{ l2_interface_config }}"
state: merged
- name: Enable interface
arista.eos.eos_interfaces:
config:
- name: Ethernet1
enabled: trueThis task downloads configuration information from a Git repository and then uses this information as a source of truth. | |
This task loads variables from the | |
This task merges the existing node configuration with the information defined by the |
The ~/example-netops/configure_interface.yml file contains the correct updates.
You can use that file for comparison or copy it to the ~/git-repos/netops/playbooks directory.
Add and commit the local changes, and then push the changes to the remote Git repository:
[student@workstation netops]$git commit -a -m 'Improve rulebook activation'[main 97e410e] Improve rulebook activation 2 files changed, 16 insertions(+), 1 deletion(-) [student@workstation netops]$git push...output omitted...
Update automation controller and EDA controller resources to use the Git repository changes.
On any 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 .
![]() |
This change and the post_events: true line added to the rulebooks/port_status.yml rulebook work together.
The rulebook sends the event, and selecting this checkbox dynamically imports the event as the ansible_eda variable that the job template can use.
In this exercise, the playbook uses the ansible_eda['event']['body']['tags']['source'] variable.
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 .
After the status changes to , click to close the window.
Shut down the Ethernet1 interface on the arista1 managed node and then verify that EDA controller triggers an action that configures the interface and brings the interface up.
From the terminal window connected to the arista1 managed node, shut down the Ethernet1 interface:
arista1.lab.example.com(config-if-Et1)#shutOn 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 this exercise, Telegraf performs a check every 30 seconds. If the automation controller job launched by the rulebook activation does not complete before the next check, then you see an additional rule in the rule audit list.
In the terminal window connected to the arista1 managed node, display the state of the Ethernet1 interface:
arista1.lab.example.com(config-if-Et1)#show running-config...output omitted...interface Ethernet1switchport access vlan 30!...output omitted...
In addition to enabling the interface, the automation controller job configured a VLAN for the interface. If necessary, then press q to exit the command.
Disconnect from the arista1 managed node:
arista1.lab.example.com(config-if-Et1)#logout
Connection to arista1 closed.
[student@workstation ~]$