Selivanov Pavel
Selivanov Pavel

Reputation: 360

Ansible custom nested loops with subelements

This is brief and expressive data structure in my vars/main.yml file:

webapp_dirs:
  - /var/www/example.com
  - /var/www/test.example.ru

webapp_acls:
  - dirs:
    - app
    - web
    - src
    - vendor
    perm: rX
    recursive: yes
  - dirs:
    - app/cache
    - app/logs
    perm: rwX
    recursive: no

I want to iterate over first list, then over second list, and finally over nested list in second list:

for top_dir in webapp_dirs:
    for acl in webapp_acls:
        for dir in i.dirs:
               ....

I have with_subelements and with_nested, but they can not be combined. Is there any way to do what I want in ansible?

UPD

@chrism gave me a good idea, but I can not make outer loop work:

- include: app-permissions.yml
  vars:
    webapp_dir: "{{ item }}"
  with_items:
    - /var/www/example.com

app-permissions.yml contains inner loop over variables. This fails with error {"failed": true, "msg": "ERROR! an unexpected type error occurred. Error was can only concatenate list (not \"str\") to list"}

Upvotes: 2

Views: 1164

Answers (1)

chrism
chrism

Reputation: 1661

In ansible 2.0 you can have nested loops via - include playbook.yml. Where the outerloop includes playbook.yml and the inner loop is in playbook.yml

Upvotes: 2

Related Questions