bug? Ignoring jinja variable substitution in templates

3,445 views
Skip to first unread message

Brice Burgess

unread,
Oct 21, 2013, 3:29:38 PM10/21/13
to ansible...@googlegroups.com
Is it possible to ignore variable substitution in templates? I've attempted {% raw %}, {% verbatim %}, slash escaping, and using jinja to output braces.

Reason: I am writing a playbook that generates playbooks; so need to preserve the curly braces. Here's an example:


<mytemplate.j2>
===

Test: Preserve curly braces, and NOT perform variable substitution:


First Attempt:
{% raw %}
  - { include: "{{ myvar }}" }
{% endraw %}


Second Attempt:
  - { include: "{{ '{{' }} myvar {{ '}}' }}" }

<playbook.yml>
===

---
- name: "test raw template output"
  hosts: localhost
 
  vars:
    myvar: "foo"
  
  tasks:
    - local_action: template src="mytemplate.j2" dest="/tmp/myoutput.txt"
     
</tmp/myoutput.txt> 
===

Test: Preserve curly braces, and NOT perform variable substitution:


First Attempt:

  - { include: "foo" }


Second Attempt:
  - { include: "foo" }





The desired output, of course, is:

First Attempt

  - { include: "{{ myvar }}" }


Can someone direct me on how to escape variable substitution? In my production scenario, I actually need variable substition to happen in parts of the template, and to NOT happen in other parts; hence not being able to use copy.




Brian Coca

unread,
Oct 21, 2013, 3:40:47 PM10/21/13
to ansible...@googlegroups.com
you can add an override header (small notice on template docs that is on my list to expand)
make this the first line of the template:
#jinja2:  variable_start_string: "[%" , variable_end_string: "%]"


then you can use [% var %] on the 'outer' template and {{ var }} won't get touched in the 'inner' template.


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Brice Burgess

unread,
Oct 21, 2013, 5:30:17 PM10/21/13
to ansible...@googlegroups.com
Brian,

Thanks for the workaround! I saw mention of this && didn't think to apply.  Do you think supporting {% raw %} is worthwhile? I can open a ticket if so.

Michael DeHaan

unread,
Oct 21, 2013, 9:23:44 PM10/21/13
to ansible...@googlegroups.com
raw is standard Jinja2, we didn't do anything to disable it.




--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Brice Burgess

unread,
Oct 24, 2013, 3:17:02 PM10/24/13
to ansible...@googlegroups.com
Brian,

I have tried this workaround. Unfortunately curly braces STILL get evaluated. E.g. in the following template

<mytemplate.j2>
#jinja2:  variable_start_string: "[%" , variable_end_string: "%]"
---
# [% app_name %] tasks

- { include: "{{ repo_automation_dir }}/test.yml" }


Both [% app_name %]  and {{ repo_automation_dir }} get evaluated!

I simply cannot find a way to preserve curly braces... Any idea? I filed an issue relating to this; https://github.com/ansible/ansible/issues/4638









On Monday, October 21, 2013 2:40:47 PM UTC-5, Brian Coca wrote:

Michael DeHaan

unread,
Oct 25, 2013, 7:39:03 PM10/25/13
to ansible...@googlegroups.com
Thanks for the ticket.  It's in queue!


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages