I have below output3.msg, how to get the desired output mentioned below? Please help in the syntax, check the one i have and suggest any modifications.I am in learning phase apologies for any mistakes Thank you.
output3.msg
{
"msg": [
{
"auto_key": {
"proxy_id": {
"entry": [
{
"local": "1.1.1.0/24",
"name": "PROXY1",
"protocol": {
"any": null
},
"remote": "192.168.0.0/24"
},
{
"local": "1.1.2.0/24",
"name": "PROXY2",
"protocol": {
"any": null
},
"remote": "192.168.1.0/24"
},
{
"local": "1.1.3.0/24",
"name": "PROXY3",
"protocol": {
"any": null
},
"remote": "192.168.2.0/24"
}
]
}
},
"copy_tos": "yes",
"name": "customer-a",
"tunnel_interface": "tunnel.5",
"tunnel_monitor": {
"enable": "no"
}
},
{
"auto_key": {
"proxy_id": {
"entry": [
{
"local": "2.2.1.0/24",
"name": "PROXY1",
"protocol": {
"any": null
},
"remote": "192.168.3.0/24"
}
]
}
},
"copy_tos": "yes",
"name": "customer-b",
"tunnel_interface": "tunnel.10",
"tunnel_monitor": {
"enable": "no"
}
},
{
"auto_key": {
"proxy_id": {
"entry": [
{
"local": "3.3.1.0/24",
"name": "PROXY1",
"protocol": {
"any": null
},
"remote": "192.168.4.0/24"
},
{
"local": "3.3.2.0/24",
"name": "PROXY2",
"protocol": {
"any": null
},
"remote": "192.168.5.0/24"
},
{
"local": "3.3.3.0/24",
"name": "PROXY3",
"protocol": {
"any": null
},
"remote": "192.168.6.0/24"
},
{
"local": "3.3.4.0/24",
"name": "PROXY2",
"protocol": {
"any": null
},
"remote": "192.168.7.0/24"
},
{
"local": "3.3.5.0/24",
"name": "PROXY3",
"protocol": {
"any": null
},
"remote": "192.168.8.0/24"
}
]
}
},
"copy_tos": "yes",
"name": "customer-c",
"tunnel_interface": "tunnel.15",
"tunnel_monitor": {
"enable": "no"
}
}
]
}
desired output
{
name:customer-a,
local:1.1.1.0/24,1.1.2.0/24,1.1.3.0/24,
remote:192.168.0.0/24,192.168.1.0/24,192.168.2.0/24,
tunnel: tunnel.5
},
{
name:customer-b,
local:2.2.1.0/24,
remote:192.168.3.0/24,
tunnel: tunnel.10
},
{
name:customer-c,
local:3.3.1.0/24,3.3.2.0/24,3.3.3.0/24,3.3.4.0/24,3.3.5.0/24
remote:192.168.4.0/24,192.168.5.0/24,192.168.6.0/24,192.168.7.0/24,192.168.8.0/24,
tunnel_interface: tunnel.15
}
my debug syntax
- debug: msg="{{output3.msg|json_query(jmesquery)}}"
vars:
jmesquery: "[*].{Name: name, local: proxy_id.entry.local,remote: proxy_id.entry.remote,tunnel:tunnel_interface}"
output i'm getting
"msg": [
{
"Name": "customer-a",
"local": null,
"remote": null,
"tunnel": "tunnel.5"
},
{
"Name": "customer-b",
"local": null,
"remote": null,
"tunnel": "tunnel.10"
},
{
"Name": "customer-c",
"local": null,
"remote": null,
"tunnel": "tunnel.15"
}]
CodePudding user response:
The reason you are seeing null values is because proxy_id.entry is an array and it doesn't have local/remote keys directly under it. The correct syntax would be something like proxy_id.entry[].local.
Using auto_key.proxy_id.entry[] syntax in json_query produced the desired output. Below is the task:
- name: show jsondata
debug:
var: output3.msg | json_query(jquery)
vars:
jquery: "[].{Name: name, local: auto_key.proxy_id.entry[].local | join(',', @), remote: auto_key.proxy_id.entry[].remote | join(',', @), tunnel: tunnel_interface}"
Note that I have used join() function to get comma separated string for local and remote. Below is the output:
ok: [localhost] => {
"output3.msg | json_query(jquery)": [
{
"Name": "customer-a",
"local": "1.1.1.0/24,1.1.2.0/24,1.1.3.0/24",
"remote": "192.168.0.0/24,192.168.1.0/24,192.168.2.0/24",
"tunnel": "tunnel.5"
},
{
"Name": "customer-b",
"local": "2.2.1.0/24",
"remote": "192.168.3.0/24",
"tunnel": "tunnel.10"
},
{
"Name": "customer-c",
"local": "3.3.1.0/24,3.3.2.0/24,3.3.3.0/24,3.3.4.0/24,3.3.5.0/24",
"remote": "192.168.4.0/24,192.168.5.0/24,192.168.6.0/24,192.168.7.0/24,192.168.8.0/24",
"tunnel": "tunnel.15"
}
]
}
