Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an Ansible template to honor new lines after a conditional

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?

like image 982
Oliver Lorton Avatar asked Mar 12 '14 11:03

Oliver Lorton


People also ask

How does the Jinja2 template helps Ansible?

Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts. You can use templating with the template module.

How are parameters used in template files in Ansible?

We need to have two parameters when using the Ansible Template module, such as: src: The source of the template file. It can be a relative and absolute path. dest: Dest is the destination path on the remote server.

How do I use Ansible templates?

Ansible templates are typically saved as . tpl files and support the use of variables, loops, and conditional expressions. Templates are commonly used to configure services based on variable values that can be set up on the playbook itself, in included variable files, or obtained via facts.

How does Jinja template work?

It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.


1 Answers

Add the following line to your template at first position:

#jinja2: trim_blocks:False 
like image 94
Acti67 Avatar answered Oct 04 '22 13:10

Acti67