Network module conditional
Let's take a look at another network device conditional example by using the comparison keyword we saw at the beginning of this chapter. We can take advantage of the fact that both IOSv and Arista EOS provide the outputs in JSON format for the show commands. For example, we can check the status of the interface:
arista1#sh interfaces ethernet 1/3 | json
{
"interfaces": {
"Ethernet1/3": {
"interfaceStatistics": {
<skip>
"outPktsRate": 0.0
},
"name": "Ethernet1/3",
"interfaceStatus": "disabled",
"autoNegotiate": "off",
<skip>
}
arista1#
If we have an operation that we want to perform and it depends on Ethernet1/3 being disabled in order to have no user impact, such as to ensure no users are actively connected to Ethernet1/3, we can use the following tasks in the chapter8_3.yml playbook. It uses the eos_command module to gather the interface state output, and checks the interface status using the waitfor and eq keywords before proceeding to the next task:
<skip>
tasks:
- name: "sh int ethernet 1/3 | json"
eos_command:
commands:
- "show interface ethernet 1/3 | json"
provider: "{{ cli }}"
waitfor:
- "result[0].interfaces.Ethernet1/3.interfaceStatus eq
disabled"
register: output
- name: show output
debug:
msg: "Interface Disabled, Safe to Proceed"
Upon the condition being met, the second task will be executed:
TASK [sh int ethernet 1/3 | json]
**********************************************
ok: [arista1]
TASK [show output]
*************************************************************
ok: [arista1] => {
"msg": "Interface Disabled, Safe to Proceed"
}
If the interface is active, an error will be given as follows following the first task:
TASK [sh int ethernet 1/3 | json]
**********************************************
fatal: [arista1]: FAILED! => {"changed": false, "commands": ["show
interface ethernet 1/3 | json | json"], "failed": true, "msg":
"matched error in response: show interface ethernet 1/3 | json |
jsonrn% Invalid input (privileged mode required)rn********1>"}
to retry, use: --limit
@/home/echou/Master_Python_Networking/chapter8/chapter8_3.retry
PLAY RECAP
******************************************************************
arista1 : ok=0 changed=0 unreachable=0 failed=1
Check out the other conditions such as contains, greater than, and less than, as they fit into your situation.