Network module conditional

Let's take a look at a network device example. We can take advantage of the fact that both IOSv and Arista EOS provide the output in JSON format in 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 preform and it depends on Ethernet1/3 being disabled in order to have no user impact, such as when users are actively connected to Ethernet1/3. We can use the following tasks configuration in chapter5_3.yml, which is provided in the eos_command module to make sure that is the case before we proceed:

    <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 met condition, the subsequent task will be executed:

    TASK [sh int ethernet 1/3 | json]  
**********************************************
ok: [arista1]

TASK [show output]
*************************************************************
ok: [arista1] => {
"msg": "Interface Disabled, Safe to Proceed"
}

Otherwise, an error will be given as follows:

    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/Chapter5/chapter5_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 in your situation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.65.130