# Accessing a list of dictionaries, within a list of dictionaries, using with\_items

**URL:** <https://forum.ansible.com/t/accessing-a-list-of-dictionaries-within-a-list-of-dictionaries-using-with-items/13612>\
**Category:** Ansible Project\
**Created:** [October 4, 2013, 9:51pm UTC](https://forum.ansible.com/t/accessing-a-list-of-dictionaries-within-a-list-of-dictionaries-using-with-items/13612 "2013-10-04T21:51:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Warner](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Robert\_Warner](https://forum.ansible.com/u/Robert_Warner)\
**Post date:** [October 4, 2013, 9:51pm UTC](https://forum.ansible.com/t/accessing-a-list-of-dictionaries-within-a-list-of-dictionaries-using-with-items/13612/1 "2013-10-04T21:51:54Z")

</div>

I asked in IRC about this, and was asked to post this here.

We use ansible to deploy multiple instances of our application. Our playbooks bring in application config data by including a var file that has a list of dictionaries containing instance specific data, like so:

# app\_settings.yml

applications:

- { domain: [site1.example.com](http://site1.example.com),  
mysql\_user: site1,

# etc etc

},

- { domain: [site2.example.com](http://site2.example.com),  
mysql\_user: site2,

# etc etc

},

Recently we needed to restrict access to certain environments, and are doing this using http basic auth. Ansible has an htpasswd tool! Excellent. Instead of having to manage the data manually, we can just store username/passwords along with our configuration file and let ansible take care of managing the hashing. The settings dictionary now looks like this:

# app\_settings.yml

applications:

- { domain: [site1.example.com](http://site1.example.com),  
mysql\_user: site1,  
http\_auth\_users:
- {user: ‘user1’, pass: ‘user1’},
- {user: ‘user2’, pass: ‘user2’},

# etc etc

},

the question is, how can we access this data in a loop? many of our tasks use with\_items: ${applications} and things like ${item.domain}, but here we need to do a double loop - once over the applications, and then an inner loop on the http\_auth\_users list, passing each dict in the list to the htpasswd module

I hear there is new support for this in the brand new subelement module?

Thanks as always for your help and hard work on ansible! We greatly appreciate it.

- Robert Warner

---

<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:** [October 5, 2013, 8:03pm UTC](https://forum.ansible.com/t/accessing-a-list-of-dictionaries-within-a-list-of-dictionaries-using-with-items/13612/2 "2013-10-05T20:03:18Z")

</div>

Hi Robert,

Yep, this looks like with\_subelements to me.

I recently added some docs on it:

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

In the above, you’d be doing:

with\_subelements:

- applications
- http\_auth\_users

And then each {{ item.0 }} would be the whole data item, and {{ item.1 }} would be each user in the http\_auth\_user list

For instance, the mysql\_user would be {{ item.0.mysql\_user }} and each http\_auth user would be like so:

{{ item.1.user }}

Let me know if this works for you or if you have further questions.

Thanks!
