gpt4 book ai didi

ubuntu - 如何从 json 输出中使用 ad hoc 和 jq 获取操作系统版本

转载 作者:行者123 更新时间:2023-12-04 19:07:02 25 4
gpt4 key购买 nike

这是一个如此愚蠢的问题,但有人可以举个例子(如果可能的话,有几个链接可以清楚地理解 jq 使用(我有点难以理解官方 jq 文档))如何从行中获取操作系统名称 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

关于ubuntu - 如何从 json 输出中使用 ad hoc 和 jq 获取操作系统版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66680372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com