Ansible 使用带有 blockinfile 的模板

005*_*005 4 jinja2 ansible-playbook ansible-2.x

blockinfile似乎用{{模板做基本的替换。该文档没有提到任何模板功能。

但是,似乎不可能做一个完整的模板,比如循环。这失败了template error while templating string: unexpected '%'

  blockinfile:
    dest: /etc/haproxy/haproxy.cfg
    marker: "# {mark} ANSIBLE CONFIG certs"
    block: |
      {% if certs %}
      bind *:443 ssl crt {% for cert in certs %}{{cert}} {{% endfor %}
      {% endif %}
Run Code Online (Sandbox Code Playgroud)

这似乎是一个基本用例,我可能想对配置文件进行一次性编辑,但仍然需要模板的强大功能。

请注意, usingwith_items在这里并没有真正做到我想要的,因为我只需要 1 行。

yae*_*shi 6

我认为问题在于您{在模板中有一个额外的内容。

      bind *:443 ssl crt {% for cert in certs %}{{cert}} {{% endfor %}
Run Code Online (Sandbox Code Playgroud)

应该

      bind *:443 ssl crt {% for cert in certs %}{{cert}} {% endfor %}
Run Code Online (Sandbox Code Playgroud)