复杂JSON和Ansible JSON_查询语法

复杂JSON和Ansible JSON_查询语法,ansible,jsonpath,json-query,Ansible,Jsonpath,Json Query,我在JSON格式中得到了复杂的结果,并且很难从中提取出值。 我测试了JSONpath在线评估器,在这里我只需键入 $.[*].toPort 并获取请求变量的列表 我尝试在Ansible中使用相同的语法,但得到的是空列表 - name: JSON test hosts: localhost gather_facts: no vars: jsoncontent: { "infraAccP

我在JSON格式中得到了复杂的结果,并且很难从中提取出值。 我测试了JSONpath在线评估器,在这里我只需键入

$.[*].toPort
并获取请求变量的列表

我尝试在Ansible中使用相同的语法,但得到的是空列表

- name: JSON test
  hosts: localhost
  gather_facts: no

  vars:
    jsoncontent:
                {
                        "infraAccPortP": {
                            "attributes": {
                                "annotation": "",
                                "childAction": "",
                                "descr": "",
                                "nameAlias": "",
                                "ownerKey": "",
                                "ownerTag": "",
                                "status": "",
                                "uid": "15374"
                            },
                            "children": [
                                {
                                    "infraHPortS": {
                                        "attributes": {
                                            "annotation": "",
                                            "uid": "8927"
                                        },
                                        "children": [
                                            {
                                                "infraPortBlk": {
                                                    "attributes": {                                                  
                                                        "fromPort": "41",
                                                        "toPort": "41",
                                                        "uid": "8927"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                },
                                {
                                    "infraHPortS": {
                                        "attributes": {
                                            "annotation": "",
                                            "uid": "8927"
                                        },
                                        "children": [
                                            {
                                                "infraPortBlk": {
                                                    "attributes": {
                                                        "fromPort": "42",
                                                        "toPort": "42",
                                                        "uid": "8927"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }

  tasks:
    - name: show jsoncontent
      debug:
        var: jsoncontent

    - name: Show just toPort values
      debug: 
        msg: "{{ jsoncontent | json_query(jmesquery)    }}"
      vars:
        jmesquery: "[*].toPort"

如何调整查询以获得预期结果有何帮助?这有点令人沮丧,因为没有太多关于如何在Ansible中实现json_查询的文档

-name:仅显示toPort值
调试:
msg:{jsoncontent.infraAccPortP.children | json_查询(jmesquery)}”
变量:
jmesquery:“[].infraHPortS.children[].infraPortBlk.attributes.toPort”
给予

msg:
- '41'
- '42'

问:“Ansible实现不允许通配符搜索?”

A:房间里似乎没有什么特别的东西。(此筛选器从2.10起移动到。)


问:“我不明白为什么我在那个在线检查器中用更短的语法得到了相同的结果。”

答:我认为问题是如何实现检查器
jq
也不能以这种方式工作。比如说

shell>cat data.json | jq.jsoncontent.infraacportp.children[].infraHPortS.children[].infraPortBlk.attributes.toPort
"41"
"42"
“checker”查询崩溃

shell>cat data.json | jq.[*].toPort
jq:error:syntax error,第1行出现意外的“*”(Unix shell引用问题?)
[*].托波特
jq:1编译错误
,或给出一个空结果

shell>cat data.json | jq.[].toPort
无效的

这是否意味着Ansible实现不允许通配符搜索?我只是不明白为什么我在那个在线检查器中用更短的语法得到了相同的结果