如何使用 ad hoc 和 jq 从 json 输出中获取 OS 版本

How to get OS version using ad hoc and jq from json output

这是个愚蠢的问题,但有人可以举个例子吗(如果可能的话,几个链接可以清楚地理解 jq 使用(我有点难以理解官方 jq 文档))如何获得OS 名称 Debian 来自行 "ansible_distribution_file_variety": "Debian",来自命令的结果:

ansible all -m setup -a 'filter=ansible_distribution*' -u vagrant --ask-pass

命令的输出是:

{
    "custom_stats": {},
    "global_custom_stats": {},
    "plays": [
        {
            "play": {
                "duration": {
                    "end": "2021-03-17T19:44:59.156229Z",
                    "start": "2021-03-17T19:44:55.293502Z"
                },
                "id": "080027fa-0b32-fdef-8eef-000000000007",
                "name": "Ansible Ad-Hoc"
            },
            "tasks": [
                {
                    "hosts": {
                        "localhost": {
                            "_ansible_no_log": false,
                            "_ansible_verbose_override": true,
                            "action": "setup",
                            "ansible_facts": {
                                "ansible_distribution": "Ubuntu",
                                "ansible_distribution_file_parsed": true,
                                "ansible_distribution_file_path": "/etc/os-release",
                                "ansible_distribution_file_variety": "Debian",
                                "ansible_distribution_major_version": "16",
                                "ansible_distribution_release": "xenial",
                                "ansible_distribution_version": "16.04",
                                "discovered_interpreter_python": "/usr/bin/python"
                            },
                            "changed": false,
                            "deprecations": [
                                {
                                    "msg": "Distribution Ubuntu 16.04 on host localhost should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information",
                                    "version": "2.12"
                                }
                            ],
                            "invocation": {
                                "module_args": {
                                    "fact_path": "/etc/ansible/facts.d",
                                    "filter": "ansible_distribution*",
                                    "gather_subset": [
                                        "all"
                                    ],
                                    "gather_timeout": 10
                                }
                            }
                        }
                    },
                    "task": {
                        "duration": {
                            "end": "2021-03-17T19:44:59.156229Z",
                            "start": "2021-03-17T19:44:55.361244Z"
                        },
                        "id": "080027fa-0b32-fdef-8eef-000000000009",
                        "name": "setup"
                    }
                }
            ]
        }
    ],
    "stats": {
        "localhost": {
            "changed": 0,
            "failures": 0,
            "ignored": 0,
            "ok": 1,
            "rescued": 0,
            "skipped": 0,
            "unreachable": 0
        }
    }
}

为避免必须导航到相关对象,您可以让 jq 进行行走:

jq '.. | objects | .ansible_distribution_file_variety // empty' data.json