# Problem with local variables in Jinja2 template includes

**URL:** <https://forum.ansible.com/t/problem-with-local-variables-in-jinja2-template-includes/14819>\
**Category:** Ansible Project\
**Created:** [March 21, 2014, 7:28am UTC](https://forum.ansible.com/t/problem-with-local-variables-in-jinja2-template-includes/14819 "2014-03-21T07:28:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Yumatov](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mike\_Yumatov](https://forum.ansible.com/u/Mike_Yumatov)\
**Post date:** [March 21, 2014, 7:28am UTC](https://forum.ansible.com/t/problem-with-local-variables-in-jinja2-template-includes/14819/1 "2014-03-21T07:28:58Z")

</div>

Hi

It seems there is a problem with locals in Jinja2 templates. For example, this works in vanilla Jinja2 environment, but doesn't work in ansible environment ("One or more undefined variables: 'publisher' is undefined"):

{# simplified config.xml.j2 #}  
\<config\>  
{% for publisher in item.publishers %}  
&nbsp;&nbsp;{% include 'publisher.xml.j2' %}  
{% endfor %}  
\</config\>

{# simplified publisher.xml.j2 #}  
\<report\>{{ publisher.report }}\</report\>

In Jinja2 local variables are passed to the new context with 'l\_' prefix. This prefix is stripped in vanilla environment [1], but that is not true in case of ansible environment [2]. So in the latter case 'publisher' variable is available in 'publisher.xml.j2' template as 'l\_publisher'.

Is this an intended feature, or should I fill a bug report?

[1]: [https://github.com/mitsuhiko/jinja2/blob/27ffd01f95cad7740497a32b22e4ef9d29762162/jinja2/runtime.py#L67-L69](https://github.com/mitsuhiko/jinja2/blob/27ffd01f95cad7740497a32b22e4ef9d29762162/jinja2/runtime.py#L67-L69)  
[2]: [https://github.com/ansible/ansible/blob/14499e8bf3ed10f7818ec67e1f865d68e4a2a60b/lib/ansible/utils/template.py#L177-L184](https://github.com/ansible/ansible/blob/14499e8bf3ed10f7818ec67e1f865d68e4a2a60b/lib/ansible/utils/template.py#L177-L184)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 21, 2014, 8:30pm UTC](https://forum.ansible.com/t/problem-with-local-variables-in-jinja2-template-includes/14819/2 "2014-03-21T20:30:40Z")

</div>

There was a post about this earlier IIRC.

Jinja2 includes in Ansible are not probably well wired up, please do file a ticket.

–Michael
