# Testing if nested variable is defined

**URL:** <https://forum.ansible.com/t/testing-if-nested-variable-is-defined/24988>\
**Category:** Ansible Project\
**Created:** [July 14, 2017, 1:18pm UTC](https://forum.ansible.com/t/testing-if-nested-variable-is-defined/24988 "2017-07-14T13:18:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jozef\_Hartinger](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jozef\_Hartinger](https://forum.ansible.com/u/Jozef_Hartinger)\
**Post date:** [July 14, 2017, 1:18pm UTC](https://forum.ansible.com/t/testing-if-nested-variable-is-defined/24988/1 "2017-07-14T13:18:50Z")

</div>

Assume the following variable structure:

`

vars:  
foo:  
bar:  
baz: 5

`

If I simply to:

`when: foo.bar.baz is defined`

this only checks the baz key. I still get a failure if e.g. foo.bar is not defined. (The error was: error while evaluating conditional (foo.bar.baz is defined): ‘dict object’ has no attribute ‘bar’)

The proper way seems to be:

`

- when:
- foo is defined
- foo.bar is defined
- foo.bar.baz is defined

`

which is kind of verbose. Is there a better / more concise way of checking the entire foo.bar.baz chain?
