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.

[1] http://jinja.pocoo.org/docs/templates/#import-context-behavior
-- 
Matt Martz
m...@sivel.net

On March 21, 2014 at 12:10:24 AM, eob...@gmail.com (eob...@gmail.com) wrote:

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

--
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@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.

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/etPan.532c330c.2eb141f2.11a12%40mobiletuvix.rackspace.corp.
For more options, visit https://groups.google.com/d/optout.

Reply via email to