jinja2 line formatting (white space)

41 views
Skip to first unread message

andrew...@jirotech.com

unread,
Jan 13, 2017, 12:46:11 AM1/13/17
to Ansible Project
I have this jinja2 going on in a play:

  with_items:  "{% for host in play_hosts -%}
                 {% if (host != inventory_hostname) -%}
                  {{host}} slave


                 {% endif -%}
                {% endfor -%}"

which basically appends some lines to a file with a line break, e.g.
line1
line2

This is working how I need, however my OCD wants to get rid of the blank line in the script.

If I try this (note the minus sign infront of "endif" and the \n after "slave" 
  with_items:  "{% for host in play_hosts -%}
                 {% if (host != inventory_hostname) -%}
                  {{host}} slave\n
                 {%- endif -%}
                {% endfor -%}"
the new line is deleted and the hosts are printed in line with no spacing. e.g.
line1line2

If I leave in the \n but remove the minus infront of endif, I end up with
line1
 line2
(note the indent)

It also works if I have it all in one line but its hard to read, and I don't want to template the file.

Is there a trick to format this correctly or should I just suck it up? 


Reply all
Reply to author
Forward
0 new messages