Jinja2: lstrip_blocks to manage indentation

According to Jinja2 documentation you can manage whitespace and tabular indentation with lstrip_blocks and trim_blocks options:

  • trim_blocks: If this is set to True the first newline after a block is removed (block, not variable tag!). Defaults to False.
  • lstrip_blocks: If this is set to True leading spaces and tabs are stripped from the start of a line to a block. Defaults to False.

By default Ansible templates have trim_blocks true and lstrip_blocks false. If you want to manage them you can do it if you set them at the very beginning of your template file as follows:

#jinja2: lstrip_blocks: "True (or False)", trim_blocks: "True (or False)"

So, if you have following Ansible code for /etc/hosts template:

{% for host in groups['webservers'] %}
    {% if inventory_hostname in hostvars[host]['ansible_fqdn'] %}
{{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['inventory_hostname'] }} MYSELF
    {% else %}
{{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host]['ansible_fqdn'] }} jcs-server{{ loop.index }} {{ hostvars[host]['inventory_hostname'] }}
    {% endif %}
{% endfor %}

By default Ansible will indent as you’re doing in your code: web01

But if you add at the top of your template the following code:

#jinja2: lstrip_blocks: “True”

Result will be without indentation: web01 web02 web03

That makes you able to write more readable jinja2 code in your templates.

Please, note that you can’t delete indentation of the code that will be written in your final file. So in our case we’ve defined hostvars variables at the very beginning of our line because we wanted them there. If you indent that code, your result will be indented.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s