On Wed, 22 Jan 2020 10:32:56 -0800 (PST) jean-christophe manciot <[email protected]> wrote:
> list:
> - key1:
> - 'abc'
> - 'def'
> key2: 'ghi'
> key3: 'jkl'
> - key1:
> - 'mno'
> - 'pqr'
> key3: 'stu'
> key4: 'dfg'
> - key1:
> - 'vwx'
> - 'yza'
> key3: 'okl'
> key4: 'azel'
> [...]
> - extract records if "key3" contains a 'k'
> - keep only keys "key1", "key3" and "key4"
> - to be as efficient as possible with thousands of records
>
> In this case, we expect the resulting list:
> result_list:
> - key1:
> - 'abc'
> - 'def'
> key3: 'jkl'
> - key1:
> - 'vwx'
> - 'yza'
> key3: 'okl'
> key4: 'azel'
The task below does the job
- set_fact:
result_list: "{{ result_list|
default([]) + [
dict(keys|
zip(keys|
map('extract', item)|
list))] }}"
vars:
keys: "{{ ['key1', 'key3', 'key4']|
intersect(item.keys()|list) }}"
loop: "{{ list|
selectattr('key3', 'regex', '^.*k.*$')|
list }}"
json_query is not much of use here, I think. If you have any large sets'
benchmarks I'll be interested to learn. Thank you.
HTH,
-vlado
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20200122214943.1799c233%40gmail.com.
pgpTQ8voK4TjI.pgp
Description: OpenPGP digital signature
