Simply  Seth
Simply Seth

Reputation: 3496

Create directories with Ansible using two separate lists

I want to create a series of directory trees dependent on two seperate lists.

Example:

---
# variable file ...
datacenters:
  - london
  - paris
types: 
  - databases
  - baremetal
  - vms

So I want my trees to be like so ...

    dest: "/{{ datacenter.0 }}/{{ types.0 }}"
    dest: "/{{ datacenter.0 }}/{{ types.1 }}"
    dest: "/{{ datacenter.0 }}/{{ types.2 }}"
    dest: "/{{ datacenter.1 }}/{{ types.0 }}"
    dest: "/{{ datacenter.1 }}/{{ types.1 }}"
    dest: "/{{ datacenter.1 }}/{{ types.2 }}"
    dest: "/{{ datacenter.N }}/{{ types.N }} .... etc

I'm not exactly sure how to do this without using an include file ....

Upvotes: 1

Views: 485

Answers (1)

helloV
helloV

Reputation: 52393

You can use Nested Loops

- name: Test with_nested
  hosts: localhost
  vars:
    datacenters:
      - london
      - paris
    types:
      - databases
      - baremetal
      - vms

  tasks:
   - name: Do it
     debug: msg="{{item[0]}}/{{item[1]}}"
     with_nested:
       - datacenters
       - types

Upvotes: 3

Related Questions