为`blockinfile`模块禁用`trim_blocks`

问题描述 投票:1回答:1

Ansible 2.7的template module允许禁用jinja2的trim_blocks设置。

我想要的是为blockinfile模块禁用此设置,但没有找到任何有关它的信息。我甚至尝试使用任何外观的模板查找插件。

有没有办法在blockinfile的游戏中禁用它?

我确实需要这个用于额外的空格控制。例如,考虑SSH配置文件的以下(简化和简化)模板:

{%- 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 %}

这允许我控制条目之间的一个空行并将内容(在这种情况下为fqdn)附加到前一行。启用trim_blocks后,我需要在变量中连接字符串,还是有更好的方法来实现类似的东西?

ansible jinja2
1个回答
1
投票

可以配置模板

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

并在blockinfile中使用它

- blockinfile:
    block: "{{ lookup('template', 'my_template.j2') }}"
  (continue)
© www.soinside.com 2019 - 2024. All rights reserved.