use item in a template

80 views
Skip to first unread message

Anoop Alias

unread,
May 25, 2016, 10:22:04 AM5/25/16
to Ansible Project
cat main.yml

---

- name: Install and setup unison in nDeploy cluster master
  file: path=/root/.unison state=directory

- name: create the unison preference file
  template:
    src: default.prf.j2
    dest: /root/.unison/{{ item }}.prf
    with_items: groups['ndeployslaves']


cat default.prf.j2
# Unison preferences file
root = /home
root = ssh://root@{{ item }}//home
ignore = Path virtfs
ignore = Path */mail



However the item does not seem to be available in the template and I am getting the following error
TASK [unison_master : create the unison prefrence file] ************************
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "'item' is undefined"}



In short:

How to get the value of item variable in the task in a jinja template referenced in the same task?

Thanks in advance.
Anoop


Anoop Alias

unread,
May 25, 2016, 12:34:52 PM5/25/16
to Ansible Project
As an update the problem seem to be somewhere in the template module as it shows the same error even after I remove the variable item from the jinja2 template

---


- name: Install and setup unison in nDeploy cluster master
  file: path=/root/.unison state=directory

- name: debug this
  debug: "{{ item }}"
  with_items: "{{ groups['ndeployslaves'] }}"

- name: create the unison preference file
  template:
    src: default.prf.j2
    dest: "/root/.unison/{{ item }}.prf"
    with_items: "{{ groups['ndeployslaves'] }}"


The output is as below

TASK [unison_master : debug this] **********************************************
task path: /root/nDeploy/conf/nDeploy-cluster/roles/unison_master/tasks/main.yml:8
ok: [127.0.0.1] => (item=cpanel.sysally.net) => {
    "invocation": {
        "module_args": {}, 
        "module_name": "debug"
    }, 
    "item": "cpanel.sysally.net", 
    "msg": "Hello world!"
}

TASK [unison_master : create the unison preference file] ***********************
task path: /root/nDeploy/conf/nDeploy-cluster/roles/unison_master/tasks/main.yml:12
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "'item' is undefined"}




Note that default.prf.j2 does not contain any variables now.

Thanks in advance for any help on this.


Matt Martz

unread,
May 25, 2016, 1:27:47 PM5/25/16
to ansible...@googlegroups.com
It seems as though your `with_items` on your `template` task is indented too far.  It should be indented to the same level as the word `template`.  Just like you have it for your debug command.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a6d87a6c-9305-4cd0-87dd-10d736160d15%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Anoop Alias

unread,
May 25, 2016, 9:03:40 PM5/25/16
to ansible...@googlegroups.com
Hi Matt,

Yes, that was it. Thanks for the help.
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/FCch6798aZc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAD8N0v_AHCVjhAfBQuDZJ23eVMBMfbNgKUTWsB0k9g6hH9gNEA%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Anoop P Alias
Reply all
Reply to author
Forward
0 new messages