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
后,我需要在变量中连接字符串,还是有更好的方法来实现类似的东西?
可以配置模板
> cat my_template.j2
#jinja2: trim_blocks:False
{%- for host in groups.linux %}
(continue)
并在blockinfile中使用它
- blockinfile:
block: "{{ lookup('template', 'my_template.j2') }}"
(continue)