jinja in task (template)

590 views
Skip to first unread message

vu001

unread,
Dec 14, 2016, 8:26:28 AM12/14/16
to Ansible Project
Hi to all, 
I have problem with evaluating jinja2 code in task (template)

This is playbook for test, just to see If I get what I want
---
- name: Demo of with_nested
  hosts: host1
  vars:
     FS_LIST:
        member1:
          FS: "/tmp/f1/dev"
          config_files: ["config1.xml","config2.xml"]
        member2:
          FS: "/tmp/f2/dev"
          config_files: ["config1.xml"]
  tasks:
    - name: Printing message
      debug: msg=" {% for i in item.config_files %} Values are {{item.FS}} and {{i}}\n{% endfor %} "
      with_items:
        - "{{FS_LIST.values()}}"

and it prints mi all permutation as I wish
            "msg": "  Values are /tmp/f1/dev and config1.xml\n Values are /tmp/f1/dev and config2.xml\n "
            "msg": "  Values are /tmp/f2/dev and config1.xml\n "


So i tried to implement this logic to template:
This template is just to check syntax:
---
- name: Demo of with_nested
  hosts: host1
  vars:
     FS_LIST:
        member1:
          FS: "/tmp/f1/dev"
          config_files: ["config1.xml","config2.xml"]
        member2:
          FS: "/tmp/f2/dev"
          config_files: ["config1.xml"]
  tasks:
    - name: Printing message
      template: src=/tmp/config1.xml.j2 dest={{item.FS}}/config1.xml
      with_items:
        - "{{FS_LIST.values()}}"

BUT
When i tried to do this:
---
- name: Demo of with_nested
  hosts: host1
  vars:
     FS_LIST:
        member1:
          FS: "/tmp/f1/dev"
          config_files: ["config1.xml","config2.xml"]
        member2:
          FS: "/tmp/f2/dev"
          config_files: ["config1.xml"]
  tasks:
    - name: Printing message
      template: {% for i in item.config_files %} src={{i}}.j2 dest={{item.FS}}/{{i}} {% endfor %}
      with_items:
        - "{{FS_LIST.values()}}"


I got error:
The offending line appears to be:

    - name: Printing message
      template: {% for i in item.config_files %} src={{i}}.j2 dest={{item.FS}}/{{i}} {% endfor %}
                 ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"


Any idea...thanks a lot...

Greg Langford

unread,
Dec 15, 2016, 6:23:24 AM12/15/16
to Ansible Project
Where you are defining the template task you should put double quotes round the jina code

e.g "{% for i in item.config_files %} src={{i}}.j2 dest={{item.FS}}/{{i}} {% endfor %}" otherwise the jinja syntax will not be evaluated.

I have however never used a for loop in this fashion so no idea if that will work, I would be interested to hear if it does.

Kind Regards

vu001

unread,
Dec 15, 2016, 7:24:59 AM12/15/16
to Ansible Project
I tried that also...but got error:

fatal: [host1]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'i' is undefined\n\nThe error appears to have been in '/etc/ansible/template_jinja_eval2.yml': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: Printing message\n      ^ here\n"}
Reply all
Reply to author
Forward
0 new messages