ubuntuansiblejqadhoc

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


It's such a silly question, but could someone give an example (and if it possible, a couple of links to clearly understand jq using (it's a little bit hard for me to understand official jq documentation) ) how to get OS name Debian from line "ansible_distribution_file_variety": "Debian", from the result of the command:

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

the output of the command is:

{
    "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
        }
    }
}

Solution

  • To avoid having to navigate to the relevant object, you could let jq do the walking:

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