Ansible 循环和更新字典
Ansible Loop and Update Dict
我正在尝试使用 Ansible 循环遍历嵌套字典并添加新的 key:value。我可以使用 combine 添加一个值到顶级字典,但不确定如何更新值字典。我看到循环可用于遍历字典,但如何同时进行更新?
我的词典
{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running'}}
我可以附加到顶级字典,但不确定如何循环和另一个 key:value 到嵌套字典列表。
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'}}
您需要对 combine
过滤器使用 recursive
参数,如下所示:
- 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
我正在尝试使用 Ansible 循环遍历嵌套字典并添加新的 key:value。我可以使用 combine 添加一个值到顶级字典,但不确定如何更新值字典。我看到循环可用于遍历字典,但如何同时进行更新?
我的词典
{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running'},
'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running'}}
我可以附加到顶级字典,但不确定如何循环和另一个 key:value 到嵌套字典列表。
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'}}
您需要对 combine
过滤器使用 recursive
参数,如下所示:
- 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