gpt4 book ai didi

Ansible with_dict 需要一个 dict

转载 作者:行者123 更新时间:2023-12-03 13:46:11 25 4
gpt4 key购买 nike

我知道这个问题之前已经被问过很多次了,但我一定在这里遗漏了一些东西!

这是重现问题的最小剧本。

这是剧本:

---
- 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 变量包含(例如):
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-hashes使用您的 with_dict语法,但似乎不再起作用了。字典需要与 with_dict 在同一行.

- name: "test2"
debug: var="item"
with_dict: "{{ zones_hash }}"

关于Ansible with_dict 需要一个 dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45377692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com