关于Ansible的问题

关于Ansible的问题,ansible,jinja2,Ansible,Jinja2,我已经开始接受责任培训。 我有一个非常简单的用例:我正在尝试使用jinja模板生成html文件。 在此处找到我的yml文件: --- - name: "Generate html file for each host" hosts: all connection: local gather_facts: yes vars: host_inventory: "localhost" inventory_dir: "/home/ansible/Ansible/ch02

我已经开始接受责任培训。
我有一个非常简单的用例:我正在尝试使用jinja模板生成html文件。
在此处找到我的yml文件:

---

- name: "Generate html file for each host"
  hosts: all
  connection: local
  gather_facts: yes
  vars:
    host_inventory: "localhost"
    inventory_dir: "/home/ansible/Ansible/ch02/var/www/html/inventory"
  tasks:
    - name: "Create template directory"
      file:
        path: "{{playbook_dir}}"
        owner: "ansible"
        group: "ansible"
        mode: "0755"
        state: "directory"
      delegate_to: "{{host_inventory}}"
    - name: "Html file generation"
      template:
        src: "host.html.j2"
        dest: "{{playbook_dir}}/{{inventory_hostname}}.html"
      delegate_to: "{{host_inventory}}"
在此处找到jinja文件:

<html>
  <head>
    <title> host {{inventory_hostname}} </title>
  </head>
  <body>
    <p> 
      This host is called {{inventory_hostname}} 
    </p>
    <p>
      Find below a list of IPv4 adresses :
    </p>
    <ul>
      {% for ip in ansible_all_ipv4_addresses %}
      <li> {{ip}} </li>
      {% endfor }
    </ul>
  </body>
</html>
由于
{{inventory\u hostname}}
魔术变量解释错误,执行似乎失败:

ansible-playbook -i hosts.inv playbook.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this
 will change, but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings
can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details


PLAY [Generate html file for each host] *****************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [ha-proxy]
ok: [mysql_serv]
ok: [frontend-02]
ok: [frontend-01]

TASK [Create template directory] ************************************************************************************************
ok: [frontend-02 -> localhost]
ok: [mysql_serv -> localhost]
ok: [frontend-01 -> localhost]
ok: [ha-proxy -> localhost]

TASK [Html file generation] *****************************************************************************************************
fatal: [ha-proxy -> localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected '}'. String: <html>\n  <head>\n    <title> host {{inventory_hostname}} </title>\n  </head>\n  <body>\n    <p> \n      This host is called {{inventory_hostname}} \n    </p>\n    <p>\n      Find below a list of IPv4 adresses :\n    </p>\n    <ul>\n      {% for ip in ansible_all_ipv4_addresses %}\n      <li> {{ip}} </li>\n      {% endfor }\n    </ul>\n  </body>\n</html>\n"}
fatal: [frontend-01 -> localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected '}'. String: <html>\n  <head>\n    <title> host {{inventory_hostname}} </title>\n  </head>\n  <body>\n    <p> \n      This host is called {{inventory_hostname}} \n    </p>\n    <p>\n      Find below a list of IPv4 adresses :\n    </p>\n    <ul>\n      {% for ip in ansible_all_ipv4_addresses %}\n      <li> {{ip}} </li>\n      {% endfor }\n    </ul>\n  </body>\n</html>\n"}
fatal: [frontend-02 -> localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected '}'. String: <html>\n  <head>\n    <title> host {{inventory_hostname}} </title>\n  </head>\n  <body>\n    <p> \n      This host is called {{inventory_hostname}} \n    </p>\n    <p>\n      Find below a list of IPv4 adresses :\n    </p>\n    <ul>\n      {% for ip in ansible_all_ipv4_addresses %}\n      <li> {{ip}} </li>\n      {% endfor }\n    </ul>\n  </body>\n</html>\n"}
fatal: [mysql_serv -> localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected '}'. String: <html>\n  <head>\n    <title> host {{inventory_hostname}} </title>\n  </head>\n  <body>\n    <p> \n      This host is called {{inventory_hostname}} \n    </p>\n    <p>\n      Find below a list of IPv4 adresses :\n    </p>\n    <ul>\n      {% for ip in ansible_all_ipv4_addresses %}\n      <li> {{ip}} </li>\n      {% endfor }\n    </ul>\n  </body>\n</html>\n"}

PLAY RECAP **********************************************************************************************************************
frontend-01                : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
frontend-02                : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
ha-proxy                   : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
mysql_serv                 : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

ansible playbook-i hosts.inv playbook.yml
[弃用警告]:默认情况下,转换\u无效\u组\u字符设置被设置为允许组名称中使用坏字符,此选项无效
将更改,但在弃用时仍然是用户可配置的。此功能将在2.10版中删除。弃用警告
可以通过在ansible.cfg中设置deprecation\u warnings=False来禁用。
[警告]:在组名称中发现无效字符,但未替换,请使用-vvv查看详细信息
播放[为每个主机生成html文件]*****************************************************************************************
任务[收集事实]**********************************************************************************************************
ok:[ha代理]
ok:[mysql\u serv]
ok:[前端-02]
好:[前端-01]
任务[创建模板目录]************************************************************************************************
确定:[前端-02->本地主机]
确定:[mysql\u serv->localhost]
确定:[前端-01->本地主机]
确定:[ha代理->本地主机]
任务[Html文件生成]*****************************************************************************************************
致命:[ha代理->本地主机]:失败!=>{“changed”:false,“msg”:“AnsibleError:模板化字符串时模板错误:意外的“}”。字符串:\n\n主机{inventory\u hostname}\n\n\n\n此主机名为{{inventory\u hostname}\n

\n请在下面查找IPv4地址列表:\n

\n
    \n{%\n
  • {ip}
  • \n{%endfor}\n
\n\n\n} 致命:[frontend-01->localhost]:失败!=>{“changed”:false,“msg”:“AnsibleError:模板化字符串时模板错误:意外的“}”。字符串:\n\n主机{inventory\u hostname}\n\n\n\n此主机名为{{inventory\u hostname}\n

\n请在下面查找IPv4地址列表:\n

\n
    \n{%\n
  • {ip}
  • \n{%endfor}\n
\n\n\n} 致命:[frontend-02->localhost]:失败!=>{“changed”:false,“msg”:“AnsibleError:模板化字符串时模板错误:意外的“}”。字符串:\n\n主机{inventory\u hostname}\n\n\n\n此主机名为{{inventory\u hostname}\n

\n请在下面查找IPv4地址列表:\n

\n
    \n{%\n
  • {ip}
  • \n{%endfor}\n
\n\n\n} 致命:[mysql\u serv->localhost]:失败!=>{“changed”:false,“msg”:“AnsibleError:模板化字符串时模板错误:意外的“}”。字符串:\n\n主机{inventory\u hostname}\n\n\n\n此主机名为{{inventory\u hostname}\n

\n请在下面查找IPv4地址列表:\n

\n
    \n{%\n
  • {ip}
  • \n{%endfor}\n
\n\n\n} 重演********************************************************************************************************************** 前端-01:确定=2已更改=0无法访问=0失败=1跳过=0已获救=0已忽略=0 前端-02:确定=2已更改=0无法访问=0失败=1跳过=0已获救=0已忽略=0 ha代理:确定=2更改=0无法访问=0失败=1跳过=0获救=0忽略=0 mysql_serv:ok=2已更改=0无法访问=0失败=1已跳过=0已获救=0已忽略=0
您看到的错误是:

AnsibleError:模板化字符串时出现模板错误:意外“}”

这通常表示模板中存在语法错误。在你的情况下,你有一个打字错误。查看
for
循环:

      {% for ip in ansible_all_ipv4_addresses %}
      <li> {{ip}} </li>
      {% endfor }
      {% for ip in ansible_all_ipv4_addresses %}
      <li> {{ip}} </li>
      {% endfor }
      {% for ip in ansible_all_ipv4_addresses %}
      <li> {{ip}} </li>
      {% endfor %}