cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
489
Views
0
Helpful
3
Replies

Ansible WHen statement based an previous Output

Netmart
Level 1
Level 1

Hello,

I wanted to create a condition to execute only an ios command when it is part of previous output of "sh run | sec eigrp".

It works to remove the eigrp process, but it skippes the removal of route-map, though it is part of output.

Please see output below and let me know any suggestions.

Thanks.

Playbook:

---
- name: Ansible ios_command on Cisco IOS XE
hosts: iosxe
vars_files:
- /home/cisco/Ansible/vault_password2.yml
vars:
ansible_become_pass: "{{ vault_sudo_password }}"
ansible_python_interpreter: /usr/bin/python3

tasks:

- name: show ip route and bfd neighbors prior to changes
ios_command:
commands:
- sh run | sec eigrp

become: true
register: output1


- name: "BGP: no EIGRP redist route map"
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- router bgp 65411
- address-family ipv4
when: output1.stdout is search("redistribute eigrp 10 route-map EIGRP-2-BGP")

become: true
register: output2


- name: "no router eigrp 10"
ios_config:
commands:
- no router eigrp 10
when: output1.stdout is search("router eigrp 10")

become: true
register: output3




- name: show ip route and bfd neighbors after changes
ios_command:
commands:
- sh ip route vrf mgmtVrf
- sh ip bgp summary
- sh ip route
- sh bfd neighbors

become: true
register: output4


- name: print result1-show run | sec eigrp
debug:
var: output1

- name: print result2-BGP no EIGRP redist route map
debug:
var: output2

- name: print result3-no router eigrp 10
debug:
var: output3

- name: print result4-show ip route and bfd neighbors after changes
debug:
var: output4

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Reduced output


~/Ansible$ ansible-playbook -i inventories/hosts-lab library/site-eos-eigrp-remove-when.yml -u cisco -k --ask-vault-pass
SSH password:
Vault password:

PLAY [Ansible ios_command on Cisco IOS XE] **********************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [10.0.0.1]

TASK [show ip route and bfd neighbors prior to changes] *************************************************************************************************************************************************************************************
ok: [10.0.0.1]

TASK [BGP: no EIGRP redist route map] *******************************************************************************************************************************************************************************************************
skipping: [10.0.0.1]

TASK [no router eigrp 10] *******************************************************************************************************************************************************************************************************************
[WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the running configuration on device
changed: [10.0.0.1]

TASK [show ip route and bfd neighbors after changes] ****************************************************************************************************************************************************************************************
ok: [10.0.0.1]

TASK [print result1-show run | sec eigrp] ***************************************************************************************************************************************************************************************************
ok: [10.0.0.1] => {
"output1": {
"changed": false,
"failed": false,
"stdout": [
"router eigrp 10\n distribute-list route-map RM-EIGRP-DL-IN in \n shutdown\n redistribute eigrp 10 route-map RM-EIGRP-2-BGP"
],
"stdout_lines": [
[
"router eigrp 10",
" distribute-list route-map RM-EIGRP-DL-IN in ",
" shutdown",
" redistribute eigrp 10 route-map RM-EIGRP-2-BGP"
]
]
}
}

TASK [print result2-BGP no EIGRP redist route map] ******************************************************************************************************************************************************************************************
ok: [10.0.0.1] => {
"output2": {
"changed": false,
"false_condition": "output1.stdout is search(\"redistribute eigrp 10 route-map EIGRP-2-BGP\")",
"skip_reason": "Conditional result was False",
"skipped": true
}
}

TASK [print result3-no router eigrp 10] *****************************************************************************************************************************************************************************************************
ok: [10.0.0.1] => {
"output3": {
"banners": {},
"changed": true,
"commands": [
"no router eigrp 10"
],
"failed": false,
"updates": [
"no router eigrp 10"
],
"warnings": [
"To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the running configuration on device"
]
}
}

:

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
10.0.0.1 : ok=8 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

 

1 Accepted Solution

Accepted Solutions

I was able to solve the whitespace by using regex_search - see example below.

 

- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- router bgp 65411
- address-family ipv4

when: "output2.stdout | regex_search('.*redistribute eigrp 10 route-map RM-EIGRP-2-BGP.*') "

become: true
register: output4

View solution in original post

3 Replies 3

Marcel Zehnder
Spotlight
Spotlight

Hi

To me this looks okay, maybe depending on the versions you use, the regex is not matching because of the whitespace before the route-map config-line - can you try change your task to the following:

- name: "BGP: no EIGRP redist route map"
  ios_config:
    lines:
      - no redistribute eigrp 10 route-map EIGRP-2-BGP
    parents:
      - router bgp 65411
      - address-family ipv4
  when: output1.stdout is search("\\sredistribute eigrp 10 route-map EIGRP-2-BGP")

Hi Marcel,

Thank you, but it did not work. I agree it is the double space in the string which fails the string method to fail.

Is there any alternative avalable?

 

TASK [print result2-BGP no EIGRP redist route map] *********************************************************************
ok: [10.254.199.199] => {
    "output3": {
        "changed": false,
        "false_condition": "output2.stdout is search(\"\\\\sredistribute eigrp 10 route-map EIGRP-2-BGP\")",
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}

I was able to solve the whitespace by using regex_search - see example below.

 

- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- router bgp 65411
- address-family ipv4

when: "output2.stdout | regex_search('.*redistribute eigrp 10 route-map RM-EIGRP-2-BGP.*') "

become: true
register: output4