Reputation: 403
I have a play as follows
- name: create the unison preference file
template:
src: default.prf.j2
dest: /root/.unison/{{ item }}.prf
with_items: groups['ndeployslaves']
The contents of the default.prf.j2 file is as follows
root = /home
root = ssh://root@{{ item }}//home
ignore = Path virtfs
ignore = Path */mail
The item variable is not working in the template and I am getting the error
TASK [unison_master : create the unison prefrence file] ************************ fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "'item' is undefined"}
How do I reference an item inside a template used in a play?
Upvotes: 2
Views: 3402
Reputation: 403
The error was caused by an indentation error.
The with_items: groups['ndeployslaves']
was indented a level deeper than it should have.
Upvotes: 1
Reputation: 3137
Since it's not letting you use {{item}} in the template, you could do this:
- 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'] }}"
The contents of default.prf on your local host should be:
root = /home
root = ssh://
ignore = Path virtfs
ignore = Path */mail
However I have {{item}} working in a template. Are you sure your whitespace is correct? src and dest need to be indented one level deeper than template, but with_items needs to be on the same level as template.
- name: create the unison preference file
template:
src: default.prf.j2
dest: "/root/.unison/{{ item }}.prf"
with_items: "{{ groups['ndeployslaves'] }}"
Upvotes: 1