# nested dictionary access

**URL:** <https://forum.ansible.com/t/nested-dictionary-access/16390>\
**Category:** Ansible Project\
**Created:** [October 1, 2014, 5:24am UTC](https://forum.ansible.com/t/nested-dictionary-access/16390 "2014-10-01T05:24:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Sudakovich](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sergey\_Sudakovich](https://forum.ansible.com/u/Sergey_Sudakovich)\
**Post date:** [October 1, 2014, 5:24am UTC](https://forum.ansible.com/t/nested-dictionary-access/16390/1 "2014-10-01T05:24:29Z")

</div>

I have a dictionary like this:

ppools:  
one:  
devices:

- abc
- cde  
types: [typea, typeb, typec]  
branch: aaa  
two:  
devices:
- fgh
- ijk  
types: [typea, typeb, typec]  
branch: bbb

I was wondering if I can somehow iterate over it so that the elements show up like so:

- one, abc, typea

- one, abc, typeb

- one, abc, typec

- one, cde, typea

- one, cde, typeb

- one, cde, typec

- two, fgh, typea

- two, fgh, typeb  
…

Appreciate any advice.

---

<div class="post-metadata">

**Author:** ![Sergey\_Sudakovich](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sergey\_Sudakovich](https://forum.ansible.com/u/Sergey_Sudakovich)\
**Post date:** [October 1, 2014, 7:28am UTC](https://forum.ansible.com/t/nested-dictionary-access/16390/2 "2014-10-01T07:28:05Z")

</div>

It looks like I will need to flatten out the structure, just want somebody to confirm that there is no other obvious way.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 1, 2014, 7:35am UTC](https://forum.ansible.com/t/nested-dictionary-access/16390/3 "2014-10-01T07:35:01Z")

</div>

You can iterate through it in a jinja template, though only for a fixed depth as you would need to code each iteration level manually (AFAIK)

{% for p in ppools %}  
{% for devices in ppools[p].devices %}

etc

---

<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:** [October 1, 2014, 12:15pm UTC](https://forum.ansible.com/t/nested-dictionary-access/16390/4 "2014-10-01T12:15:14Z")

</div>

Restructing it slightly, you could use the “with\_subelements” lookup:

[http://docs.ansible.com/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/playbooks_loops.html#looping-over-subelements)

---

<div class="post-metadata">

**Author:** ![Sergey\_Sudakovich](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sergey\_Sudakovich](https://forum.ansible.com/u/Sergey_Sudakovich)\
**Post date:** [October 1, 2014, 5:36pm UTC](https://forum.ansible.com/t/nested-dictionary-access/16390/5 "2014-10-01T17:36:04Z")

</div>

Thank you, ended up flattening it and using subelements.
