Use select and map with ansible

ansible

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 any search test that can be applied to a hashmap (The closest I can think of is the python search method of an re -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 filter

Below is an example playbook using both approaches leading to the same result.

---
- name: Sum size of FS
  hosts: localhost
  gather_facts: false

  vars:
    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

  tasks:

    - name: Calculate with selectattr, map and sum
      debug:
        msg: "{{ FS | selectattr('vg_name', 'equalto', 'vgapplis') | map(attribute='size_FS') | list | sum }}"

    - name: Calculate with json_query
      vars:
        sum_query: "[?vg_name=='vgapplis'].size_FS | sum(@)"
      debug:
        msg: "{{ FS | json_query(sum_query) }}"

And the result

PLAY [Sum size of FS] ****************************************************************

TASK [Calculate with selectattr, map and sum] ****************************************
ok: [localhost] => {
    "msg": "20"
}

TASK [Calculate with json_query] *****************************************************
ok: [localhost] => {
    "msg": "20"
}

PLAY RECAP ***************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0