Double loop Ansible

In fact you can't. Loops in Ansible are one-dimensional. There is a trick which used to work in previous versions and will again work in Ansible 2.0:

You can have one loop together with an include statement and in that included yml you have the 2nd loop. So something along these lines:

main.yml:

- include: nested_loop.yml obj={{ item }}
  with_items: objs

nested_loop.yml:

- name: create files
  file: path={{obj.key1 }}/{{ item }} state=touch
  with_items: obj.key2

Again, this will not work in the current version (1.9.2) of Ansible. The feature was dropped as it caused some problems but will be supported again in Ansible 2.0, so it should be available when you use the devel branch from github.

You can install from github source with this:

git clone https://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
sudo make install

Take a look at with_subelements in Ansible's docs for loops.

  1. You need to create directories:
  2. Iterate though objs and create files:

Here is an example:

---

- hosts: localhost
  gather_facts: no
  vars:
    objs:
      - { key1: value1, key2: [ value2, value3] }
      - { key1: value4, key2: [ value5, value6] }
  tasks:
    - name: create directories
      file: path="{{ item.key1 }}"  state=directory
      with_items:
        objs

    - name: create files
      file: path="{{ item.0.key1 }}/{{ item.1 }}"  state=touch
      with_subelements:
        - objs
        - key2

An output is pretty self explanatory, the second loop iterates through the values the way you need it:

PLAY [localhost] ************************************************************** 

TASK: [create files] ********************************************************** 
changed: [localhost] => (item={'key2': ['value2', 'value3'], 'key1': 'value1'})
changed: [localhost] => (item={'key2': ['value5', 'value6'], 'key1': 'value4'})

TASK: [create files] ********************************************************** 
changed: [localhost] => (item=({'key1': 'value1'}, 'value2'))
changed: [localhost] => (item=({'key1': 'value1'}, 'value3'))
changed: [localhost] => (item=({'key1': 'value4'}, 'value5'))
changed: [localhost] => (item=({'key1': 'value4'}, 'value6'))

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=2    unreachable=0    failed=0