Выпуск на Ansible - PullRequest
       7

Выпуск на Ansible

2 голосов
/ 10 января 2020

Я начал тренировку Ansible.
У меня очень простой вариант использования: я пытаюсь сгенерировать html файлы, используя шаблон jinja.
Найдите здесь мой файл 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>

Инвентаризация хостов проста:

[ha-proxies]
ha-proxy

[apache]
frontend-01
frontend-02

[mysql]
mysql_serv

Кажется, что выполнение не выполнено из-за неверно интерпретированной переменной {{inventory_hostname}} magi c:

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

1 Ответ

5 голосов
/ 10 января 2020

Ошибка, которую вы видите:

AnsibleError: ошибка шаблона при шаблонной строке: неожиданное '}'.

Это обычно указывает на синтаксическую ошибку в вашем шаблоне , В твоем случае у тебя опечатка. Взгляните на for l oop:

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

Вам не хватает %. Вы хотите:

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