name: do something
shell: do task
when: foo is defined
This works perfectly for most situations however we have a number of places where we need to unset the variable to stop the command on certain hosts. Neither of the following work when set in the group vars and result in the task working.
foo:
foo: “”
So my question is how would one normally unset a variable like this or correctly test for it. Jinja docs suggest its just an “if variable” [1], so “when: foo” but that doesn’t work whenever the variable has a value
fatal: [127.0.0.1] => error while evaluating conditional: foo
The closest I’ve gotten is
when: foo is defined and foo is string
but this feels wrong and there is no reference to using an “is string” check in the ansible docs.
Thanks, that will work in the case of foo: “”. Before I go about switching all our templates, is there a way to also make it work in the case of foo: , i.e. without the empty string defined? Ansible detects these as None