python - Ansible 循环和更新字典
问题描述
我正在尝试使用 Ansible 循环遍历嵌套的字典并添加一个新的键:值。我可以使用 combine 将值添加到顶级字典,但不确定如何更新值字典。我看到可以使用循环来遍历 dict 但如何同时进行更新?
我的字典
{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running'}}
我能够附加到顶级字典,但不知道如何循环和另一个键:值到嵌套字典列表。
tasks:
- name: Iterate and update dict
set_fact:
my_dict: '{{my_dict|combine({"location": "building-a"})}}'
- debug: var=my_dict
更新后所需的字典:
{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'},
'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'},
'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'}}
解决方案
您需要使用过滤器的recursive
参数combine
,如下所示:
- hosts: localhost
gather_facts: false
vars:
my_dict:
host-a: {'os': 'Linux', 'port': '22', 'status': 'Running'}
host-b: {'os': 'Linux', 'port': '22', 'status': 'Running'}
host-c: {'os': 'Linux', 'port': '22', 'status': 'Running'}
tasks:
- name: update dict
set_fact:
my_dict: "{{ my_dict|combine({item: {'location': 'building-a'}}, recursive=true) }}"
loop: "{{ my_dict|list }}"
- debug:
var: my_dict
上面的剧本将输出:
PLAY [localhost] *************************************************************************************************************************************************************
TASK [update dict] ***********************************************************************************************************************************************************
ok: [localhost] => (item=host-a)
ok: [localhost] => (item=host-b)
ok: [localhost] => (item=host-c)
TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
"my_dict": {
"host-a": {
"location": "building-a",
"os": "Linux",
"port": "22",
"status": "Running"
},
"host-b": {
"location": "building-a",
"os": "Linux",
"port": "22",
"status": "Running"
},
"host-c": {
"location": "building-a",
"os": "Linux",
"port": "22",
"status": "Running"
}
}
}
PLAY RECAP *******************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
推荐阅读
- pandas - 如何在熊猫数据框中提取组的最后一个值
- python - 使用 aiohttp 的多个请求,但每个请求单独超时
- ruby-on-rails - 我如何使用 rspec 运行测试,使用 simple_token_authentication 并设计
- http - 为什么以下代码中的最后一个错误处理程序存在无效参数错误?
- c - 除了字母值之外,错误字符串变量正在接收 0x603270
- javascript - 如何在 React Hook 中添加自定义类名?
- regex - 为什么有grep的时候find命令有-regex?
- android - 在拦截器中使用改造刷新令牌后重新执行 Web 服务请求
- javascript - 检查 Array 中的数组元素是否包含 false 的方法
- simulation - 时间步长的变化不会影响 Dymola 中的模拟结果