エック
エック

Reputation: 65

array over array create folder ansible

I tried with_items with * but look like it doesn't support.

vars/main.yml

---
Z:
  A1:
    - "a"
    - "b"
  A2: "c"
  A3:
    - "d"
    - "e"

tasks/main.yml

---
- name: Create folder
  file:
    path: "{{ item }}"
    state: directory
    mode: '0755'
    owner: tomcat
    group: tomcat
  with_items:
    - "/opt/Z/{{ Z.A1.* }}"
    - "/opt/Z/{{ Z.A1.* }}/in"
    - "/opt/Z/{{ Z.A1.* }}/in/output/"
    - "/opt/Z/{{ Z.A1.* }}/in/output/fail"
    - "/opt/Z/{{ Z.A1.* }}/in/output/success"

I would like to get the following output, I'm not sure how to use with_items with array over array.

  /opt/Z/a
  /opt/Z/a/in
  /opt/Z/a/in/output/
  /opt/Z/a/in/output/fail
  /opt/Z/a/in/output/success

  /opt/Z/b
  /opt/Z/b/in
  /opt/Z/b/in/output/
  /opt/Z/b/in/output/fail
  /opt/Z/b/in/output/success

Upvotes: 1

Views: 182

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 67959

The filter product does the job. For example the play

      vars:
        Z:
          A1:
            - "a"
            - "b"
          A2:
            - "c"
          A3:
            - "d"
            - "e"
        list2:
          - ""
          - "/in"
          - "/in/output/"
          - "/in/output/fail"
          - "/in/output/success"

      tasks:
        - debug:
            msg: "/opt/Z/{{ item.0 }}{{ item.1 }}"
          loop: "{{ Z.A1|product(list2)|list }}"

gives

      msg: /opt/Z/a
      msg: /opt/Z/a/in
      msg: /opt/Z/a/in/output/
      msg: /opt/Z/a/in/output/fail
      msg: /opt/Z/a/in/output/success
      msg: /opt/Z/b
      msg: /opt/Z/b/in
      msg: /opt/Z/b/in/output/
      msg: /opt/Z/b/in/output/fail
      msg: /opt/Z/b/in/output/success

Upvotes: 3

エック
エック

Reputation: 65

It looks good when using with debug, but when i use loop with file the output is bad :(

- name: Create folders
  file:
    path: "/opt/Z/{{ item.0 }}{{ item.1 }}"
    state: directory
    mode: '0755'
  loop:
    - "{{ Z.A1|product(list2)|list }}"

gives

# tree
.
└── Z
    └── ['a',\ '']['a',\ '
        └── in']

Upvotes: 0

Related Questions