我有以下json输出(由ansible生成)和用jq
提取几个键/值对看似简单的任务。但是,我似乎无法获得想要的输出。
{
"custom_stats": {},
"plays": [
{
"play": {
"id": "001dd8b7-1cca-07c6-2280-00000000043a",
"name": "Ansible Ad-Hoc"
},
"tasks": [
{
"hosts": {
"hostname01-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname02-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname03-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname04-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname05-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname06-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname07-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname01-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname02-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname03-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname04-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname05-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname01-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname02-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname03-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname04-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname05-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname06-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
},
"hostname07-con": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"hw_system_serial": "<SERIALNUMBER>"
}
},
"task": {
"id": "001dd8b7-1cca-07c6-2280-00000000043c",
"name": "debug"
}
}
]
}
],
"stats": {
"hostname01-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname02-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname03-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname04-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname05-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname06-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname07-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname01-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname02-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname03-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname04-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname05-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname01-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname02-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname03-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname04-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname05-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname06-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
},
"hostname07-con": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
}
}
}
基本上,我只想将最里面的对象的键及其hw_system_serial
的值返回到同一行。像这样:
hostname01-con: <SERIAL_NUMBER_1>
hostname02-con: <SERIAL_NUMBER_2>
etc
我觉得这应该是微不足道的(这次我可能写了一些ruby或python脚本...),但是请尝试一下,因为我可能无法正确理解。我已经尝试过各种方法,但最终还是要遍历每个序列号的主机列表,或者返回两个单独的列表(一个主机和一个序列号)。
例如,此:
.plays[].tasks[].hosts | keys as $hosts | .[].hw_system_serial as $serial | $hosts | join(": ") + $serial
在一行上返回每个主机名的列表,后跟一个序列号。
我认为,如果我可以将$serial
转换为数组,它将起作用,但是我尝试使用split()
的尝试也失败了(例如,$serial | split("\r")
返回一个单项数组而不是项目数组的列表)。
我在这里想念什么?我觉得jq
应该很轻松(再次,任何使用JSON解析器的脚本语言...),但这不是工作的正确工具吗?
答案 0 :(得分:2)
是的,这对于jq
工具是可行的:
jq -r '.plays[].tasks[].hosts | to_entries[] | "\(.key +": "+ .value.hw_system_serial)"' input.json
输出:
hostname01-con: <SERIALNUMBER>
hostname02-con: <SERIALNUMBER>
hostname03-con: <SERIALNUMBER>
hostname04-con: <SERIALNUMBER>
hostname05-con: <SERIALNUMBER>
hostname06-con: <SERIALNUMBER>
hostname07-con: <SERIALNUMBER>