# Combinations of lists

**URL:** <https://forum.ansible.com/t/combinations-of-lists/19954>\
**Category:** Ansible Project\
**Created:** [December 7, 2015, 6:51pm UTC](https://forum.ansible.com/t/combinations-of-lists/19954 "2015-12-07T18:51:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Searle](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Chris\_Searle](https://forum.ansible.com/u/Chris_Searle)\
**Post date:** [December 7, 2015, 6:51pm UTC](https://forum.ansible.com/t/combinations-of-lists/19954/1 "2015-12-07T18:51:09Z")

</div>

I get the feeling I’m missing something obvious - so feel free to tell me so 🙂

I seem to often want to do a combination of two lists - most often one that is static and one that is dependent on the group.

One example:

I have a set of apache vhosts. So - I have a variable that has a list of dicts - one per host. From that - I can create the htdocs dirs, log dirs, the apache config files etc etc. This works fine.

For apache I create the following directory structure per vhost:

/srv/www/[fqdn]

/srv/www/[fqdn]/htdocs  
/srv/www/[fqdn]/logs

But - I also want to apply a standard set of extended attributes (using acl) on the [fqdn], htdocs and logs dirs. So - for the standard set of acl’s I want to set - I have second (static) items list.

I can’t see how to apply each of the second list for each of the items in the first list.

Another example is managing a given list of four files for each of a set of given directories - the files list is a static list, the directories varies group to group.

So - I was looking for something similar to with\_together - but that doesn’t seem quite what I’m after.

If I give with\_together two lists like [a,b,c] and [1,2,3] I observe that I get [a,1], [b,2] and [c,3].

What I’m after is [a,1], [a,2], [a,3], [b,1], [b,2], [b,3], [c,1], [c,2] and [c,3]

What’s the best way to approach this?

Regards

Chris

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [December 7, 2015, 7:32pm UTC](https://forum.ansible.com/t/combinations-of-lists/19954/2 "2015-12-07T19:32:24Z")

</div>

have you tried ansible\_nested ([http://docs.ansible.com/ansible/playbooks\_loops.html#nested-loops](http://docs.ansible.com/ansible/playbooks_loops.html#nested-loops))

`  
Enter code here…—

- name: stuff  
hosts: localhost  
gather\_facts: no  
connection: local

vars:  
l: [a,b,c]  
n: [1,2,3]

tasks:

- name: show nested  
debug: msg=“[{{ item[0] }},{{ item[1] }}]”  
with\_nested:
- l
- n  
.  
`

results in:

`  
TASK: [show nested] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=[‘a’, 1]) =\> {  
“msg”: “[a,1]”  
}  
ok: [localhost] =\> (item=[‘a’, 2]) =\> {  
“msg”: “[a,2]”  
}  
ok: [localhost] =\> (item=[‘a’, 3]) =\> {  
“msg”: “[a,3]”  
}  
ok: [localhost] =\> (item=[‘b’, 1]) =\> {  
“msg”: “[b,1]”  
}  
ok: [localhost] =\> (item=[‘b’, 2]) =\> {  
“msg”: “[b,2]”  
}  
ok: [localhost] =\> (item=[‘b’, 3]) =\> {  
“msg”: “[b,3]”  
}  
ok: [localhost] =\> (item=[‘c’, 1]) =\> {  
“msg”: “[c,1]”  
}  
ok: [localhost] =\> (item=[‘c’, 2]) =\> {  
“msg”: “[c,2]”  
}  
ok: [localhost] =\> (item=[‘c’, 3]) =\> {  
“msg”: “[c,3]”  
}

`

---

<div class="post-metadata">

**Author:** ![Chris\_Searle](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Chris\_Searle](https://forum.ansible.com/u/Chris_Searle)\
**Post date:** [December 7, 2015, 7:43pm UTC](https://forum.ansible.com/t/combinations-of-lists/19954/3 "2015-12-07T19:43:34Z")

</div>

I cannot believe I missed that. Well - actually I can - I said I thought I was missing something obvious 🙂

I don’t know how many times I read the with\_nested docs without spotting it was _exactly_ what I needed - even by name. For some reason it just didn’t register.

Many thanks - worked perfectly.
