我有一出戏如下
- name: create the unison preference file
template:
src: default.prf.j2
dest: /root/.unison/{{ item }}.prf
with_items: groups['ndeployslaves']default.prf.j2文件的内容如下
root = /home
root = ssh://root@{{ item }}//home
ignore = Path virtfs
ignore = Path */mail项目变量在模板中不起作用,因此我得到了错误。
任务unison_master :创建unison_master文件*致命: 127.0.0.1: FAILED!=> {"failed":true,"msg":"'item‘是未定义的“}
如何引用在戏剧中使用的模板中的项目?
发布于 2016-05-25 15:30:44
由于它不允许在模板中使用{{item},所以可以这样做:
- name: create the unison preference file
copy:
src: default.prf
dest: "/root/.unison/{{ item }}.prf"
force: no
with_items: "{{ groups['ndeployslaves'] }}"
- name: edit preference file
lineinfile:
dest: "/root/.unison/{{ item }}.prf"
line: "root = ssh://root@{{item}}//home"
regexp: '^root = ssh://'
with_items: "{{ groups['ndeployslaves'] }}"本地主机上的default.prf内容应该是:
root = /home
root = ssh://
ignore = Path virtfs
ignore = Path */mail但是,我有{{item}在模板中工作。你确定你的空格是正确的吗?src和dest需要比模板更深一层缩进,但with_items需要与模板在同一级别上。
- name: create the unison preference file
template:
src: default.prf.j2
dest: "/root/.unison/{{ item }}.prf"
with_items: "{{ groups['ndeployslaves'] }}"发布于 2016-05-26 01:11:09
此错误是由缩进错误引起的。
with_items: groups['ndeployslaves']被缩进了一个比它应有的更深的层次。
https://stackoverflow.com/questions/37436768
复制相似问题