# Loop over a list that points at structures

**URL:** <https://forum.ansible.com/t/loop-over-a-list-that-points-at-structures/15897>\
**Category:** Ansible Project\
**Created:** [August 8, 2014, 6:56pm UTC](https://forum.ansible.com/t/loop-over-a-list-that-points-at-structures/15897 "2014-08-08T18:56:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [August 8, 2014, 6:56pm UTC](https://forum.ansible.com/t/loop-over-a-list-that-points-at-structures/15897/1 "2014-08-08T18:56:35Z")

</div>

I have structures in group\_vars files that looks roughly like the following:

Accounts1:  
files:

- { src: ‘src\_path/filename1’, dest: ‘dest\_path/filename1’ }
- { src: ‘src\_path/filename2’, dest: ‘dest\_path/filename2’ }  
app\_groups:
- { name: ‘group1’, gid: ‘1234’ }
- { name: ‘group2’, gid: ‘1235’ }
- { name: ‘group3’, gid: ‘1236’ }  
app\_accounts:
- { name: ‘account1’, uid: ‘1234’, home: ‘/home/account1’, comment: ‘account1’ }
- { name: ‘account2’, uid: ‘1235’, home: ‘/home/account2’, comment: ‘account3’ }
- { name: ‘account3’, uid: ‘1236’, home: ‘/home/account3’, comment: ‘account3’ }

Accounts2:  
files:

- { src: ‘src\_path/filename3’, dest: ‘dest\_path/filename3’ }
- { src: ‘src\_path/filename4’, dest: ‘dest\_path/filename4’ }  
app\_groups:
- { name: ‘group4’, gid: ‘2234’ }
- { name: ‘group5’, gid: ‘2235’ }
- { name: ‘group6’, gid: ‘2236’ }  
app\_accounts:
- { name: ‘account4’, uid: ‘2234’, home: ‘/home/account1’, comment: ‘account4’ }
- { name: ‘account5’, uid: ‘2235’, home: ‘/home/account2’, comment: ‘account5’ }
- { name: ‘account6’, uid: ‘2236’, home: ‘/home/account3’, comment: ‘account6’ }

And then I have a list that looks like this:  
Accounts: [‘Accounts1’, ‘Accounts2’]

I want to be able to iterate over the Accounts list to refer to the members of the structures something like the following:

group: name=“{{item.app\_groups.name}}” gid=“{{item.app\_groups.gid}}”  
with\_items: Accounts

It isn’t quite working. What I am finding is that ‘item’ in the group task is being populated with the strings “Accounts1” and “Accounts2”, but what I really want in ‘item’ is the objects named Accounts1 and Accounts2. So is there some way make ‘item’ be a reference to an object instead of an object itself? I have tried ‘with\_item’, ‘with\_nested’, and ‘with\_dict’, all to no avail.  
Thanks,  
-Mark

---

<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:** [August 8, 2014, 11:27pm UTC](https://forum.ansible.com/t/loop-over-a-list-that-points-at-structures/15897/2 "2014-08-08T23:27:22Z")

</div>

Yeah you are going to need to write your own iterator for a lookup plugin at that level of complexity.

Take a look at “with\_subelements” for one example of one that is almost there.

Or you could just restructure your info to make it easier, if it was coming from a YAML file.
