1

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"
    }]

netv
  • 165
  • 2
  • 12

1 Answers1

1

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"
        }
    ]
}
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • I modified your JSON structure to make it valid (answer updated), and then it works fine. If this not matching what you have, then please correct the JSON data posted in your question. – seshadri_c Nov 19 '21 at 06:40
  • you are right i made a typo while copy pasting, `proxy_id` is under `"auto_key": {` i have edited the question, does now `local: proxy_id.entry[].local | join(',', @)` becomes `local: auto_key.proxy_id.entry[].local | join(',', @)` ??? – netv Nov 19 '21 at 06:42
  • Yes, that's correct. you need to use `auto_key.proxy_id.entry[].local`. I updated the answer. – seshadri_c Nov 19 '21 at 06:48
  • now getting below error : `FAILED! => {"msg": "JMESPathError in json_query filter plugin:\nIn function join(), invalid type for value: 1.1.1.0/24, expected one of: ['array-string'], received: \"AnsibleUnsafeText\""}` – netv Nov 19 '21 at 06:51
  • 1
    worked now, jsut did some modifications in output but syntax is right as you suggested, thanks for your help. – netv Nov 19 '21 at 08:02