writing output to a csv file

115 views
Skip to first unread message

SK

unread,
Sep 3, 2020, 6:44:29 AM9/3/20
to Ansible Project
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.


Nuno Jordão

unread,
Sep 3, 2020, 8:27:58 AM9/3/20
to ansible...@googlegroups.com
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

--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/79186368-aa44-423b-b13a-c691269dbcabn%40googlegroups.com.

SK

unread,
Sep 4, 2020, 6:52:57 AM9/4/20
to Ansible Project
Thank Nuno, after changed the fork value its working fine now.

Nuno Jordão

unread,
Sep 4, 2020, 9:55:00 PM9/4/20
to ansible...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages