I have data that is represented in this manner:
{
\"key1\": [{
\"name\": \"some name1\",
\"index\": \"some idx1\"
},
{
\"name\
Ansible has rather limited tools to work with keys in dictionaries (see this answer).
But dictsort filter can be handy sometimes, as in your case.
It converts dict into list and you can use json_query
to process it.
Here's a task to fetch root key by index name:
- debug:
msg: "key is '{{ list_with_keys | json_query(qry) }}' for index {{ item }}"
vars:
list_with_keys: "{{ dict1 | dictsort | to_json | from_json }}"
qry: "[?contains(([1] | [].index),`{{ item }}`)][] | [0]"
with_items:
- some idx5
- some idx3
where dict1
is taken from your example.