03-22-2024 03:46 PM
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
Solved! Go to Solution.
03-24-2024 03:43 AM
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
03-24-2024 03:43 AM
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
04-02-2024 10:19 PM
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..
04-04-2024 05:08 AM - edited 04-04-2024 05:09 AM
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.
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