# Retrieve facts from Ansible Setup module using JQ

**URL:** <https://forum.ansible.com/t/retrieve-facts-from-ansible-setup-module-using-jq/36161>\
**Category:** Ansible Project\
**Created:** [December 31, 2021, 1:24pm UTC](https://forum.ansible.com/t/retrieve-facts-from-ansible-setup-module-using-jq/36161 "2021-12-31T13:24:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [December 31, 2021, 1:24pm UTC](https://forum.ansible.com/t/retrieve-facts-from-ansible-setup-module-using-jq/36161/1 "2021-12-31T13:24:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 31, 2021, 4:11pm UTC](https://forum.ansible.com/t/retrieve-facts-from-ansible-setup-module-using-jq/36161/2 "2021-12-31T16:11:16Z")

</div>

Weird. The shell script version got split into multiple text boxes by the groups back-end. Anyway, the jq query should be pretty clear anyway.

---

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [January 3, 2022, 4:07pm UTC](https://forum.ansible.com/t/retrieve-facts-from-ansible-setup-module-using-jq/36161/3 "2022-01-03T16:07:11Z")

</div>

Thanks for that, It was exactly what I was looking for. I was so close…

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)] | . | select(length\>0), 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”: [  
“/”,  
“/boot”  
]  
}
