Tec*_*eer 7 ansible ansible-2.x
我知道这个问题之前已经被问过很多次了,但我一定在这里遗漏了一些东西!
这是重现问题的最小剧本。
这是剧本:
---
- hosts:
- localhost
gather_facts: false
vars:
zones_hash:
location1:
id: 1
control_prefix: '10.1.254'
data_prefix: '10.1.100'
location2:
id: 2
control_prefix: '10.2.254'
data_prefix: '10.2.100'
tasks:
- name: "test1"
debug: var="zones_hash"
- name: "test2"
debug: var="item"
with_dict:
- "{{ zones_hash }}"
Run Code Online (Sandbox Code Playgroud)
这是输出:
$ ansible --version
ansible 2.3.1.0
config file = /home/configs/_ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
$ ansible-playbook playbook.yml
PLAY [localhost] *******************************************************************************
TASK [test1] ***********************************************************************************
ok: [localhost] => {
"zones_hash": {
"location1": {
"control_prefix": "10.1.254",
"data_prefix": "10.1.100",
"id": 1
},
"location2": {
"control_prefix": "10.2.254",
"data_prefix": "10.2.100",
"id": 2
}
}
}
TASK [test2] ***********************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"}
PLAY RECAP *************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1
Run Code Online (Sandbox Code Playgroud)
我希望 task2 中打印的 item 变量包含(例如):
key: location1
value: {
id: 1
control_prefix: '10.1.254'
data_prefix: '10.1.100'
}
Run Code Online (Sandbox Code Playgroud)
我们缺少什么?
看起来 Ansible 的文档需要更新,或者您发现了一个错误。 http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes使用您的with_dict语法,但似乎不再起作用。字典需要与with_dict.
- name: "test2"
debug: var="item"
with_dict: "{{ zones_hash }}"
Run Code Online (Sandbox Code Playgroud)
with_dict:
- "{{ zones_hash }}"
Run Code Online (Sandbox Code Playgroud)
声明一个以 dict 为第一个索引的列表,Ansible 理所当然地抱怨,因为它需要一个 dict。
kfreezy 提到的解决方案有效,因为它实际上提供了一个字典with_dict而不是一个列表:
with_dict: "{{ zones_hash }}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18970 次 |
| 最近记录: |