muffel
muffel

Reputation: 7370

disable `trim_blocks` for `blockinfile` module

The template module of Ansible 2.7 allows to disable the trim_blocks setting of jinja2.

What I want is to disable this setting for the blockinfile module, but don't find any information about it. I even tried to use the template lookup plugin with any look.

Is there a way to disable this in a play for blockinfile?

I do need this for additional whitespace control. Consider for example the following (simplified and stripped-down) template for an SSH config file:

{%- for host in groups.linux %}
{%- set vars = hostvars[host] %}
Host {{ vars.inventory_hostname_short }}
{#- add the fqdn as alias if present #}
{%- if host != vars.inventory_hostname_short %} {{ host }}{% endif %}
Hostname {{ vars.ansible_ssh_host }}
IdentityFile {{ vars.ansible_ssh_private_key_file }}
{% endfor %}

This allows me to control one empty line between entries and to append content (the fqdn in this case) to the previous line. With trim_blocks enabled I would need to concat strings in variables, or is there a better way to achieve something similar?

Upvotes: 1

Views: 802

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68044

It is possible to configure the template

> cat my_template.j2
#jinja2: trim_blocks:False
{%- for host in groups.linux %}
(continue)

and use it in blockinfile

- blockinfile:
    block: "{{ lookup('template', 'my_template.j2') }}"
  (continue)

Upvotes: 1

Related Questions