Extract unique dictionary item from json

11 views
Skip to first unread message

janaki raman

unread,
Apr 20, 2021, 8:11:35 AM4/20/21
to Ansible Development
Hi All,

I am trying create logical volume group and need to get the vg name and device list from the json variable..  i am not sure what is the logic i need to use to get the desired result.. 

Below is the current output:

{
    "msg": [
        {
            "vg_hana_data": "/dev/sdf"
        },
        {
            "vg_hana_data": "/dev/sdg"
        },
        {
            "vg_hana_data": "/dev/sdh"
        },
        {
            "vg_hana_log": "/dev/sdi"
        },
        {
            "vg_hana_log": "/dev/sdj"
        }
    ]
}


Here is my code:

 - name: Filesystem data gather from input file
     set_fact:
       fs: "{{jsondata | json_query(jmesquery) }}"
     vars:
             jmesquery: 'filesystem[*].{hostname:hostname,devicename:Devicename,path:MountPath,type:FileSystemType,owner:FSOwner,group:FSGroup,vgname:vgname}'
   - name: creating dictionary to store vgname
     set_fact:
             #diskdata: "{{ diskdata|default({})|combine ([ {item.vgname: item.devicename} ]) }}"
        diskdata: "{{ diskdata|default([]) + [ {item.vgname: item.devicename} ] }}"
             #    vg_dict: "{{vg_dict|combine( {item.vgname : item.path}) }}"
     with_items:
       - "{{fs}}"
     when: "'{{item.vgname}}'.startswith('vg_hana')"
   - name:
     debug:
       msg: "{{diskdata}}"
   - name: Appending data variable
     set_fact:
       hana_data: "{{hana_data}} + ['{{item.vgname}}']"
     with_items: "{{fs}}"
     when: "'{{item.vgname}}'.startswith('vg_hana')"
     vars:
       hana_data: []
   - debug:
       msg: "{{hana_data}}"

Badly need some help on this.
Reply all
Reply to author
Forward
0 new messages