# Ansible playbook disk space csv report problem

**URL:** <https://forum.ansible.com/t/ansible-playbook-disk-space-csv-report-problem/35075>\
**Category:** Ansible Project\
**Created:** [April 28, 2021, 5:47am UTC](https://forum.ansible.com/t/ansible-playbook-disk-space-csv-report-problem/35075 "2021-04-28T05:47:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna\_gupta](https://avatars.discourse-cdn.com/v4/letter/k/cab0a1/32.png) [@krishna\_gupta](https://forum.ansible.com/u/krishna_gupta)\
**Post date:** [April 28, 2021, 5:47am UTC](https://forum.ansible.com/t/ansible-playbook-disk-space-csv-report-problem/35075/1 "2021-04-28T05:47:14Z")

</div>

Dear All,

I am executing the df -kh command saving the result in csv but result is not coming in proper csv format.

--------Playbook--------

---

<div class="post-metadata">

**Author:** ![rolandm](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rolandm/32/755_2.png) [@rolandm](https://forum.ansible.com/u/rolandm)\
**Post date:** [April 30, 2021, 7:46am UTC](https://forum.ansible.com/t/ansible-playbook-disk-space-csv-report-problem/35075/2 "2021-04-30T07:46:34Z")

</div>

Hello,

> Dear All,
> 
> I am executing the df -kh command saving the result in csv but result is not coming in proper csv format.
> 
> --------Playbook--------
> 
> * * *
> 
> - name: Generate the disk spacke in csvormat  
> hosts: all  
> tasks:
> 
> - name: disk space  
> shell: “df -kh”  
> register: shell\_output
> 
> - set\_fact: t1=“{{ hostvars[inventory\_hostname][‘shell\_output’][‘stdout\_lines’] }}”
> 
> - debug: var=t1
> 
> - name: Generate Report  
> template:  
> src: hosts.j2  
> dest: /tmp/host\_report.csv  
> delegate\_to: localhost  
> run\_once: yes
> 
> -------template: -----------
> 
> #SNo., Filesystem, Size, Used, Avail, Use%, Mounted on
> 
> {% for hostnode in ansible\_play\_hosts %}  
> {{ loop.index }},{{ t1 }},{{ hostnode }}
> 
> {% endfor %}
> 
> -please advice…

You did not tell us what kind of problems you encountered and may be what you expect. Typically, many people - including me - do not answer.

Thus, missing problem description:

1. the template produces a single line for every host. Output of the df command contains one line per filesystem/disk
2. t1 contains lines of data separated by one or more spaces. Separators - I assume - should be single ‘,’
3. the header of df output is include as first line in t1

Solutions  
(1) make a second loop in hosts.j2 for the lines in t1  
(2) use some expression to replace one or more whitespaces with a ‘,’  
(3) not addressed, this remains to you

{% for hostnode in ansible\_play\_hosts %}  
{% for df\_line in t1 %}  
{{ loop.index }},{{ df\_line | regex\_replace(‘\s+’, ‘,’) }},{{ hostnode }}  
{% endfor %}  
{% endfor %}

BR,  
Roland
