45

The template looks like this:

solr.replication.master=
    {% if ansible_eth0.ipv4.address == servermaster.eth0 %}
        false
    {% else %}
        true
    {% endif %}

solr.replication.slave=false

And the output should look like this:

solr.replication.master=true
solr.replication.slave=false

What I am actually getting is:

solr.replication.master=truesolr.replication.slave=false

I understand that Jinja2 strips whitespace, and that ansible is probably configuring this by default. But it does not seem to honor -/+ whitespace tags.

Is there a way to force a line break?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Oliver Lorton
  • 709
  • 1
  • 6
  • 21

5 Answers5

37

Add the following line to your template at first position:

#jinja2: trim_blocks:False
Acti67
  • 577
  • 4
  • 11
  • Where did you find this? Documentation reference would be great because I can't find it and I think I need something very similar but not this exactly – byoungb Mar 30 '17 at 18:34
  • This results in every line that contains only conditionals being printed as a blank line. Might be ok for some, but... – Alain Collins May 12 '17 at 18:22
  • @byoungb See [this section of the docs](https://jinja.palletsprojects.com/en/2.10.x/templates/#whitespace-control), and also, [the source](https://github.com/pallets/jinja/blob/1af1205a9c9e884c49f74eb3e75dabbcee1b1967/jinja2/environment.py#L113) for the environment options may be of use. – Lukas Juhrich Oct 26 '19 at 17:23
  • 1
    Yeah actually I meant to ask where it was documented that you could set jinja2 environment variables as comments at the top of templates. – byoungb Oct 29 '19 at 13:27
  • documentation about such comment in ansible template: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html#notes – simohe Oct 06 '20 at 09:56
21

I had the same issue. I solved it by adding

{{''}}

to the end of the line, for example:

solr.replication.master={% if ansible_eth0.ipv4.address == servermaster.eth0 %}false{% else %}true{% endif %}{{''}}

This inserts an empty string literal, with the side effect that whitespace is not stripped.

Peter Lloyd
  • 561
  • 5
  • 3
8

As you mentioned -/+ whitespace tags are not honored, nor are line macros enabled (at least not %% or # or ##).

trim_blocks is enabled in ansible. The only thing that I found that does work, is that trim_blocks ignores only the first newline

For your example, just adding an extra newline should be sufficient

solr.replication.master={% if ansible_eth0.ipv4.address == servermaster.eth0 %}false{% else %}true{% endif %}

solr.replication.slave=false
Flair
  • 2,609
  • 1
  • 29
  • 41
00500005
  • 3,727
  • 3
  • 31
  • 38
2

I believe using a ternary filter might help.

solr.replication.master={{ (ansible_eth0.ipv4.address == servermaster.eth0) | ternary('false', 'true') }}
solr.replication.slave=false
moon.musick
  • 5,125
  • 2
  • 23
  • 23
1

As workaround you can add to your template

{% raw %}{% endraw %}
panticz
  • 2,135
  • 25
  • 16