- Network Automation Cookbook
- Karim Okasha
- 381字
- 2025-04-04 13:10:14
How it works...
In this recipe, we are exploring how to use the pyATS framework to perform network validation. pyATS is an open source Python library developed by Cisco as a testing framework for network testing. Genie is another Python library that provides parsing capabilities for transforming CLI-based output to Python data structures that we can consume in our automation scripts. Cisco released an Ansible role that uses the pyATS and Genie libraries. Within this role, there are multiple modules that we can use in order to build more robust Ansible validation playbooks to validate the network state. In order to start working with this role, we need to perform the following steps:
- Install pyats and enie Python packages using python-pip.
- Install the Ansible-pyats role using Ansible-galaxy.
In this recipe, we are using one of the modules within the Ansible-pyats role, which is pyats_parse_command. This module executes an operational command on the remote managed device and returns both the CLI output for this command and the parsed structured output for this command. The following code snippet outlines the structured data returned by this module for ip ospf neigbor on the wan01 device:
"structured": {
"interfaces": {
"GigabitEthernet2": {
"neighbors": {
"10.100.1.1": {
"address": "10.3.1.1",
"dead_time": "00:00:37",
"priority": 0,
"state": "FULL/ -"
}
}
}
}
}
We save the data returned by this module to the ospf_output variable and we use the set_fact module to capture the structured data returned by this module, before saving it to a new variable – pyats_ospf_data. Then, we use the set_fact module to filter the links defined in wan_l3_interfaces to just the ports that are enabled for OSPF.
Using the structured data returned by pyats_parse_command, we can validate this data and compare it with our OSPF peer definition using the assert module so as to validate the correct number of OSPF peers and their states.
To extract the OSPF peer state, we use the json_query filter to filter the returned data and provide just the OSPF state for each neighbor.