Reputation: 498
In ansible, I've got a a list of dict like:
- { name: "a" }
- { name: "b", cond: true }
I would like to extract the list of name when cond is not defined or false (1) and also a list when cond is true or not defined (2):
1 => [ 'a' ]
2 => [ 'a', 'b' ]
How to do that? I did not find.
Thanks
Upvotes: 0
Views: 65
Reputation: 311298
You could do that with the json_query
filter:
---
- hosts: localhost
gather_facts: false
vars:
mylist:
- name: a
- name: b
cond: true
tasks:
- set_fact:
true_or_unset: "{{ mylist|json_query('[?cond == null || cond].[name]') }}"
false_or_unset: "{{ mylist|json_query('[?cond == null || !cond].[name]') }}"
- debug:
msg:
true_or_unset: "{{ true_or_unset }}"
false_or_unset: "{{ false_or_unset }}"
Which produces:
PLAY [localhost] ******************************************************************************
TASK [set_fact] *******************************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] => {
"msg": {
"false_or_unset": [
[
"a"
]
],
"true_or_unset": [
[
"a"
],
[
"b"
]
]
}
}
PLAY RECAP ************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
Upvotes: 1