# "when: key1.key2 is defined" not working

**URL:** <https://forum.ansible.com/t/when-key1-key2-is-defined-not-working/27698>\
**Category:** Ansible Project\
**Created:** [June 4, 2018, 8:47am UTC](https://forum.ansible.com/t/when-key1-key2-is-defined-not-working/27698 "2018-06-04T08:47:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_de\_Jong](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@K\_de\_Jong](https://forum.ansible.com/u/K_de_Jong)\
**Post date:** [June 4, 2018, 8:47am UTC](https://forum.ansible.com/t/when-key1-key2-is-defined-not-working/27698/1 "2018-06-04T08:47:46Z")

</div>

I’m trying to create a condition which checks if a dict has a certain subkey, e.g.:

`when: key1.key2 is defined`

The when condition is always true if key1 is defined, but it doesn’t check if key2 is also defined. How can such a condition be realized?

---

<div class="post-metadata">

**Author:** ![K\_de\_Jong](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@K\_de\_Jong](https://forum.ansible.com/u/K_de_Jong)\
**Post date:** [June 4, 2018, 8:56am UTC](https://forum.ansible.com/t/when-key1-key2-is-defined-not-working/27698/2 "2018-06-04T08:56:03Z")

</div>

Already found out how…

`  
when: key1 is defined and key1.key2 is defined

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 4, 2018, 8:57am UTC](https://forum.ansible.com/t/when-key1-key2-is-defined-not-working/27698/3 "2018-06-04T08:57:14Z")

</div>

You should probably show your code, since this works and is used all the time.
