【问题标题】:jinja2 - AnsibleUndefinedVariable: 'dict object' has no attribute '"{{ target_hosts }}"'"jinja2 - AnsibleUndefinedVariable: 'dict object' 没有属性 '"{{ target_hosts }}"'"
【发布时间】:2018-10-28 22:28:30
【问题描述】:

这是我更新 /etc/hosts 文件的 Ansible 剧本:

- name: Update /etc/hosts file
  hosts: "{{ target_hosts }}"
  remote_user: awx
  become: yes
  become_method: sudo
  tasks:

    - debug:
        msg: 'show me the variable: {{ target_hosts }}'

    - name: Update /etc/hosts file
      template: src=../../templates/hosts.j2 dest=/etc/hosts

这是神社模板:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

{% for item in groups['"{{ target_hosts }}"'] %}
{{ hostvars[item]['ansible_ssh_host'] }} {{ hostvars[item]['openstack']['name'] }}
{% endfor %}

如果我在模板中放置一个静态值(例如对于组中的项目['my-server-group']),一切正常,但我想使用一个变量,通过动态地到剧本。

我得到的错误是:

AnsibleUndefinedVariable: 'dict object' has no attribute '\"{{ target_hosts }}\"'"

通过调试消息,我确定剧本获得了参数:

> "msg": "show me the variable: my-server-group". 

也许 j2 模板没有?
语法错了吗?我用引号、双引号和两者的组合都试过了。

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    语法错了吗?

    是的。不要嵌套 Jinja2 表达式。在您的情况下,{{{% 内。

    正确的语法:

    {% for item in groups[target_hosts] %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多