On Mon, 6 Mar 2023 19:09:21 +0000
Aharonu <[email protected]> wrote:

> "csv": [
>         "cluster1,virtual_clu1,log_vol1,online,used",
>         "cluster1,virtual_clu1,log_vol2,offline,not_used",
>         "cluster1,virtual_clu1,log_vol3_Test,online,not_used",
>         "cluster1,virtual_clu1,log_vol4,offline,not_used",
>         "cluster2,virtual_clu2,log_vol6,online,used",
>         "cluster2,virtual_clu2,log_vol1,offline,not_used",
>         "cluster2,virtual_clu2,log_vol3,online,not_used"
>         "cluster2,virtual_clu2,log_vol4,online,used"
>     ]
> 
> *Looking for:*
> The header must be:  cluster_name,log_cluster,vol_name,status,work_status
> 
> not working:
> (it has to give here when status=online and  work_status=not_used )
> exclude:
> (it has go give here when status=offline and  vol_name=<name contacts
> 'Test'>)  
> working:
> (it has to give here when status=online and work_status=used
> [other then not working & exclude list] )
> 
> *example: file1.csv*
> 
> not working:
> cluster_name,log_cluster,vol_name,status,work_status
> cluster2,virtual_clu2,log_vol3,online,not_used
> 
> exclude:
> cluster_name,log_cluster,vol_name,status,work_status
> cluster1,virtual_clu1,log_vol3_Test,online,not_used
> cluster1,virtual_clu1,log_vol2,offline,not_used
> cluster1,virtual_clu1,log_vol4,offline,not_used
> cluster2,virtual_clu2,log_vol1,offline,not_used
> 
> working:
> cluster_name,log_cluster,vol_name,status,work_status
> cluster1,virtual_clu1,log_vol1,online,used
> cluster2,virtual_clu2,log_vol6,online,used
> cluster2,virtual_clu2,log_vol4,online,used

Put the header into the list

  header: [cluster_name, log_cluster, vol_name, status, work_status]

and use the filter *community.general.dict* to create the dictionary

  csv_dict: "{{ csv|
                map('split', ',')|
                map('zip', header)|
                map('map', 'reverse')|
                map('community.general.dict') }}"

gives

  csv_dict:
  - cluster_name: cluster1
    log_cluster: virtual_clu1
    status: online
    vol_name: log_vol1
    work_status: used
  - cluster_name: cluster1
    ...

Select the subsets

  notworking: "{{ csv_dict|
                  selectattr('status', '==', 'online')|
                  selectattr('work_status', '==', 'not_used') }}"
  exclude: "{{ csv_dict|
               selectattr('status', '==', 'offline')|
               selectattr('vol_name', 'regex', '^log_vol[1-4]$') }}"
  working: "{{ csv_dict|
               selectattr('status', '==', 'online')|
               difference(notworking)|
               difference(exclude) }}"

and write them to the files. Test it first

    - debug:
        msg: |
          dest: {{ item.1 }}
          {{ item.0 }}:
          {{ header|join(',') }}
          {% for l in lookup('vars', item.0) %}
          {{ l.values()|join(',') }}
          {% endfor %}
      loop:
        - [notworking, fiel1.csv]
        - [exclude, fiel2.csv]
        - [working, fiel3.csv]

gives (abridged)

  msg: |-
    dest: fiel1.csv
    notworking:
    cluster_name,log_cluster,vol_name,status,work_status
    cluster1,virtual_clu1,log_vol3_Test,online,not_used
    cluster2,virtual_clu2,log_vol3,online,not_used

  msg: |-
    dest: fiel2.csv
    exclude:
    cluster_name,log_cluster,vol_name,status,work_status
    cluster1,virtual_clu1,log_vol2,offline,not_used
    cluster1,virtual_clu1,log_vol4,offline,not_used
    cluster2,virtual_clu2,log_vol1,offline,not_used

  msg: |-
    dest: fiel3.csv
    working:
    cluster_name,log_cluster,vol_name,status,work_status
    cluster1,virtual_clu1,log_vol1,online,used
    cluster2,virtual_clu2,log_vol6,online,used
    cluster2,virtual_clu2,log_vol4,online,used


Notes:

* There are two items in *notworking* not only one

* The line where vol_name='log_vol3_Test' shouldn't be in the set
  *exclude* because of the condition status=='offline'. Fit the regex
  to your needs.

  cluster1,virtual_clu1,log_vol3_Test,online,not_used


-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20230307101422.2ce5d380%40gmail.com.

Attachment: pgpuWsCqpWct5.pgp
Description: OpenPGP digital signature

Reply via email to