11-19-2023 08:45 PM
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
Solved! Go to Solution.
11-26-2023 10:37 PM
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
11-19-2023 11:31 PM
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")
11-26-2023 12:31 AM
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
}
}
11-26-2023 10:37 PM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide