How it works...

Ansible provides a fact-gathering module to collect the basic system properties for Juniper devices and returns these facts in a consistent and structured data structure. We can use the facts collected by this module in order to validate the basic properties and operational state of our devices, and we can use this data to build simple reports that capture the state of our devices.

In this recipe, we use the junos_facts module to collect the device facts for all our Juniper devices. This module returns the basic facts collected by Ansible for each device in multiple variables, as follows:

"Ansible_net_serialnum": "VM5D112EFB39",
"Ansible_net_system": "junos",
"Ansible_net_version": "17.1R1.8",
"Ansible_network_os": "junos",

We use this data in order to build a fact report for each device using the blockinfile module, and we use this data to validate the operational state of the core interfaces of each device using the assert module.

Once we run our playbook, we can see that a facts report for each device is generated, as follows:

$ tree device_facts/

device_facts/
├── mxp01.txt
├── mxp02.txt
├── mxpe01.txt
└── mxpe02.txt


$ cat device_facts/mxp01.txt

device_name: mxp01
model: junos vmx
os_version: 14.1R4.8
serial_number: VM5701F131C6

In the final task, we use the assert module in order to validate that all the core interfaces on all the Juniper devices are operational. Ansible stores all the interfaces' operational status for the device under Ansible_net_interfaces. We use the data in this data structure to validate that the operational state is up. In the case that all the core interfaces are operational, the task will succeed—otherwise, the task will fail.