Ansible - 字典第 1 项中的模板:尝试编写从模板创建文件的任务
NODES:
node1:
server: host1
script: manage1
node2:
server: host2
script: manage2
node3:
server: host3
- name: Create files from templates
template:
src: "templ.j2"
dest: "/etc/init.d/{{item.key}}"
loop: "{{NODES|dict2items}}"
when: "{{ 'script' in item.value }}"
上面创建文件:
/etc/init.d/node1
/etc/init.d/node2
我找不到办法获得:
/etc/init.d/manage1
/etc/init.d/manage2
第二个问题:在寻找上述过程中,将字典更改为哈希列表(我宁愿保留字典):
NODES:
- node1:
server: host1
script: manage1
- node2:
server: host2
script: manage2
对于上面的列表,简单的文件创建工作正常:
- name: Create files
file:
path: "/etc/init.d/{{item.script}}"
state: touch
with_items: "{{ NODES }}"
但从模板创建类比文件不会:
- name: Create files template
file:
dest: "/etc/init.d/{{item.script}}"
src: templ.j2
with_items: "{{ NODES }}"
我从 Ansible 开始,不容易理解,尤其是第二种情况。
答案1
问题一:“我找不到办法得到”
/etc/init.d/manage1
/etc/init.d/manage2
A:
使用item.value.script
- name: Create files from templates
template:
src: "templ.j2"
dest: "/etc/init.d/{{ item.value.script }}"
loop: "{{ NODES|dict2items }}"
when: "{{ 'script' in item.value }}"