数组覆盖数组创建文件夹ansible



我尝试了with_items*,但看起来不支持。

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"

我想得到以下输出,我不知道如何将with_items与数组一起使用。

/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

过滤器产品完成了这项工作。例如播放

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 }}"

给出

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

debug一起使用时看起来不错,但与file一起使用loop时输出不好:(

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

给出

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

最新更新