我已经开始了 Ansible 培训。
我有一个非常简单的用例:我正在尝试使用 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}}"
Run Code Online (Sandbox Code Playgroud)
在这里找到 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>
Run Code Online (Sandbox Code Playgroud)
主机库存很简单:
[ha-proxies]
ha-proxy
[apache]
frontend-01
frontend-02
[mysql]
mysql_serv
Run Code Online (Sandbox Code Playgroud)
由于{{inventory_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
Run Code Online (Sandbox Code Playgroud)
您看到的错误是:
AnsibleError:模板化字符串时出现模板错误:意外的“}”。
这通常表明模板中存在语法错误。就你而言,你有一个错字。看一下循环for
:
{% for ip in ansible_all_ipv4_addresses %}
<li> {{ip}} </li>
{% endfor }
Run Code Online (Sandbox Code Playgroud)
你缺少一个%
. 你要:
{% for ip in ansible_all_ipv4_addresses %}
<li> {{ip}} </li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
189 次 |
最近记录: |