11-06-2023 11:29 PM
Hello,
I wanted to print only one message when show run | eigrp output does contain "eigrp".
However, print result is skipped.
Please advise.
Thanks
Like:
---
- name: EIGRP Config Check
hosts: iosxe
vars_files:
- /home/cisco/Ansible/vault_password.yml
vars:
ansible_become_pass: "{{ vault_sudo_password }}"
ansible_python_interpreter: /usr/bin/python3
tasks:
- name: show run | sec eigrp
ios_command:
commands:
- show run | sec eigrp
become: true
register: output
- name: print result
debug:
var: output
msg: " EIGRP configured "
when: '"router eigrp" in output.stdout'
11-07-2023 02:35 AM
@Netmart i see you are using the when clause to check if the output of the show run | sec eigrp command contains the string "router eigrp". However, the output of this command will also contain other lines of configuration related to EIGRP, such as the network statements and the redistribute statements. As a result, the when clause will always evaluate to true, and the print result task will always be executed.
Hope this helps.
11-07-2023 08:30 AM
Thank you.
Let me try to rephrase my question.
The goal is to implement an "if condition" by search for a string in a block of text
If the output of "show run | sec eigrp" does contain the string "eigrp", only print the message: "EIGRP config exists". Otherwise print the message "EIGRP does NOT exist".
Is this possible?
Or is way to utilize the Python find method like:
The find() method returns -1 if the value is not found.
Please advise.
11-07-2023 08:48 AM
This condition should work for your purposes:
when: output.stdout is search("router eigrp")
11-12-2023 08:48 PM
Thank you Torbjorn.
This seems to work.
Is it possible to expand this condition, by printing a message if EIGRP config is not found.
Like:
- name: print result
debug:
#var: output
msg: " EIGRP configured "
when: output.stdout is search("router eigrp")
- name: print result
debug:
#var: output
msg: " EIGRP NOT configured "
when: if output.stdout is search(=!\"router eigrp\")
11-12-2023 10:29 PM
No problem! Yes, that can be done. You can use a second task with the following when statement/condition:
when: output.stdout is not search("router eigrp")
11-07-2023 09:47 AM
You could implement an "if condition" to search for a string in a block of text using Ansible. Like you can use the find() filter to search for the string in the output.stdout variable. The find() filter returns the index of the first occurrence of the string, or -1 if the string is not found
Hope this helps.
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