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

**URL:** <https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606>\
**Category:** Ansible Project\
**Created:** [February 1, 2023, 7:42pm UTC](https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606 "2023-02-01T19:42:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Nokes](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Chris\_Nokes](https://forum.ansible.com/u/Chris_Nokes)\
**Post date:** [February 1, 2023, 7:42pm UTC](https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606/1 "2023-02-01T19:42:33Z")

</div>

Hi,  
I just opened [https://github.com/ansible/ansible/issues/79874](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](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

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 1, 2023, 8:30pm UTC](https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606/2 "2023-02-01T20:30:59Z")

</div>

Hi,

> I just opened [https://github.com/ansible/ansible/issues/79874](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](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](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

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [February 1, 2023, 9:42pm UTC](https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606/3 "2023-02-01T21:42:33Z")

</div>

Hi,  
I just opened [https://github.com/ansible/ansible/issues/79874](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](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.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 2, 2023, 1:17pm UTC](https://forum.ansible.com/t/ansible-core-2-14-and-determining-if-top-level-variables-exist-in-source/37606/4 "2023-02-02T13:17:00Z")

</div>

How do these differ?

when: query(‘varnames’, ‘^foo$’)

when: foo is defined

Walter
