I have a var that is only defined when running vagrant:
is_vagrant: yes
When I run other playbook it's not defined as the var is not included.
When I do in ansible:
when: is_vagrant is defined
It works like a charm when it's defined or not defined.
But when I use something like this:
when: gitclone.changed or is_vagrant is defined
it just skips the task even when is_vagrant var is included. I'm not sure why?
I've given up trusting ansible to do the right thing when it comes to variable expansions. So even when ansible tries to misguide me with messages like "you don't need to use {{}} or """, I still do.
Did you try:
when: "{{ gitclone.changed or is_vagrant is defined }}"
-- edit --
when: "{{ is_vagrant is defined or ( gitclone is defined and gitclone.changed ) }}"
Why do I have to check the defined. .changed is true or false I assume?
If you want the task to 'fail' when the variable is not defined, then you don't have to check if it's defined. But if you don't want the UndefinedError: 'git_output' error then you have check.
changed is inside git_output so if git_output isn't defined git_output.changed isn't defined by extension.
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