I see this thread comes up in google: https://github.com/ansible/ansible/issues/2699
I’m finding that I often want to test if a variable is set before I run a task. Specifically I’m running into situations where I load variables conditionally like:
var_files:
- path/to/{{ environment}}/vars.yml
Where downstream tasks operate on lists defined in vars.yml that may or may not exist depending on the environment.
-John
Hi John,
I use the following idiom all the time:
- name: maybe do this is foo is set
shell: echo ‘Hazzah!’
when: foo is defined
K
That’s perfect, thanks! -John
We should put this in the docs under ‘when’ in 1.2.
Nice.
BTW, it will work here but you should be careful naming variables named ‘environment’, as environment is set to the values of ‘environment:’ later on.
It’s documented as reserved.
Done Pull request submitted.
K