Hi,
I just opened https://github.com/ansible/ansible/issues/79874 and Matt explained that 2.14 change is working as designed and closed the issue.
Matt put a link in the issue describing variable lazy eval and how undefined variables now work:
https://docs.ansible.com/ansible-core/2.14/porting_guides/porting_guide_core_2.14.html#playbook
I believe I’m tracking those changes, but I’m still lost on how my use case can be implemented. My specific use case was a task’s when clause determining to execute based on a top level variable being “authored”. If the author made a mistake in the nested elements, then an exception is desired. How can this use case be implemented in 2.14?
Thanks,
Chris
Hi,
I just opened https://github.com/ansible/ansible/issues/79874 and
Matt explained that 2.14 change is working as designed and closed the
issue.
Matt put a link in the issue describing variable lazy eval and how
undefined variables now work:
https://docs.ansible.com/ansible-core/2.14/porting_guides/porting_guide_core_2.14.html#playbook
if someone is interested, the implementation of that change happened in
https://github.com/ansible/ansible/pull/56116
I believe I'm tracking those changes, but I'm still lost on how my
use case can be implemented. My specific use case was a task's when
clause determining to execute based on a top level variable being
"authored". If the author made a mistake in the nested elements,
then an exception is desired. How can this use case be implemented
in 2.14?
I'm not sure it can actually be implemented anymore, at least for
something that works in the form of `nested is my_undefined_test`.
Maybe it is still possible to do something like `"nested" is
my_undefined_test`, where `my_undefined_test` is a custom test plugin
which checks whether a global object of that name seems to exist. But
that depends on how Jinja2 internally works, which I know too little
about.
Best,
Felix
Hi,
I just opened https://github.com/ansible/ansible/issues/79874 and Matt explained that 2.14 change is working as designed and closed the issue.
Matt put a link in the issue describing variable lazy eval and how undefined variables now work:
https://docs.ansible.com/ansible-core/2.14/porting_guides/porting_guide_core_2.14.html#playbook
I believe I’m tracking those changes, but I’m still lost on how my use case can be implemented. My specific use case was a task’s when clause determining to execute based on a top level variable being “authored”. If the author made a mistake in the nested elements, then an exception is desired. How can this use case be implemented in 2.14?
You can use the varnames lookup to determine whether a top-level variable exists:
- name: Run when foo exists
debug:
when: query(‘varnames’, ‘^foo$’)
If you want to do a preflight check that the variable is defined properly, you can do something like this:
- assert:
that: foo is defined
when: query(‘varnames’, ‘^foo$’)
Then you can continue using when: foo is defined
on your actual task(s), since you have asserted that the variable is either fully undefined or fully defined.
How do these differ?
when: query(‘varnames’, ‘^foo$’)
when: foo is defined
Walter