【问题标题】:Ansible json_query filterAnsible json_query 过滤器
【发布时间】:2020-02-05 10:56:29
【问题描述】:

所以我有这个输出来自 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",

每个主机。

ansible 任务是这样的:

   - 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】:

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

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

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

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

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

    参考:

    【讨论】:

    • 任何想法如何过滤 stdout_lines 输出以仅显示 64 bytes from 10.240.18.20: icmp_req=1 ttl=64 time=0.268 ms 并忽略所有其他行?
    • 提示。 stdout_lines 是一个列表。您要查找的元素始终是第二个元素。列表索引从 0 开始。
    • 显然这在 ansible 2.9.x 中不再起作用。有什么线索吗?
    • “不工作”不是对您的问题的准确描述。在这种情况下,ansible 版本可能不是问题。对于它的价值,我每天使用 json_query 和 ansible 2.9.x 扮演角色,没有任何问题。如果这是一个全新的 ansible 安装,您是 pip install jmespath 吗?
    • 不,我没有 :)) 这就是问题所在,输出没有被完全过滤,谢谢!那个 pip install 成功了!
    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2020-11-25
    相关资源
    最近更新 更多