如何在ansible中的json中使用相同的键合并值
How to merge values with the same key in a json in ansible
我想根据特定键值将值从 1 json 合并到另一个值。
请找到下面的代码,它在合并时只给我一个空数组。主要问题是 gp_value var 有一个空数组。
我想将 gpfinalgpid 从 gpidmgmtfinal.json 合并到 gp-test.json匹配 gpfinalnet 和 networkId 值。
我已经尝试在 gp_value 中添加 First,但是会抛出一个错误提示 No first item, sequence was empty
tasks:
- name: Combine GP
vars:
gplist: "{{ lookup('file', 'gpidmgmtfinal.json') | from_json }}"
gpconf: "{{ lookup('file', 'gp-test.json') | from_json }}"
gp: >-
{{
gplist
| json_query('results[*].ansible_facts[]')
}}
gp_value: >-
{{
gp
| selectattr('gpfinalnet', '==', item.networkId)
| map(attribute='gpfinalgpid')
| list
| default(None)
}}
set_fact:
value: "{{ gp_value }}"
result: >-
{{
result | default([])
+
[item | combine({'gpid': gp_value})]
}}
loop: "{{ gpconf }}"
gplist JSON:
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"gpfinalgpid": "101",
"gpfinalnet": "L_456789 "
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": [
{
"gpmgmtcurrent": {
"L_456789": [
"102"
]
}
},
{
"L_456789": [
"101"
]
}
]
},
{
"ansible_facts": {
"gpfinalgpid": "103",
"gpfinalnet": "N_11447788 "
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": [
{
"gpmgmtcurrent": {
"N_11447788": [
"101"
]
}
},
{
"N_11447788": [
"103"
]
}
]
}
],
"skipped": false
}
gpconf JSON:
[
{
"name": "MGMT",
"applianceIp": "2.2.2.2",
"groupPolicyId": "100",
"networkId": "L_456789",
"subnet": "2.2.2.1/28"
},
{
"name": "MGMT",
"applianceIp": "1.1.1.2",
"groupPolicyId": "101",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
} ]
当前输出:带有键 gpid 但带有值而不是空数组的新行。
{
"applianceIp": "1.1.1.2",
"gpid": [],
"groupPolicyId": "101",
"name": "MGMT",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
}
预期输出:
{
"applianceIp": "1.1.1.2",
"gpid": 103,
"groupPolicyId": "101",
"name": "MGMT",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
}
这是我的一个基本错误。我在 gpfinalnet 的 gplist json 中有一个额外的 space。删除多余的 space 后一切正常。
有额外的space
"gpfinalnet": "N_11447788 "
没有额外的space:
"gpfinalnet": "N_11447788"
我想根据特定键值将值从 1 json 合并到另一个值。 请找到下面的代码,它在合并时只给我一个空数组。主要问题是 gp_value var 有一个空数组。 我想将 gpfinalgpid 从 gpidmgmtfinal.json 合并到 gp-test.json匹配 gpfinalnet 和 networkId 值。
我已经尝试在 gp_value 中添加 First,但是会抛出一个错误提示 No first item, sequence was empty
tasks:
- name: Combine GP
vars:
gplist: "{{ lookup('file', 'gpidmgmtfinal.json') | from_json }}"
gpconf: "{{ lookup('file', 'gp-test.json') | from_json }}"
gp: >-
{{
gplist
| json_query('results[*].ansible_facts[]')
}}
gp_value: >-
{{
gp
| selectattr('gpfinalnet', '==', item.networkId)
| map(attribute='gpfinalgpid')
| list
| default(None)
}}
set_fact:
value: "{{ gp_value }}"
result: >-
{{
result | default([])
+
[item | combine({'gpid': gp_value})]
}}
loop: "{{ gpconf }}"
gplist JSON:
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"gpfinalgpid": "101",
"gpfinalnet": "L_456789 "
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": [
{
"gpmgmtcurrent": {
"L_456789": [
"102"
]
}
},
{
"L_456789": [
"101"
]
}
]
},
{
"ansible_facts": {
"gpfinalgpid": "103",
"gpfinalnet": "N_11447788 "
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": [
{
"gpmgmtcurrent": {
"N_11447788": [
"101"
]
}
},
{
"N_11447788": [
"103"
]
}
]
}
],
"skipped": false
}
gpconf JSON:
[
{
"name": "MGMT",
"applianceIp": "2.2.2.2",
"groupPolicyId": "100",
"networkId": "L_456789",
"subnet": "2.2.2.1/28"
},
{
"name": "MGMT",
"applianceIp": "1.1.1.2",
"groupPolicyId": "101",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
} ]
当前输出:带有键 gpid 但带有值而不是空数组的新行。
{
"applianceIp": "1.1.1.2",
"gpid": [],
"groupPolicyId": "101",
"name": "MGMT",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
}
预期输出:
{
"applianceIp": "1.1.1.2",
"gpid": 103,
"groupPolicyId": "101",
"name": "MGMT",
"networkId": "N_11447788",
"subnet": "1.1.1.1/28"
}
这是我的一个基本错误。我在 gpfinalnet 的 gplist json 中有一个额外的 space。删除多余的 space 后一切正常。
有额外的space
"gpfinalnet": "N_11447788 "
没有额外的space:
"gpfinalnet": "N_11447788"