如何使用 json_query render/parse ansible 输出,获取空值

How to render/parse ansible output using json_query, getting null value

我有下面output3.msg,如何得到下面提到的想要的输出?请在语法方面提供帮助,检查我的语法并提出任何建议 modifications.I 正在学习阶段,如有任何错误,我深表歉意。谢谢。

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

期望的输出


    {
            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
                    
    }

我的调试语法


       - debug: msg="{{output3.msg|json_query(jmesquery)}}"     
         vars:
             jmesquery: "[*].{Name: name, local: proxy_id.entry.local,remote: proxy_id.entry.remote,tunnel:tunnel_interface}" 

 

我得到的输出


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

您看到 null 值的原因是因为 proxy_id.entry 是一个数组,它的正下方没有 local/remote 键。正确的语法应该是 proxy_id.entry[].local.

json_query 中使用 auto_key.proxy_id.entry[] 语法产生了所需的输出。下面是任务:

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

请注意,我已使用 join() 函数获取 localremote 的逗号分隔字符串。下面是输出:

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