Ansible with_dict期望有一个dict [英] Ansible with_dict expects a dict
问题描述
我知道这个问题已经被问过很多次了,但是我一定在这里遗漏了一些东西!
I know this question has been asked many times before but I must be missing something here!
这是重现此问题的最小手册.
This is a minimal playbook to reproduce the issue.
这是剧本:
---
- 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 }}"
以下是输出:
$ 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
我希望task2中打印的item变量包含(例如):
I would expect the item variable printed in task2 to contain (for example):
key: location1
value: {
id: 1
control_prefix: '10.1.254'
data_prefix: '10.1.100'
}
我们缺少什么?
推荐答案
看起来Ansible的文档需要更新,或者您发现了错误. http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashhes 使用您的 with_dict
语法,但似乎不再起作用.字典必须与 with_dict
在同一行.
Looks like Ansible's documentation needs updated or you've found a bug. http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes uses your with_dict
syntax but it seems like that doesn't work anymore. The dictionary needs to be on the same line as with_dict
.
- name: "test2"
debug: var="item"
with_dict: "{{ zones_hash }}"
这篇关于Ansible with_dict期望有一个dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!