I want to filter every keys value from below json using json_query | JMESPATH, how to achieve this?
{
"facts_hash": {
"processors::count": "96",
"processors::physicalcount": "2",
"processor0": "Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
"processorcount": "96",
"macaddress": "08:f1:ea:6d:04:3G",
"ipaddress": "192.168.101.135",
"manufacturer": "HPE",
"productname": "ProLiant DL360 Gen10",
"serialnumber": "SGH93GDCR",
"memorysize_mb": "773730.12",
"ipmi_ipaddress": "172.16.200.28",
},
"id": 284
}
Expected output, can be as below
[ "96",
"2",
"Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
"96",
"08:f1:ea:6d:03:3c",
"192.168.101.135",
"HPE",
"ProLiant DL360 Gen10",
"SGH936XG91",
"773730.12",
"172.16.100.24",
"284" ]
Here is the below query which i have tried which return the value.
"{{ facts_hash | json_query('[manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]') | list }}"
few keys are in json with four :: which I am not able to filter here are those keys "processors::count" & "processors::physicalcount" along with i am not able to fetch the "id"
query return value.
ok: [localhost] => {
"ansible_facts": {
"allvalue": [
"HPE",
"192.168.101.135",
"08:f1:ea:6d:03:3c",
"ProLiant DL360 Gen10",
"SGH936XG91",
"96",
"Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz",
"773730.12",
"172.16.100.24"
]
},
"changed": false
}
Thanks
I would do that with facts_hash | dict2items | map(attribute="value") | list
myself, but the answer to your question appears to be to quote the key names in the JMESPath selector:
- debug:
msg: "{{ facts_hash |
json_query('[\"processors::count\", \"processors::physicalcount\", manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]') | list }}"
which one can make a little more legible by extracting the JMESPath expression out to its own local variable:
- debug:
msg: "{{ facts_hash | json_query(facts_query) | list }}"
vars:
facts_query: '["processors::count", "processors::physicalcount", manufacturer, ipaddress, macaddress, productname, serialnumber, processorcount, processor0, memorysize_mb, ipmi_ipaddress]'
as for
along with i am not able to fetch the "id"
that's because id
is a sibling of facts_hash
, so whatever structure contains facts_hash
that you are doing a set_fact:
to extract facts_hash
, you'll want a similar extraction to grab its id
field