- Network Automation Cookbook
- Karim Okasha
- 280字
- 2025-04-04 13:10:14
How it works...
One of the advantages of using the NETCONF API to interact with Juniper devices is that we can get a structured output for all the operational commands that we execute on the Juniper devices. The output that the device returns to us over the NETCONF session is in XML, and Ansible uses a Python library called jxmlease to decode this XML and transform it to JSON for better representation. That is why our first task was to install the jxmlease Python package.
We use the junos_command module to send operational commands to a Juniper device, and we specify that we need XML as the output format that gets returned from the node. This XML data structure is transformed to JSON using the jxmlease package by Ansible. We save this data using the register keyword to a new variable called ospf_output. Here is a sample of the JSON data that is returned from this command:
"msg": [
{
"rpc-reply": {
"ospf-neighbor-information": {
"ospf-neighbor": [
{
"activity-timer": "34",
"interface-name": "ge-0/0/0.0",
"neighbor-address": "10.1.1.2",
"neighbor-id": "10.100.1.254",
"neighbor-priority": "128",
"ospf-neighbor-state": "Full"
},
{
"activity-timer": "37",
"interface-name": "ge-0/0/1.0",
"neighbor-address": "10.1.1.8",
"neighbor-id": "10.100.1.253",
"neighbor-priority": "128",
"ospf-neighbor-state": "Full"
}
]
}
}
}
]
All this data structure is contained in the ospf_output.output[0] variable, and we use the set_fact module to capture the ospf-neigbour data. After that, we use the assert module to loop through all the OSPF peers in this data structure and validate that the OSPF neighbor state is equal to Full. If all the OSPF peers are in a Full state, the task will succeed. However, if the OSPF state is in any other state, the task will fail.