I have multiple tasks depend from the value of variable1. I want to check if the value is in {{variable1}} but i got an error:
- name: do something when the value in variable1 command: <command> when: "'value' in {{variable1}}"
I'm using ansible 2.0.2
To implement conditions in Ansible, we use the when keyword. The keyword takes Boolean expressions based on a value or a variable from previous tasks or facts gathered from the remote hosts. This guide will teach you how to implement conditions in Ansible playbooks using the when keyword.
This module allows setting new variables. Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will be available to subsequent plays during an ansible-playbook run.
As per latest Ansible Version 2.5, to check if a variable is defined and depending upon this if you want to run any task, use undefined keyword. tasks: - shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out. this play requires 'bar'" when: bar is undefined.
If variable1
is a string, and you are searching for a substring in it, this should work:
when: '"value" in variable1'
if variable1
is an array or dict instead, in
will search for the exact string as one of its items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With