# Optional second key with\_subelements

**URL:** <https://forum.ansible.com/t/optional-second-key-with-subelements/14336>\
**Category:** Ansible Project\
**Created:** [January 14, 2014, 3:54am UTC](https://forum.ansible.com/t/optional-second-key-with-subelements/14336 "2014-01-14T03:54:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Meredith](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@John\_Meredith](https://forum.ansible.com/u/John_Meredith)\
**Post date:** [January 14, 2014, 3:54am UTC](https://forum.ansible.com/t/optional-second-key-with-subelements/14336/1 "2014-01-14T03:54:55Z")

</div>

Hi,

I have a vars file with the following:

> applications:
> 
> - name: app1
> 
> sublist:
> 
> - item11
> 
> - item12
> 
> - name: app2
> 
> sublist:
> 
> - item21
> 
> - item22
> 
> - name: app3

…and a playbook task of:

> - debug: msg=“{{ item.0.name }}”

> with\_subelements:
> 
> - applications
> 
> - sublist

… which bombs out on the last application.

I was wondering if there was a more “ansible” (or python) way of having the second key optional for the with\_subelements loop.

I’ve unsuccessfully tried adding a 'when: “‘ssl’ in item.0” condition.

TIA,

John

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 14, 2014, 1:24pm UTC](https://forum.ansible.com/t/optional-second-key-with-subelements/14336/2 "2014-01-14T13:24:30Z")

</div>

You could write your own lookup plugin.

I think it would be very important to have the existing continue to require the key, to avoid confusing errors in the case of typo’ing the key and not getting any results.
