Problem with local variables in Jinja2 template includes

141 views
Skip to first unread message

Mike Yumatov

unread,
Mar 21, 2014, 3:28:58 AM3/21/14
to ansible...@googlegroups.com
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 %}
{% 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
[2]: https://github.com/ansible/ansible/blob/14499e8bf3ed10f7818ec67e1f865d68e4a2a60b/lib/ansible/utils/template.py#L177-L184

Michael DeHaan

unread,
Mar 21, 2014, 4:30:40 PM3/21/14
to ansible...@googlegroups.com
There was a post about this earlier IIRC.

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

--Michael





--
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/2001111395386938%40web1m.yandex.ru.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages