Reputation: 21
After having loaded the following Ansible local facts :
{
"cdbs": {
"e01ca601": {
"char_set": "AL32UTF8",
"home": "/u01/dbhome_1",
"npdbs": "1",
"pdbs": "pdb1"
},
"e01ca602": {
"char_set": "WE8ISO8859P1",
"home": "/u01/dbhome_2",
"npdbs": "0",
"pdbs": ""
},
"e01ca603": {
"char_set": "AL32UTF8",
"home": "/u01/dbhome_3",
"npdbs": "0",
"pdbs": ""
},
"e01ca604": {
"char_set": "WE8ISO8859P1",
"home": "/u01/dbhome_1",
"npdbs": "0",
"pdbs": ""
},
"e01ca605": {
"char_set": "AL32UTF8",
"home": "/u01/dbhome_2",
"npdbs": "0",
"pdbs": ""
},
"e01ca900": {
"char_set": "WE8ISO8859P1",
"home": "/u01/dbhome_3",
"npdbs": "1",
"pdbs": "pdb2"
}
},
"pdbs": {
"pdb1": {
"cdb": "e01ca601",
"creation_time": "2020-01-21 14:10:39"
},
"pdb2": {
"cdb": "e01ca900",
"creation_time": "2020-01-13 13:34:21"
}
}
}
I would like to use them in a filter to select only on e.g. cdbs.*.char_set == 'AL32UTF8', but am not able to figure out how to add the filter condition in a task:
- name: "Task1"
vars:
myquery : '[cdbs.*.char_set][0]'
debug:
msg:
- "Query condition: {{ myquery }}"
- "Query filter : {{ ansible_local | json_query(myquery) }}"
In addition to this would it be possible to get a list of names of the items, i.e. e01ca605 etc ?
Any help would be appreciated!
Regards, Dirk
Upvotes: 1
Views: 449
Reputation: 21
finally, combining Vladimir's replies the solution found is:
- name: "Task10"
vars:
myquery: "[?value.char_set=='AL32UTF8']"
debug: var=item.key
with_items: "{{ ansible_local.cdbs|dict2items|json_query(myquery) }}"
resulting in a list of items
ok: [exa101vm01] => (item={'key': u'e01ca900', 'value': {u'home': u'/u01/dbhome_1', u'npdbs': u'1', u'char_set': u'AL32UTF8', u'pdbs': u'pdb1', u'archivelog_mode': u'NOARCHIVELOG'}}) => {
"ansible_loop_var": "item",
"item": {
"key": "e01ca900",
"value": {
"archivelog_mode": "NOARCHIVELOG",
"char_set": "AL32UTF8",
"home": "/u01/dbhome_1",
"npdbs": "1",
"pdbs": "pdb1"
}
},
"item.key": "e01ca900"
}
... (cut here)
Syntax and possibilities seem quite powerful, however difficult to debug ;-)
Thanks for your help!
Cheers, Dirk
Upvotes: 0
Reputation: 21
I got this (using a pipe) to work:
- name: "Task4"
vars:
myquery: cdbs.* | [?char_set=='AL32UTF8']
debug:
msg:
- "{{ item }}"
with_items: "{{ ansible_local | json_query(myquery) }}"
Upvotes: 1
Reputation: 68384
Q: Select cdbs.*.char_set == 'AL32UTF8'
A: The tasks below
- set_fact:
sel_AL32UTF8: "{{ cdbs|json_query(myquery) }}"
vars:
myquery: "*|[?char_set=='AL32UTF8']"
- debug:
var: sel_AL32UTF8
give
"sel_AL32UTF8": [
{
"char_set": "AL32UTF8",
"home": "/u01/dbhome_3",
"npdbs": "0",
"pdbs": ""
},
{
"char_set": "AL32UTF8",
"home": "/u01/dbhome_1",
"npdbs": "1",
"pdbs": "pdb1"
},
{
"char_set": "AL32UTF8",
"home": "/u01/dbhome_2",
"npdbs": "0",
"pdbs": ""
}
]
Q: Get a list of names.
A: Apply the keys()
method. For example
- set_fact:
cdbs_keys: "{{ cdbs.keys()|list }}"
- debug:
var: cdbs_keys
give
"cdbs_keys": [
"e01ca900",
"e01ca602",
"e01ca603",
"e01ca601",
"e01ca604",
"e01ca605"
]
Upvotes: 0