- name: Extract CSV data and create summary report hosts: all ignore_unreachable: true vars: energy_label: "Energy/Ah" voltage_label: "Voltage/V" time_label: "Timestamp" output_file: "Summary.csv" tasks: - name: Initialize output file with headers local_action: shell echo "Host,Energy,Voltage,Timestamp" > "{{ output_file }}" run_once: true delegate_to: localhost - name: Process CSV data block: - name: Read CSV file from remote host ansible.builtin.shell: cat /var/www/html/Data/Status.csv register: csv_content - name: Extract energy and voltage values ansible.builtin.set_fact: energy_value: "{{ (csv_content.stdout_lines | select('search', '^' + energy_label) | first | split(','))[1] | default('Not Found') }}" voltage_value: "{{ (csv_content.stdout_lines | select('search', '^' + voltage_label) | first | split(','))[1] | default('Not Found') }}" time_value: "{{ (csv_content.stdout_lines | select('search', '^' + time_label) | first | split(','))[1] | default('Not Found') }}" - name: Append values to output file local_action: shell echo "{{ inventory_hostname }},{{ energy_value }},{{ voltage_value }},{{ time_value }}" >> "{{ output_file }}" delegate_to: localhost rescue: - name: Handle errors by writing Not Found values local_action: shell echo "{{ inventory_hostname }},Not Found,Not Found,Not Found" >> "{{ output_file }}" delegate_to: localhost - name: Write Not Found row for unreachable host local_action: shell echo "{{ inventory_hostname }},Not Found,Not Found,Not Found" >> "{{ output_file }}" delegate_to: localhost when: ansible_host_unreachable is defined and ansible_host_unreachable - name: Copy result file to Apache html folder hosts: localhost tasks: - name: Copy CSV file to /var/www/html/ copy: src: Summary.csv dest: /var/www/html/Summary.csv owner: www-data group: www-data mode: '0644'