# Traverse nested dictionary with keeping the keys

**URL:** <https://forum.ansible.com/t/traverse-nested-dictionary-with-keeping-the-keys/14742>\
**Category:** Ansible Project\
**Created:** [March 10, 2014, 1:13pm UTC](https://forum.ansible.com/t/traverse-nested-dictionary-with-keeping-the-keys/14742 "2014-03-10T13:13:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Verspuy](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@Robert\_Verspuy](https://forum.ansible.com/u/Robert_Verspuy)\
**Post date:** [March 10, 2014, 1:13pm UTC](https://forum.ansible.com/t/traverse-nested-dictionary-with-keeping-the-keys/14742/1 "2014-03-10T13:13:03Z")

</div>

All,

I’ve tryied to use with\_subelements, but this function does not keep the key of the main loop.

For example:

In host\_vars/testserver.yml:

---

<div class="post-metadata">

**Author:** ![Robert\_Verspuy](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@Robert\_Verspuy](https://forum.ansible.com/u/Robert_Verspuy)\
**Post date:** [March 10, 2014, 2:36pm UTC](https://forum.ansible.com/t/traverse-nested-dictionary-with-keeping-the-keys/14742/2 "2014-03-10T14:36:22Z")

</div>

I made a pull request for this by the way (with a new module): [https://github.com/ansible/ansible/pull/6370](https://github.com/ansible/ansible/pull/6370)

Currently limited to only 2 layers, but extra layers can be added fairly easily in the future.

Regards,  
Robert

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 10, 2014, 3:20pm UTC](https://forum.ansible.com/t/traverse-nested-dictionary-with-keeping-the-keys/14742/3 "2014-03-10T15:20:23Z")

</div>

Understand your desire for this, though see comments on the ticket.
