Is anyone good with ./JQ?
I’m trying to pull out data from the setup output from Ansible. I can do most of what I need to but I’m stuck on something that is probably quite simple.
I’m trying to generate a list of hosts including disks + sizes and partitions + sizes AND mount points.
I can get the first two quite easily like this (this should work on any server running on localhost) - I need the sed to format the json correctly:
$ ansible localhost -m setup | sed ‘1 s/^.*$/{/’ | jq ‘.ansible_facts | {hostname: .ansible_hostname, Disks: .ansible_devices| with_entries(.value |= .size), Partitions: .ansible_devices.partitions | with_entries(.value |= .size) }’
{
“hostname”: “ip-172-31-16-55”,
“Disks”: {
“xvda”: “10.00 GB”
},
“Partitions”: {
“xvda1”: “1.00 GB”,
“xvda2”: “9.00 GB”
}
}
(formatting is a little off with copy and paste)
Now if I want to collect mount points I can do it like this:
$ ansible localhost -m setup | sed ‘1 s/^.*$/{/’ | jq -r ‘.ansible_facts.ansible_mounts.mount’
/
/boot
But because the mount information is an array (I think?) i need to map it to get it to work? I’d like to get the results in the same list as the one above but when i add it in it works, but it gives me a new list for every mount point instead of just one list!
This is what I get:
$ ansible localhost -m setup | sed ‘1 s/^.*$/{/’ | jq ‘.ansible_facts | {hostname: .ansible_hostname, Disks: .ansible_devices| with_entries(.value |= .size), Partitions: .ansible_devices.partitions | with_entries(.value |= .size), Mounts: .ansible_mounts.mount }’
{
“hostname”: “ip-172-31-16-55”,
“Disks”: {
“xvda”: “10.00 GB”
},
“Partitions”: {
“xvda1”: “1.00 GB”,
“xvda2”: “9.00 GB”
},
“Mounts”: “/”
}
{
“hostname”: “ip-172-31-16-55”,
“Disks”: {
“xvda”: “10.00 GB”
},
“Partitions”: {
“xvda1”: “1.00 GB”,
“xvda2”: “9.00 GB”
},
“Mounts”: “/boot”
}
BUT, this is what I’m trying to get:
{
“hostname”: “ip-172-31-16-55”,
“Disks”: {
“xvda”: “10.00 GB”
},
“Partitions”: {
“xvda1”: “1.00 GB”,
“xvda2”: “9.00 GB”
},
“Mounts”: “/”
“Mounts”: “/boot”
}
}
or even a “,” separated list on one line?
},
“Mounts”: “/”, “/boot”
}
I think I can’t see the wood for the trees now and a little nudge would be greatly appreciated.
Thanks