# Ansible variable data to CSV file

**URL:** <https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735>\
**Category:** Ansible Project\
**Created:** [March 6, 2023, 7:09pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735 "2023-03-06T19:09:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 7:09pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/1 "2023-03-06T19:09:21Z")

</div>

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=\<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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 7:18pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/2 "2023-03-06T19:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 7:20pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/3 "2023-03-06T19:20:54Z")

</div>

FYI … based on your CSV list of strings you can use split and map as predecessors in a filter sequence as I showed you in your prior question. This would convert your list of strings into a list of JSON dictionaries. You could then loop through that dict list on multiple tasks with an appropriate when: clause on each one.

Walter

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 7:43pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/4 "2023-03-06T19:43:46Z")

</div>

Hi Walter,

Getting for the day!

Actually I have data stored in ‘csv’ as mentioned below it is received from some other task and stories in ‘csv’ variable.

I wanted to created CVS file from that data with required data structure I requested. Could you please help me with this to achieve my requirement? Thank you

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 7:48pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/5 "2023-03-06T19:48:24Z")

</div>



---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 7:58pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/6 "2023-03-06T19:58:53Z")

</div>

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=\<name contacts ‘Test’\>)  
working:  
(it has to give here when status=online and work\_status=used  
[other then not working & exclude list] )

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 8:15pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/7 "2023-03-06T20:15:32Z")

</div>

- 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

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 8:26pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/8 "2023-03-06T20:26:06Z")

</div>

Hello Walter,

Oh, it helps.

So can I move these 3 conditions data (not workimg, excludr and working) into only **one csv file**? That is what my final required result.

Thank you,

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 8:53pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/9 "2023-03-06T20:53:43Z")

</div>

**% cat csv\_report.j2**  
(it has to give here when status=online and work\_status=not\_used )  
not working:  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for vol in csv\_json %}  
{% if vol.status == ‘online’ and vol.work\_status == ‘not\_used’ %}  
{{ vol.cluster\_name }},{{vol.log\_cluster}},{{vol.vol\_name}},{{vol.status}},{{vol.work\_status}}  
{% endif %}  
{% endfor %}

(it has go give here when status=offline and vol\_name=\<name contacts ‘Test’\>)  
exclude:  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for vol in csv\_json %}  
{% if vol.status == ‘offline’ and ‘Test’ in vol.vol\_name %}  
{{ vol.cluster\_name }},{{vol.log\_cluster}},{{vol.vol\_name}},{{vol.status}},{{vol.work\_status}}  
{% endif %}  
{% endfor %}

(it has to give here when status=online and work\_status=used [other then not working & exclude list] )  
working:  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for vol in csv\_json %}  
{% if vol.status == ‘online’ and vol.work\_status == ‘used’ %}  
{{ vol.cluster\_name }},{{vol.log\_cluster}},{{vol.vol\_name}},{{vol.status}},{{vol.work\_status}}  
{% endif %}  
{% endfor %}

**ansible task**

- name: write output file  
template:  
src: csv\_report.j2  
dest: csv\_report.csv  
delegate\_to: localhost

**% more csv\_report.csv**  
(it has to give here when status=online and work\_status=not\_used )  
not working:  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
cluster2,virtual\_clu2,log\_vol3,online,not\_used

(it has go give here when status=offline and vol\_name=\<name contacts ‘Test’\>)  
exclude:  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
cluster1,virtual\_clu1,log\_vol3\_Test,offline,not\_used

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

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 9:01pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/10 "2023-03-06T21:01:28Z")

</div>

If you want to learn all about jinja2 you can read more here.

[https://jinja.palletsprojects.com/en/3.1.x/](https://jinja.palletsprojects.com/en/3.1.x/)

Walter

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 7, 2023, 4:05am UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/11 "2023-03-07T04:05:51Z")

</div>

```auto
---
- name: Creating CSVs from subsets of data
  hosts: localhost
  gather_facts: false
  vars:
    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
  tasks:

    - name: Copy csv where status == 'online' and work_status == 'not_used'
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {% set (cluster_name,log_cluster,vol_name,status,work_status) = line.split(',', maxsplit=4) %}
          {% if status == 'online' and work_status == 'not_used' %}
          {{ line }}
          {% endif %}
          {% endfor %}
        dest: aharonu-online-not_used.csv

    - name: Copy csv where status == 'offline' and vol_name.endswith('Test')
      # Note: there are none of these in the test data.
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {% set (cluster_name,log_cluster,vol_name,status,work_status) = line.split(',', maxsplit=4) %}
          {% if status == 'offline' and vol_name.endswith('Test') %}
          {{ line }}
          {% endif %}
          {% endfor %}
        dest: aharonu-offline-test.csv

    - name: Copy csv where status == 'online' and work_status == 'used'
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {% set (cluster_name,log_cluster,vol_name,status,work_status) = line.split(',', maxsplit=4) %}
          {% if status == 'online' and work_status == 'used' %}
          {{ line }}
          {% endif %}
          {% endfor %}
        dest: aharonu-online-used.csv

```

Save the above as aharonu-csv.yml and run as

```auto
ansible-playbook aharonu-csv.yml -vv && grep , ./aharonu*.csv

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 7, 2023, 9:14am UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/12 "2023-03-07T09:14:22Z")

</div>

Put the header into the list

&nbsp;&nbsp;header: [cluster\_name, log\_cluster, vol\_name, status, work\_status]

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

&nbsp;&nbsp;csv\_dict: "{{ csv|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('split', ',')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('zip', header)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('map', 'reverse')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('community.general.dict') }}"

gives

&nbsp;&nbsp;csv\_dict:  
&nbsp;&nbsp;- cluster\_name: cluster1  
&nbsp;&nbsp;&nbsp;&nbsp;log\_cluster: virtual\_clu1  
&nbsp;&nbsp;&nbsp;&nbsp;status: online  
&nbsp;&nbsp;&nbsp;&nbsp;vol\_name: log\_vol1  
&nbsp;&nbsp;&nbsp;&nbsp;work\_status: used  
&nbsp;&nbsp;- cluster\_name: cluster1  
&nbsp;&nbsp;&nbsp;&nbsp;...

Select the subsets

&nbsp;&nbsp;notworking: "{{ csv\_dict|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('status', '==', 'online')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('work\_status', '==', 'not\_used') }}"  
&nbsp;&nbsp;exclude: "{{ csv\_dict|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('status', '==', 'offline')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('vol\_name', 'regex', '^log\_vol[1-4]$') }}"  
&nbsp;&nbsp;working: "{{ csv\_dict|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('status', '==', 'online')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(notworking)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(exclude) }}"

and write them to the files. Test it first

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

gives (abridged)

&nbsp;&nbsp;msg: |-  
&nbsp;&nbsp;&nbsp;&nbsp;dest: fiel1.csv  
&nbsp;&nbsp;&nbsp;&nbsp;notworking:  
&nbsp;&nbsp;&nbsp;&nbsp;cluster\_name,log\_cluster,vol\_name,status,work\_status  
&nbsp;&nbsp;&nbsp;&nbsp;cluster1,virtual\_clu1,log\_vol3\_Test,online,not\_used  
&nbsp;&nbsp;&nbsp;&nbsp;cluster2,virtual\_clu2,log\_vol3,online,not\_used

&nbsp;&nbsp;msg: |-  
&nbsp;&nbsp;&nbsp;&nbsp;dest: fiel2.csv  
&nbsp;&nbsp;&nbsp;&nbsp;exclude:  
&nbsp;&nbsp;&nbsp;&nbsp;cluster\_name,log\_cluster,vol\_name,status,work\_status  
&nbsp;&nbsp;&nbsp;&nbsp;cluster1,virtual\_clu1,log\_vol2,offline,not\_used  
&nbsp;&nbsp;&nbsp;&nbsp;cluster1,virtual\_clu1,log\_vol4,offline,not\_used  
&nbsp;&nbsp;&nbsp;&nbsp;cluster2,virtual\_clu2,log\_vol1,offline,not\_used

&nbsp;&nbsp;msg: |-  
&nbsp;&nbsp;&nbsp;&nbsp;dest: fiel3.csv  
&nbsp;&nbsp;&nbsp;&nbsp;working:  
&nbsp;&nbsp;&nbsp;&nbsp;cluster\_name,log\_cluster,vol\_name,status,work\_status  
&nbsp;&nbsp;&nbsp;&nbsp;cluster1,virtual\_clu1,log\_vol1,online,used  
&nbsp;&nbsp;&nbsp;&nbsp;cluster2,virtual\_clu2,log\_vol6,online,used  
&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;\*exclude\* because of the condition status=='offline'. Fit the regex  
&nbsp;&nbsp;to your needs.

&nbsp;&nbsp;cluster1,virtual\_clu1,log\_vol3\_Test,online,not\_used

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 7, 2023, 6:08pm UTC](https://forum.ansible.com/t/ansible-variable-data-to-csv-file/37735/13 "2023-03-07T18:08:50Z")

</div>

**Thank you Todd for your time and help.**

Hi All,

I have tested in my environment. It is working good.

i have one item to update for conditions. Could anyone please do me a favor . Thank you

[how to add one condition like vol\_name != ‘\_test’ or vol\_name !=‘Test\*’] ( this way working in Python looking help of playbook as i tried and not working)

- name: Copy csv where status == ‘online’ and work\_status == ‘not\_used’  
ansible.builtin.copy:  
content: |  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for line in csv %}  
{% set (cluster\_name,log\_cluster,vol\_name,status,work\_status) = line.split(‘,’, maxsplit=4) %}  
{% if status == ‘online’ and work\_status == ‘not\_used’ %}  
{{ line }}  
{% endif %}  
{% endfor %}  
dest: aharonu-online-not\_used.csv  
[How dow we include instead of endswith(‘Test’), i have to include like vol\_name contains ‘Test’, ‘test’..etc]
- name: Copy csv where status == ‘offline’ and vol\_name.endswith(‘Test’)

# Note: there are none of these in the test data.

ansible.builtin.copy:  
content: |  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for line in csv %}  
{% set (cluster\_name,log\_cluster,vol\_name,status,work\_status) = line.split(‘,’, maxsplit=4) %}  
{% if status == ‘offline’ or status == ‘online’ and vol\_name.endswith(‘Test’) %}  
{{ line }}  
{% endif %}  
{% endfor %}  
dest: aharonu-offline-test.csv

- name: Copy csv where status == ‘online’ and work\_status == ‘used’  
ansible.builtin.copy:  
content: |  
cluster\_name,log\_cluster,vol\_name,status,work\_status  
{% for line in csv %}  
{% set (cluster\_name,log\_cluster,vol\_name,status,work\_status) = line.split(‘,’, maxsplit=4) %}  
{% if status == ‘online’ and work\_status == ‘used’ %}  
{{ line }}  
{% endif %}  
{% endfor %}  
dest: aharonu-online-used.csv
