cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
268
Views
1
Helpful
3
Replies

Ansible - Using Variables in When Condition

Netmart
Level 1
Level 1

Hello,

I was wondering, if it is possible to use variable in a search string within a WHEN block:

- name: Ansible site-ios-xe-L2-L3-Metrics-Check

  hosts: IDFiosxe

  vars_files:

         - /home/cisco/Ansible/vault_password.yml

  vars:

      ansible_become_pass: "{{ vault_sudo_password }}"

      ansible_python_interpreter: /usr/bin/python3

   

      image:

         - cat4500es8-universalk9.SPA.03.11.10.E.152-7.E10.bin

         - cat4500es8-universalk9.SPA.03.11.9.E.152-7.E10.bin

         - cat4500es8-universalk9.SPA.03.11.8.E.152-7.E10.bin


:

     - name: Task34.4 - result - bootvar-Match

       debug:

       #var: output

          msg: " OK: Boot Variance is updated with latest new image {{image[0]}} @ {{inventory_hostname}} "

       when:

         - output34.stdout is search("BOOT variable = bootflash:{{image[0]}},1;")

         - output34.stdout is search("Standby BOOT variable = bootflash:{{image[0]}}n,1;")

 

Do this Ansible is complaining:

variable = bootflash:{{image[0]}},1;")
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: output34.stdout is search("Standby

 

Therefore, I am wondering whether there is any alternative to achieve the same.

 

Thanks,

Martin

 

1 Accepted Solution

Accepted Solutions

Torbjørn
Spotlight
Spotlight

When trying to solve this it is useful to understand why it doesn't allow the use of Jinja delimiters. To quote the Ansible docs: "The when clause is a raw Jinja2 expression without double curly braces". You should hence write a regular jinja expression, within which it is not natural to use Jinja delimiters. You should be able to use these expressions using the concatenation operator(~):

output34.stdout is search("BOOT variable = bootflash: " ~ image[0])
output34.stdout is search("Standby BOOT variable = bootflash: " ~ image[0])

You can read more about Ansible conditionals and Jinja2 expressions here:

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html

https://jinja.palletsprojects.com/en/3.1.x/templates/#expressions

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

View solution in original post

3 Replies 3

Torbjørn
Spotlight
Spotlight

When trying to solve this it is useful to understand why it doesn't allow the use of Jinja delimiters. To quote the Ansible docs: "The when clause is a raw Jinja2 expression without double curly braces". You should hence write a regular jinja expression, within which it is not natural to use Jinja delimiters. You should be able to use these expressions using the concatenation operator(~):

output34.stdout is search("BOOT variable = bootflash: " ~ image[0])
output34.stdout is search("Standby BOOT variable = bootflash: " ~ image[0])

You can read more about Ansible conditionals and Jinja2 expressions here:

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html

https://jinja.palletsprojects.com/en/3.1.x/templates/#expressions

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

Netmart
Level 1
Level 1

Thank you Torbjoern.

Yes it worked by using the  concatenation operator(~) as suggested by you.

Is there another way to do or would recommend keep using the concatenation operator(~).

 

Thanks..

 

No problem!

I think this is a good way to solve the task at hand. Its simple, robust and unlikely to result in false positives. I would leave it as it is.

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