Closed tomkivlin closed 2 years ago
Files identified in the description:
If these files are incorrect, please update the component name
section of the description or use the !component
bot command.
Feel free to close if this isn't an issue with json_query, I have found a different solution to my needs: https://stackoverflow.com/questions/69953172/in-ansible-how-to-query-hostvars-to-get-a-specific-value-of-a-key-from-a-list-i
- debug: msg: "{{ hostvars[inventory_hostname] | to_nice_json | community.general.json_query(interfaces[?name=='PCI1.1'].mac_address) }}"
- debug: msg: "{{ hostvars[inventory_hostname] | to_nice_json | community.general.json_query(ansible_host) }}"
Please note that your queries are missing quotes. You need to quote what you pass to json_query
, it expects a string.
@felixfontein many thanks. So the correct syntax, respectively, should be:
- debug:
msg: "{{ hostvars[inventory_hostname] | to_nice_json | community.general.json_query('interfaces[?name==`PCI1.1`].mac_address') }}"
- debug:
msg: "{{ hostvars[inventory_hostname] | to_nice_json | community.general.json_query('ansible_host') }}"
@tomkivlin I think so. Otherwise it will definitely not work. (Whether it works with these I don't know, I don't use the json_query
filter.)
That's definitely not the right syntax, since json_query
operates on data structures, but you're passing in a JSON string. You at a minimum need to remove to_nice_json
.
@tomkivlin could you please confirm if this is still an issue? TIA
@russoz apologies for the delay. I can confirm that as @flowerysong suggests, I needed to remove to_nice_json
and as @felixfontein suggested, I needed quotes. So the following both work:
- debug:
msg: "{{ hostvars[inventory_hostname] | community.general.json_query('interfaces[?name==`PCI1.1`].mac_address') }}"
- debug:
msg: "{{ hostvars[inventory_hostname] | community.general.json_query('ansible_host') }}"
Worth noting again, that for my use-case (getting info about the host the play is running against), there is a simpler way as described here (thanks again to @flowerysong, I assume): https://stackoverflow.com/a/69958617/10556530
Thanks @tomkivlin!! Glad that it worked for you! :-)
Summary
When I try to query hostvars output (once converted to json), the json_query filters don't work. I've tested on https://jmespath.org/ and the query is sound. I am using the nb_inventory inventory plugin.
NB I originally opened on ansible/ansible but it got closed: https://github.com/ansible/ansible/issues/76289
Issue Type
Bug Report
Component Name
json_query
Ansible Version
Community.general Version
Configuration
OS / Environment
Mac OS 11.6.1
Steps to Reproduce
Running the following task produces what appears to be normal JSON that I can query elsewhere without issue:
Example snippet of the JSON output:
However when I try and query that JSON, I get the errors:
Even something simple doesn't work:
Expected Results
I expect to be presented with the value that the query results in, e.g.
(This is what https://jmespath.org/ produces.)
Actual Results
If I do use single quotes in the json_query, I just get a blank output (no failure).