I have a dictionary and I would like to select all elements having vg_name == 'vgapplis'
and calculate the sum of their size_FS
. I tried this but it does not work. An idea ?
- set_fact:
FS_vgapplis:"{{ FS | select(''search'', ''\bvgapplis\b'')| map(attribute='size_FS')|list|sum }}"
This is what my FS
variable looks like
FS:
- nom_FS: /appm/oracle/product
nom_LV: lv_product
size_FS: 5
owner_FS: oracle
group_FS: dba
vg_name: vgapplis
- nom_FS: /appm/oracle/product/12.1.0.2
nom_LV: lv_12102
size_FS: 15
owner_FS: oracle
group_FS: dba
vg_name: vgapplis
- nom_FS: /apps/oracle/logs
nom_LV: lvlogs
size_FS: 5
owner_FS: oracle
group_FS: dba
vg_name: vglogs
Thank you
Best Answer
You are using the
select
filter wrongly with rather weird argument (I suspect a copy/paste error but I'm not sure).select
will apply a test to each object in the list. I don't know anysearch
test that can be applied to a hashmap (The closest I can think of is the pythonsearch
method of anre
-i.e. regexp- object which would not be appropriate anyway)In your case, you are looking for a specific value of an attribute of your hashmap. This can be done with the
selectattr
filter which will apply a test to a given attribute of the objects in the list and return only the ones passing the test.There is a different approach to your problem which is more compact IMO using the
json_query
filterBelow is an example playbook using both approaches leading to the same result.
And the result