Re: [ansible-project] Ansible variable data to CSV file

2023-03-07 Thread Vladimir Botka
On Mon, 6 Mar 2023 19:09:21 +
Aharonu  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= '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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20230307101422.2ce5d380%40gmail.com.


pgpuWsCqpWct5.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Ansible variable data to CSV file

2023-03-06 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
- name: not working

  debug: var=item

  loop: "{{ csv_json }}"

  when: item.status == 'online' and item.work_status == 'not_used'


- name: exclude

  debug: var=item

  loop: "{{ csv_json }}"

  when: (item.status == 'offline') and ('Test' in item.vol_name)


- name: working

  debug: var=item

  loop: "{{ csv_json }}"

  when: (item.status == 'online') and (item.work_status == 'used')


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Mar 6, 2023, at 2:58 PM, Aharonu  wrote:

Thanks Walter.

May I still ask a favor for CVS data structure atleast for any of one condion 
here so I can refer and work on remaining? Once I got flow it helps to explore 
more. Thanking you.

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=)
working:
(it has to give here when status=online and work_status=used
[other then not working & exclude list] )

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2C985CA4-26A4-4EB4-9EF0-C43FD4A0C13B%40nist.gov.


Re: [ansible-project] Ansible variable data to CSV file

2023-03-06 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
Do you have a playbook that isn't working or are you asking us to write this 
playbook? When I have challenges like this I create a sandbox playbook that 
tackles the individual challenge. When I get it working I copy that back into 
the larger playbook I am developing.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Mar 6, 2023, at 2:09 PM, Aharonu  wrote:

Hi All,

Could anyone please help for storing data into CSV file from variable with 
required structure ? Thank you

I have csv variable data from other set_fact as mentioned below

"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=)
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








--
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 
ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CANGEjuWprqkT90LdW3%2BVzUfs5YD4pwDZZnmiUMFHWdkVmqzjOQ%40mail.gmail.com.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/127203C1-7A98-46C0-9BCF-72065591FECB%40nist.gov.


[ansible-project] Ansible variable data to CSV file

2023-03-06 Thread Aharonu
Hi All,

Could anyone please help for storing data into CSV file from variable with
required structure ? Thank you

I have csv variable data from other set_fact as mentioned below

"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=)
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

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CANGEjuWprqkT90LdW3%2BVzUfs5YD4pwDZZnmiUMFHWdkVmqzjOQ%40mail.gmail.com.