# jinja2 line formatting (white space)

**URL:** <https://forum.ansible.com/t/jinja2-line-formatting-white-space/23447>\
**Category:** Ansible Project\
**Created:** [January 13, 2017, 5:46am UTC](https://forum.ansible.com/t/jinja2-line-formatting-white-space/23447 "2017-01-13T05:46:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew.hudson](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@andrew.hudson](https://forum.ansible.com/u/andrew.hudson)\
**Post date:** [January 13, 2017, 5:46am UTC](https://forum.ansible.com/t/jinja2-line-formatting-white-space/23447/1 "2017-01-13T05:46:11Z")

</div>

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?
