【发布时间】:2022-06-19 02:27:25
【问题描述】:
我想在嵌套字典中添加新值,旧值应该被删除。
这是我的 parse.json 文件。
{
"class": "Service_HTTPS",
"layer4": "tcp",
"profileTCP": {
"egress": {
"use": "/Common/Shared/f5-tcp-wan"
},
"ingress": {
"use": "/Common/Shared/f5-tcp-lan"
}
}
}
这是我的脚本,它将添加新值但也会保留旧值,我知道我正在使用 recursive=True 但如果没有此命令,我将丢失入口密钥。
这是我的脚本:
- set_fact:
json_file: "{{ lookup('file', 'parse.json') }}"
- set_fact:
json_file: "{{ json_file | combine( egress_modify, recursive=True) }}"
when: json_file['profileTCP']['egress'] is defined
vars:
egress_modify: "{{ json_file | combine({ 'profileTCP': { 'egress': { 'bigip': '/Common/' + json_file['profileTCP']['egress']['use'].split('/')[-1] } }}) }}"
- name: debug
debug:
msg: "{{ json_file }}"
错误的结果
ok: [localhost] => {
"msg": {
"class": "Service_HTTPS",
"layer4": "tcp",
"profileTCP": {
"egress": {
"bigip": "/Common/f5-tcp-wan",
"use": "/Common/Shared/f5-tcp-wan"
},
"ingress": {
"use": "/Common/Shared/f5-tcp-lan"
}
}
}
}
但我想要这个结果
ok: [localhost] => {
"msg": {
"class": "Service_HTTPS",
"layer4": "tcp",
"profileTCP": {
"egress": {
"bigip": "/Common/f5-tcp-wan",
},
"ingress": {
"use": "/Common/Shared/f5-tcp-lan"
}
}
}
}
【问题讨论】:
标签: ansible