template substitution

41 views
Skip to first unread message

Peter Gray

unread,
Jun 1, 2016, 1:23:10 AM6/1/16
to Ansible Project
If I have in a template file

#
#  {% if i_want_yes %} yes {% else %} no {% endif %}
#

and the variable i_want_yes is True

I seem to get

#
#  yes #

as output. What happened to my newline and how do I get it back?

BTW, I am new to this and I have spent a little time in the jinga2 docs to no avail.







Johannes Kastl

unread,
Jun 1, 2016, 9:01:32 AM6/1/16
to ansible...@googlegroups.com
On 01.06.16 07:23 Peter Gray wrote:
> If I have in a template file
>
> #
> # {% if i_want_yes %} yes {% else %} no {% endif %}
> #

I normally write it like this:

{% if i_want_yes %}
# yes
{% else %}
# no
{% endif %}

Would that work for you?

Johannes

signature.asc

Brian Coca

unread,
Jun 1, 2016, 9:12:58 AM6/1/16
to ansible...@googlegroups.com
ansible uses the trim_blocks setting when invoking jinja, by default its 'yes'.

ansible allows for overrides by creating a special header:


```
#jinja2: trim_blocks: False
#
#  {% if i_want_yes %} yes {% else %} no {% endif %}
#
```

----------
Brian Coca

Peter Gray

unread,
Jun 1, 2016, 6:46:45 PM6/1/16
to ansible...@googlegroups.com
Many thanks for this information. For future reference where is this documented?
The interaction of ansible with jinga2 seems a bit opaque.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/GPGvcRXJzz0/unsubscribe.
To unsubscribe from this group and all its topics, 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/CACVha7d6DSia6-WTyeVWHfvRE6dyr7U5iYbo-8yCRpuWvCkz7Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Josh Smift

unread,
Jun 2, 2016, 12:03:57 AM6/2/16
to ansible...@googlegroups.com
PG> Many thanks for this information. For future reference where is this
PG> documented?

https://groups.google.com/forum/#!topic/ansible-project/qHyIjyal_Ic is a
thread where it came up before, which says

you can use a per template header to override this, you can see an
example in the template docs.

https://docs.ansible.com/template_module.html

which is now at http://docs.ansible.com/ansible/template_module.html, and
which only seems to mention it in a note saying

Since Ansible version 0.9, templates are loaded with trim_blocks=True.

and not any actual examples, or any explanation of what trim_blocks does,
so it could be hard to spot this if you didn't already know what you were
looking for.

PG> The interaction of ansible with jinga2 seems a bit opaque.

That page also doesn't mention
http://docs.ansible.com/ansible/playbooks_filters.html, which I've found
to be super useful in turning up examples of Ansible-specific Jinja filters.

-Josh (j...@care.com)

(apologies for the automatic corporate disclaimer that follows)




This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
Reply all
Reply to author
Forward
0 new messages