writing output to a csv file

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.

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

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

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