Total Pageviews

Tuesday, June 3, 2025

Playbook to merge two csv residng on different server

 ---

- name: Consolidate two CSV files from different servers without Python

  hosts: localhost

  gather_facts: no

  vars:

    server1: "srv1"

    server2: "srv2"

    file1: "/tmp/OLAM_CP/CP_report/file.csv"

    file2: "/tmp/OLAM_CP/CP_report/file.csv"

    local_tmp_dir: "/tmp/csv_consolidate"

    consolidated_file: "/tmp/OLAM_CP/CP_report/final_data.csv"


  tasks:

    - name: Ensure local temp directory exists

      ansible.builtin.file:

        path: "{{ local_tmp_dir }}"

        state: directory

        mode: '0755'


    - name: Fetch CSV from Server 1

      ansible.builtin.shell: |

        scp {{ server1 }}:{{ file1 }} {{ local_tmp_dir }}/file1.csv

      delegate_to: localhost


    - name: Fetch CSV from Server 2

      ansible.builtin.shell: |

        scp {{ server2 }}:{{ file2 }} {{ local_tmp_dir }}/file2.csv

      delegate_to: localhost


    - name: Consolidate the two CSV files (append using shell)

      ansible.builtin.shell: |

        head -n 1 {{ local_tmp_dir }}/file1.csv > {{ consolidated_file }}

        tail -n +2 {{ local_tmp_dir }}/file1.csv >> {{ consolidated_file }}

        tail -n +2 {{ local_tmp_dir }}/file2.csv >> {{ consolidated_file }}

      delegate_to: localhost

    - name: Clean up temp directory

      ansible.builtin.file:

        path: "{{ local_tmp_dir }}"

        state: absent

No comments:

Post a Comment