这是我更新/ etc / hosts文件的Ansible playbook:
- 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
这是jinja模板:
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']中的项目),一切正常,但我想使用一个变量,通过dinamically传递给playbook .
我得到的错误是:
AnsibleUndefinedVariable: 'dict object' has no attribute '\"{{ target_hosts }}\"'"
使用调试消息我确定该playbook获取参数:
> "msg": "show me the variable: my-server-group".
也许j2模板没有?
语法错了吗?我试着用引号,双引号和两个组合 .
1 回答
是 . 不要嵌套Jinja2表达式 . 在你的情况
{{
里面{%
.正确的语法: