This is just covering the use case you added from your linked question, and not the really generic wildcard in a key question, that will most likely not be possible.
But in case you indeed have something to query like
key in ['people_in_town', 'people_with_town']
So, with a finite number of know keys, then you can definitely do something like
[@.people_in_town, @.people_with_town][].letter
That would give:
[
"a",
"b",
"e",
"f"
]
The main issue you'll face with a pure JMESPath approach is that the only actions you can do on keys is based on the keys
function, and sadly is is not possible to have both the array of keys and then come back to the part to select those keys.
If you really want to resort to only JMESPath, then, you'll have to end with two pass:
- Create the list of keys with
keys
and the starts_with
/ends_with
functions
- Then do a second pass to come back to the query as stated above
In an Ansible point of view this ends up with this utterly ugly playbook:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: >-
{{
data | json_query(
"[@." ~ data | json_query(
"join(
',@.',
keys(@)[?
starts_with(@, 'people_')
&& ends_with(@, '_town')
]
)"
) ~ "][].letter"
)
}}
vars:
data:
people_in_town:
- letter: a
- letter: b
people_with_shoes:
- letter: c
- letter: d
people_with_town:
- letter: e
- letter: f
That yields
PLAY [all] *******************************************************************************************
TASK [debug] *****************************************************************************************
ok: [localhost] =>
msg:
- a
- b
- e
- f
PLAY RECAP *******************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
So, really if I were you, I would stick to the option proposed in the answers of the linked question and stay away from JMESPath for this specific use case.
foo_*_bar
(note this is a wildcard and not a regex, actually) or to dofoo_abc_bar OR foo_def_bar OR foo_ghi_bar
? When the first one will most likely not be possible, the second one, is definitly. – Fridafriday