# writing output to a csv file

**URL:** <https://forum.ansible.com/t/writing-output-to-a-csv-file/33736>\
**Category:** Ansible Project\
**Created:** [September 3, 2020, 10:44am UTC](https://forum.ansible.com/t/writing-output-to-a-csv-file/33736 "2020-09-03T10:44:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SK11](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SK11](https://forum.ansible.com/u/SK11)\
**Post date:** [September 3, 2020, 10:44am UTC](https://forum.ansible.com/t/writing-output-to-a-csv-file/33736/1 "2020-09-03T10:44:29Z")

</div>

Hi,

Ansible version - 2.9

Facing issue in writing output to a csv file, its not writing the output consistently into the file.

Having an inventory file with three server IPs, script will execute command to check the disk space of each server and writing the output to a csv file.

Sometimes its writing all the three server details into the file, sometimes its writing only one or two server details into the file.

- hosts: localhost  
connection: local  
gather\_facts: False  
vars:  
filext: “.csv”  
tasks:

- name: get the username running the deploy  
local\_action: command whoami  
register: username\_on\_the\_host

- name: get current dir  
local\_action: command pwd  
register: current\_dir

- name: create dir  
file: path={{ current\_dir.stdout }}/HCT state=directory

- name: Set file path here  
set\_fact:  
file\_path: “{{ current\_dir.stdout }}/HCT/HCT\_check{{ filext }}”

- name: Creates file  
file: path={{ file\_path }} state=touch

# Writing to a csv file

- hosts:
- masters  
become: false  
vars:  
disk\_space: "Able to get disk space for the CM {{ hostname }} "  
disk\_space\_error: “The server {{ hostname }} is down for some reason. Please check manually.”  
disk\_space\_run\_status: “{{disk\_space}}”  
cur\_date: “{{ansible\_date\_time.iso8601}}”

tasks:

- name: runnig command to get file system which are occupied  
command: bash -c “df -h | awk ‘$5\>20’”  
register: disk\_space\_output  
changed\_when: false  
ignore\_errors: True  
no\_log: True

- name: Log the task get list of file systems with space occupied  
lineinfile:  
dest: “{{ hostvars[‘localhost’][‘file\_path’] }}”  
line: “File system occupying disk space, {{ hostname }}, {{ ip\_address }}, {{ cur\_date }}”  
insertafter: EOF  
state: present  
delegate\_to: localhost

Please help to resolve this issue.

---

<div class="post-metadata">

**Author:** ![Nuno\_Jordao](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nuno\_Jordao](https://forum.ansible.com/u/Nuno_Jordao)\
**Post date:** [September 3, 2020, 12:27pm UTC](https://forum.ansible.com/t/writing-output-to-a-csv-file/33736/2 "2020-09-03T12:27:08Z")

</div>

Hello,

If you are writing to the file in parallel into the localhost you may have concurrency issues. Try to use only one fork to see if it helps.

Nuno Jordão

---

<div class="post-metadata">

**Author:** ![SK11](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SK11](https://forum.ansible.com/u/SK11)\
**Post date:** [September 4, 2020, 10:52am UTC](https://forum.ansible.com/t/writing-output-to-a-csv-file/33736/3 "2020-09-04T10:52:57Z")

</div>

Thank Nuno, after changed the fork value its working fine now.

---

<div class="post-metadata">

**Author:** ![Nuno\_Jordao](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nuno\_Jordao](https://forum.ansible.com/u/Nuno_Jordao)\
**Post date:** [September 5, 2020, 1:54am UTC](https://forum.ansible.com/t/writing-output-to-a-csv-file/33736/4 "2020-09-05T01:54:11Z")

</div>

I’m glad it worked. Beware that, depending on your playbook and number of hosts, using only one fork may slow down it a lot.  
If you are using Ansible 2.9 the “throttle: 1” in the file writing task may be better.

Nuno Jordão
