How to do it...

  1. Install the Python libraries needed for pyATS:
$ sudo pip3 install pyats genie
  1. Create the roles directory and then create the requirements.yml file with the following data:

$ cat roles/requirements.yml
- src: https://github.com/CiscoDevNet/Ansible-pyats
scm: git
name: Ansible-pyats
  1. Install the Ansible-pyats role as shown in the following code:
 $ Ansible-galaxy install -r requirements.yml
  1. Create a new playbook called pb_validate_pyats.yml and populate it with the following task to collect the ospf neighbor from the wan devices.
---
- name: Network Validation with pyATS
hosts: wan
roles:
- Ansible-pyats
vars:
Ansible_connection: local
tasks:
- pyats_parse_command:
command: show ip ospf neighbor
register: ospf_output
vars:
Ansible_connection: network_cli
  1. Update the playbook with the following tasks to extract the data for OSPF peer information:
      - name: "FACT >> Pyats OSPF Info"
set_fact:
pyats_ospf_data: "{{ ospf_output.structured.interfaces }}"

- name: " FACT >> Set OSPF peers"
set_fact:
OSPF_PEERS: "{{ wan_l3_links[inventory_hostname] | selectattr('ospf','equalto',true) | list }}"
  1. Update the playbook with the following tasks to validate OSPF peers and the OSPF peer state:
      - name: Validate Number of OSPF Peers
assert:
that:
- pyats_ospf_data | length == OSPF_PEERS | length
loop: "{{ OSPF_PEERS }}"

- name: Validate All Peers are in Full State
assert:
that:
- pyats_ospf_data[item.name] | json_query('neighbors.*.state') | first == 'FULL/ -'
loop: "{{ OSPF_PEERS }}"