Salt: Jinja template ignore new line after {% %}

Created on 27 Jan 2014  路  5Comments  路  Source: saltstack/salt

Hi,
this is a feature request for being able to write more legible templates. For example this jinja template:

....
tar --ignore-failed-read \
    --exclude-tag-under=.NO-BACKUP \
    -cpf - \
{% for dir in pillar['global_vars']['backup']['allways'] %}
    {{dir}} \
{% endfor %}
{% if 'backup_dirs' in pillar['server_roles'][grains['server_role']] %}
{% for dir in pillar['server_roles'][grains['server_role']]['backup_dirs'] %}
    {{dir}} \
{% endfor %}
{% endif %}
| gpg \
  -r DevOps --encrypt \
....

Creates this file:

...
tar --ignore-failed-read \
    --exclude-tag-under=.NO-BACKUP \
    -cpf - \

    /etc \

    /var/spool/cron \


    /var/lib/tomcat7 \

    /usr/something \

    /opt/another_thing \

| gpg \
  -r DevOps --encrypt \
...

while I'll expect the {% %} logic to be completely invisible to the final file, like:

...
tar --ignore-failed-read \
    --exclude-tag-under=.NO-BACKUP \
    -cpf - \
    /etc \
    /var/spool/cron \
    /var/lib/tomcat7 \
    /usr/something \
    /opt/another_thing \
| gpg \
  -r DevOps --encrypt \
...

to that lines with %}[ \t]*$ should not result in empty lines. My suggestion would be, for example, remove the new line in the end to append the next line. Thanks.

Most helpful comment

I'm not sure if you can as I've never investigated it. I usually just use {% -%} and {%- %}.

All 5 comments

This is due to Jinja, take a look at the whitespace control docs.

Any idea how to set trim_blocks and lstrip_blocks for salt?

I'm not sure if you can as I've never investigated it. I usually just use {% -%} and {%- %}.

Thank you!

Any idea how to set trim_blocks and lstrip_blocks for salt?

Maybe it works as in ansible, the first line of the file:
#jinja2: trim_blocks:False

from https://stackoverflow.com/a/39210691/4124767

Was this page helpful?
0 / 5 - 0 ratings