Problem accessing variables in nested templates

510 views
Skip to first unread message

eob...@gmail.com

unread,
Mar 20, 2014, 4:49:13 PM3/20/14
to ansible...@googlegroups.com
I'm trying to use ansible to generate a file using nested templates, and I'm running into issues with
having the variables available to me in the nested templates.  I'll try to explain this concisely.

File ./hosts.yml:
---
- hosts: myhosts
  roles:
     - role: nestedtemplate
       my_configs:
         - name: config1
           section1:
             - { type: type1, params: { path: "hi" } }
             - { type: type2, params: { path: "bye" } }


Essentially, my_configs represents a list of config files to generate.  For each section of the config file,
there is a list of dictionaries each containing a type (the config sub-template to use) and a params dictionary,
containing parameters to be substituted as necessary in that template.

File ./roles/nestedtemplate/tasks/main.yaml:
---
- name: nested template test
  template: src=testfile_skeleton.j2
            dest=/tmp/testfile-{{ item.name }}
  with_items: my_configs

File ./roles/nestedtemplate/templates/testfile_skeleton.j2:
# Config file skeleton

{% for input in item.section1 %}

  {{ input }}

  {% set incfile = 'my_' + input.type + '.conf.j2' %}
  {% include incfile %}
{% endfor %}


File ./roles/nestedtemplate/templates/my_type1.j2:
---------
type1 subtemplate


File ./roles/nestedtemplate/templates/my_type2.j2:
---------
type2 subtemplate


When I run the playbook, the generated file looks like this:
# Config file skeleton


  {'type': 'type1', 'params': {'path': 'hi'}}

    ---------
type1 subtemplate

  {'type': 'type2', 'params': {'path': 'bye'}}

    ---------
type2 subtemplate


Note that the {{ input }} I put into the skeleton template does what I'd expect.  However, any
attempt to use {{ input }} in any of the nested templates fails with an error saying that input
is undefined.  Am I doing something wrong here?  Is there an ansible-y way to accomplish what
I'm attempting?

Thanks in advance,
E

Matt Martz

unread,
Mar 21, 2014, 8:39:40 AM3/21/14
to eob...@gmail.com, ansible...@googlegroups.com
What version of jinja2 are you running?

According to the docs[1], as of 2.1 this should work.  Something else to try would be something like:

{% include incfile with context %}

Not sure if something would be disabling context, so maybe this would override that if that is indeed the problem.

-- 
Matt Martz
ma...@sivel.net
--
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/f45f488b-a614-4f51-8cd9-fcee655c54ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eob...@gmail.com

unread,
Mar 21, 2014, 8:53:49 AM3/21/14
to ansible...@googlegroups.com, eob...@gmail.com
Running ansible 1.5 with Jinja2  2.7.2 on Debian 7.  I should have mentioned - I did try the 'with context' change, but it made no difference.

Reply all
Reply to author
Forward
0 new messages