cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
658
Views
2
Helpful
6
Replies

Conditional output in Ansible

Netmart
Level 1
Level 1

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'

6 Replies 6

@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.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

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.

 

This condition should work for your purposes:

when: output.stdout is search("router eigrp")

 

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev

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\")

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")

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev

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.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io