首页 > 解决方案 > Ansible JSON解析空字符串

问题描述

我正在尝试在 Ansible 中使用 jmesquery 学习 JSON 解析。

请考虑以下玩法:

---
- name: GET ALL THE INTERFACES
  junos_command:
    commands: show configuration interfaces | display json
  register: A


- name: DISPLAY VARIABLE A CONTENTS
  debug:
    var: A.stdout_lines



- name: JOSN QUERY TO STORE PORTS IN NEW VARIABLE ALL_PORTS
  set_fact:
      ALL_PORTS: "{{ A.stdout_lines | json_query(jmesquery) }}"
  vars:
    jmesquery: 'configuration.interfaces.interface[*].name'


- name: DISPLAY VARIABLE ALL_PORTS CONTENTS
  debug:
   var: ALL_PORTS
#

根据 JSON 查询,端口 ge-0/0/0 将存储在 ALL_PORT 中,但我们没有看到当我们运行 playbook 时,在 ALL_PORT 上进行调试显示它是空的。

PLAY [ROUTER-STIG-PLAYBOOK] ************************************************************************************************

TASK [STIG_ROUTER : GET ALL THE INTERFACES] ********************************************************************************
ok: [192.168.22.9]

TASK [STIG_ROUTER : DISPLAY VARIABLE A CONTENTS] ***************************************************************************
ok: [192.168.22.9] => {
    "A.stdout_lines": [
        {
            "configuration": {
                "@": {
                    "junos:changed-localtime": "2020-05-10 11:14:49 UTC",
                    "junos:changed-seconds": "1589109289",
                    "xmlns": "http://xml.juniper.net/xnm/1.1/xnm"
                },
                "interfaces": {
                    "interface": [
                        {
                            "name": "ge-0/0/0",
                            "unit": [
                                {
                                    "family": {
                                        "inet": {
                                            "address": [
                                                {
                                                    "name": "192.168.22.9/24"
                                                }
                                            ]
                                        }
                                    },
                                    "name": 0
                                }
                            ]
                        }
                    ]
                },
                "security": {
                    "policies": {
                        "global": {
                            "policy": [
                                {
                                    "match": {
                                        "application": [
                                            "any"
                                        ],
                                        "destination-address": [
                                            "any"
                                        ],
                                        "source-address": [
                                            "any"
                                        ]
                                    },
                                    "name": "TEST",
                                    "then": {
                                        "permit": [
                                            null
                                        ]
                                    }
                                }
                            ]
                        }
                    },
                    "screen": {
                        "ids-option": [
                            {
                                "icmp": {
                                    "ping-death": [
                                        null
                                    ]
                                },
                                "ip": {
                                    "source-route-option": [
                                        null
                                    ],
                                    "tear-drop": [
                                        null
                                    ]
                                },
                                "name": "untrust-screen",
                                "tcp": {
                                    "land": [
                                        null
                                    ],
                                    "syn-flood": {
                                        "alarm-threshold": 1024,
                                        "attack-threshold": 200,
                                        "destination-threshold": 2048,
                                        "queue-size": 2000,
                                        "source-threshold": 1024,
                                        "timeout": 20
                                    }
                                }
                            }
                        ]
                    },
                    "zones": {
                        "security-zone": [
                            {
                                "name": "trust",
                                "tcp-rst": [
                                    null
                                ]
                            },
                            {
                                "name": "untrust",
                                "screen": "untrust-screen"
                            },
                            {
                                "host-inbound-traffic": {
                                    "protocols": [
                                        {
                                            "name": "all"
                                        }
                                    ],
                                    "system-services": [
                                        {
                                            "name": "all"
                                        }
                                    ]
                                },
                                "interfaces": [
                                    {
                                        "name": "ge-0/0/0.0"
                                    }
                                ],
                                "name": "A"
                            }
                        ]
                    }
                },
                "system": {
                    "license": {
                        "autoupdate": {
                            "url": [
                                {
                                    "name": "https://ae1.juniper.net/junos/key_retrieval"
                                }
                            ]
                        }
                    },
                    "root-authentication": {
                        "encrypted-password": "$6$thcHCjAV$e3o5ZRNWv7WtysOxuKpBP2X0cA3QDNtWYyCSBAUkImSEsulEGTgfwEQBa12Wll0fegpwvZfTHLvCbDUIW1n211"
                    },
                    "services": {
                        "ftp": [
                            null
                        ],
                        "netconf": {
                            "ssh": [
                                null
                            ]
                        },
                        "ssh": {
                            "root-login": "allow"
                        }
                    },
                    "syslog": {
                        "file": [
                            {
                                "contents": [
                                    {
                                        "any": [
                                            null
                                        ],
                                        "name": "any"
                                    },
                                    {
                                        "info": [
                                            null
                                        ],
                                        "name": "authorization"
                                    }
                                ],
                                "name": "messages"
                            },
                            {
                                "contents": [
                                    {
                                        "any": [
                                            null
                                        ],
                                        "name": "interactive-commands"
                                    }
                                ],
                                "name": "interactive-commands"
                            }
                        ],
                        "user": [
                            {
                                "contents": [
                                    {
                                        "emergency": [
                                            null
                                        ],
                                        "name": "any"
                                    }
                                ],
                                "name": "*"
                            }
                        ]
                    }
                },
                "version": "20191026.124700_builder.r1063854"
            }
        }
    ]
}

TASK [STIG_ROUTER : JOSN QUERY TO STORE PORTS IN NEW VARIABLE ALL_PORTS] ***************************************************
ok: [192.168.22.9]

TASK [STIG_ROUTER : DISPLAY VARIABLE ALL_PORTS CONTENTS] *******************************************************************
ok: [192.168.22.9] => {
    "ALL_PORTS": ""
}
#

我使用了在线 JSON 查询测试器。 https://jsonpath.com/ 当我针对 A.stdout_lines 检查我的查询“configuration.interfaces.interface[*].name”时,我发现了预期的响应,即 [ge-0/0/0]

任何反馈/指导表示赞赏!有一个美好的周末!!

标签: ansible

解决方案


您的 JMESPath 很可能对一个对象是正确的,但顾名思义,正如您所debug: var=A.stdout_lines展示的,stdout_lines它是一个列表

因此,您可以做以下几件事之一:

  • 认识到您stdout_lines只包含一个对象并将输入json_query{{ A.stdout_lines[0] | json_query(jmesquery) }}
  • 用于the map filter将相同的过滤器应用于每个列表项,例如{{ A.stdout_lines | map("json_query", jmesquery) | list }}
  • 重写您的 JMESPath 以将该过滤器应用于输入列表,类似于json_query("[*].configuration.AND THE REST HERE")

最后两个自然会产生不同的输出形状,因为它们是列表的列表,所以它看起来像[['ge-0/0/0']]输出时,但它们确实有一个优势,如果你stdout_lines曾经神秘地开始包含更多对象,它们将是 json_query-正如预期的那样


推荐阅读