Ansible json_query过滤器

问题描述 投票:1回答:1

所以我得到此输出是由于ansible(使用shell模块对一些主机执行ping操作:

TASK [debug] ***************************************************************************************************************************************************************************************
skipping: [10.240.18.58]
ok: [10.240.18.57] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": "ping -c1 -W1 \"10.240.18.20\"",
                "delta": "0:00:00.005339",
                "end": "2020-02-05 10:41:06.527708",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "ping -c1 -W1 \"10.240.18.20\"",
                        "_uses_shell": true,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.240.18.20",
                "rc": 0,
                "start": "2020-02-05 10:41:06.522369",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "PING 10.240.18.20 (10.240.18.20) 56(84) bytes of data.\n64 bytes from 10.240.18.20: icmp_req=1 ttl=64 time=0.268 ms\n\n--- 10.240.18.20 ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 0.268/0.268/0.268/0.000 ms",
                "stdout_lines": [
                    "PING 10.240.18.20 (10.240.18.20) 56(84) bytes of data.",
                    "64 bytes from 10.240.18.20: icmp_req=1 ttl=64 time=0.268 ms",
                    "",
                    "--- 10.240.18.20 ping statistics ---",
                    "1 packets transmitted, 1 received, 0% packet loss, time 0ms",
                    "rtt min/avg/max/mdev = 0.268/0.268/0.268/0.000 ms"
                ]
            },
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": "ping -c1 -W1 \"10.240.23.34\"",
                "delta": "0:00:00.006229",
                "end": "2020-02-05 10:41:09.889206",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "ping -c1 -W1 \"10.240.23.34\"",
                        "_uses_shell": true,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.240.23.34",
                "rc": 0,
                "start": "2020-02-05 10:41:09.882977",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "PING 10.240.23.34 (10.240.23.34) 56(84) bytes of data.\n64 bytes from 10.240.23.34: icmp_req=1 ttl=63 time=1.33 ms\n\n--- 10.240.23.34 ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 1.333/1.333/1.333/0.000 ms",
                "stdout_lines": [
                    "PING 10.240.23.34 (10.240.23.34) 56(84) bytes of data.",
                    "64 bytes from 10.240.23.34: icmp_req=1 ttl=63 time=1.33 ms",
                    "",
                    "--- 10.240.23.34 ping statistics ---",
                    "1 packets transmitted, 1 received, 0% packet loss, time 0ms",
                    "rtt min/avg/max/mdev = 1.333/1.333/1.333/0.000 ms"
                ]
            },
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": "ping -c1 -W1 \"10.240.23.35\"",
                "delta": "0:00:00.006403",
                "end": "2020-02-05 10:41:13.237920",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "ping -c1 -W1 \"10.240.23.35\"",
                        "_uses_shell": true,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.240.23.35",
                "rc": 0,
                "start": "2020-02-05 10:41:13.231517",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "PING 10.240.23.35 (10.240.23.35) 56(84) bytes of data.\n64 bytes from 10.240.23.35: icmp_req=1 ttl=63 time=1.36 ms\n\n--- 10.240.23.35 ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 1.364/1.364/1.364/0.000 ms",
                "stdout_lines": [
                    "PING 10.240.23.35 (10.240.23.35) 56(84) bytes of data.",
                    "64 bytes from 10.240.23.35: icmp_req=1 ttl=63 time=1.36 ms",
                    "",
                    "--- 10.240.23.35 ping statistics ---",
                    "1 packets transmitted, 1 received, 0% packet loss, time 0ms",
                    "rtt min/avg/max/mdev = 1.364/1.364/1.364/0.000 ms"
                ]
            }
        ]
    }
}

我创建了一个ansible任务,我想严格使用debug行显示这些行:

 "stdout_lines": [
                    "PING 10.240.18.20 (10.240.18.20) 56(84) bytes of data.",
                    "64 bytes from 10.240.18.20: icmp_req=1 ttl=64 time=0.268 ms",

对于每个主机。

烦人的任务是这样:

   - debug:
       msg: "{{ smm_ping.json | json_query(pfilter) }}"
     vars:
       pfilter: "[*].{Ping response: stdout_lines}"
     when: "('primary' in default_hostname or 'Primary' in default_hostname)"
     tags: ['ic', 'smm']

由于某种原因,它不起作用:

TASK [debug]
fatal: [10.240.18.57]: FAILED! => {"msg": "JMESPathError in json_query filter plugin:\nExpecting: colon, got: unquoted_identifier: Parse error at column 10, token \"response\" (UNQUOTED_IDENTIFIER), for expression:\n\"[*].{Ping response: stdout_lines}\"\n           ^"}
ansible ansible-2.x ansible-facts
1个回答
1
投票

由于您的标识符中有空格,因此需要引用它。

pfilter: "[*].{\"Ping response\": stdout_lines}"

为了简化书写/可读性,您可以使用yaml折叠块标量并删除一级引号转义:

pfilter: >-
  [*].{"Ping response": stdout_lines}

注意:标识符的引号是双引号(")。 不要使用单引号用于字符串值。请参阅下面的规格。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.