Combinations of lists

25 views
Skip to first unread message

Chris Searle

unread,
Dec 7, 2015, 1:55:38 PM12/7/15
to Ansible Project
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

Mike Biancaniello

unread,
Dec 7, 2015, 2:32:24 PM12/7/15
to Ansible Project
have you tried ansible_nested (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]"
}

Chris Searle

unread,
Dec 7, 2015, 2:43:34 PM12/7/15
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages