# Nested looping with dict/sub-list

**URL:** <https://forum.ansible.com/t/nested-looping-with-dict-sub-list/17456>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [February 5, 2015, 3:07pm UTC](https://forum.ansible.com/t/nested-looping-with-dict-sub-list/17456 "2015-02-05T15:07:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duncan\_Hutty](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@Duncan\_Hutty](https://forum.ansible.com/u/Duncan_Hutty)\
**Post date:** [February 5, 2015, 3:07pm UTC](https://forum.ansible.com/t/nested-looping-with-dict-sub-list/17456/1 "2015-02-05T15:07:54Z")

</div>

I'm having a bit of difficulty with getting the behaviour that I want in as concise a way as I think should be possible.

Here's the point:  
I'm having trouble with iterating over a hash where the value for one of the keys is itself a list. Is this possible with Ansible? Or would I need to write a custom iterator?

I think that, having read the docs on how Ansible does looping ([http://docs.ansible.com/playbooks\_loops.html](http://docs.ansible.com/playbooks_loops.html)) that I must be misunderstanding something, so I'm asking for illumination concerning the general case - this repository problem is just an example.

I want to configure /etc/apt/sources.list to contain:

deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise main  
deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise universe  
deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise-updates main  
deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise-updates universe  
deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise-security main  
deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise-security universe

My desired approach is to have a role called 'repositories', because I will need to take care of multiple platform versions/releases and non-distro/vendor repositories.

roles/repositories/defaults/main.yml:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 5, 2015, 4:18pm UTC](https://forum.ansible.com/t/nested-looping-with-dict-sub-list/17456/2 "2015-02-05T16:18:12Z")

</div>

I’m guessing what you want is:

apt\_repository: repo=“deb {{item.value.uri}} {{ ansible\_distribution\_release }}{{ item.value.suffix }} {{ item.components|join(” “) }}” state=present

This just uses the ‘join’ filter to join that components list into a space separated string.

---

<div class="post-metadata">

**Author:** ![Duncan\_Hutty](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@Duncan\_Hutty](https://forum.ansible.com/u/Duncan_Hutty)\
**Post date:** [February 5, 2015, 5:07pm UTC](https://forum.ansible.com/t/nested-looping-with-dict-sub-list/17456/3 "2015-02-05T17:07:05Z")

</div>

That's an interesting strategy for getting the sources.list.d configured, but it relies on the fact that that line defining a deb repository can take multiple components 'deb [http://mirrors.us.kernel.org/ubuntu/](http://mirrors.us.kernel.org/ubuntu/) precise main universe', so I'm no clearer on the real question about iterating over a dict with a sub-list.

&nbsp;&nbsp;(although it also made me notice that I want to sort the dict and it's not immediately obvious how to do so: "with\_dict: apt\_repositories | dictsort" doesn't work - I think because dictsort yields a list of key,value pairs not a real dict).

Thanks for the helpful reply!

Duncan Hutty
