ansible-core 2.14 and determining if top level variables exist in source

25 views
Skip to first unread message

Chris Nokes

unread,
Feb 1, 2023, 2:42:33 PM2/1/23
to Ansible Project
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:

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

Felix Fontein

unread,
Feb 1, 2023, 3:31:23 PM2/1/23
to ansible...@googlegroups.com
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


flowerysong

unread,
Feb 1, 2023, 4:42:33 PM2/1/23
to Ansible Project
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.

Rowe, Walter P. (Fed)

unread,
Feb 2, 2023, 8:17:22 AM2/2/23
to ansible...@googlegroups.com
How do these differ?

  when: query('varnames', '^foo$')

  when: foo is defined

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6d836e46-e3ae-466e-a463-57b02ac6fb6an%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages