0

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

  • 1
    Hi @pnakache and welcome to SF. does not work is nether a good description of your problem in itself. You should always give as much info as possible for others to help you: show the output when running the playbook, describe the result you get compared to the one you expect, if you have any errors, describe what you have done to try to fix them and where you are stuck. – Zeitounator Jun 21 at 18:18
0

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   
  • Thank you very much. It works but I had to replace equalto by match for the compatibility with pre-jinja 2.8 – pnakache Jun 24 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.