-1

We have more than 1000+ VMs running on different Hyper-V nodes. I want to create a report in CSV format for one systemd service status.

For example, I would like to check the running status of postfix whether it's in state started or stopped. These statuses need to be print into CSV file format.

Expected result as below format

enter image description here

U880D
  • 8,601
  • 6
  • 24
  • 40
Ck_7
  • 469
  • 7
  • 12
  • 2
    Cool. What did you try so far? What happened? – Mark Setchell Sep 12 '22 at 09:22
  • Does [Ansible: How to get ... services?](https://stackoverflow.com/a/70376280/6771046), [How to check service exists ...?](https://stackoverflow.com/a/69773111/6771046) or [... specific systemd service from list of services using Ansible](https://stackoverflow.com/a/70590709/6771046) gives an idea where to start? – U880D Sep 12 '22 at 14:24
  • @U880D Thanks I have already gone through the Ansible documents regarding gather facts. But I am not able to export the status to CSV format. Do u have any idea how to play with Ansible ? – Ck_7 Sep 13 '22 at 04:42
  • https://gregsowell.com/?p=7289 Found some cool stuff with these I did some alternative then I got above output – Ck_7 Sep 15 '22 at 12:42

1 Answers1

0

Finally, I got solution for this above request. Here is the code below, helpful for others

Thanks to the gregsowell blog helped me get these done. https://gregsowell.com/?p=7289

---
- name: Generate an HTML report from jinja template
  hosts: postfix-hosts
  gather_facts: true
  vars:
    #email settings
    email_subject: System status Report
    email_host: stackoverflw.smtp.com
    email_from: noreply@stackoverflw.com
    email_to: AdMin_Stack@stackoverflw.com

    #random settings
    csv_path: /tmp
    csv_filename: report.csv
    headers: Hostname,OS,Distro Ver,Kernel Ver,Postfix Status,FQDN,Total VCPU,Total RAM,Total SWAP,Total Disk,Hyper-V

  tasks:
  - name: Gather last Postfix Status
    ansible.builtin.shell:  systemctl status postfix | egrep -i Active | awk '{ print $2,$3}'
    register: active

  - name: Save CSV headers
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename }}"
      line: "{{ headers }}"
      create: true
      state: present
    delegate_to: localhost
    run_once: true

  - name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename }}"
      line: "{{ inventory_hostname }},{{ ansible_distribution }},{{ ansible_distribution_version }},{{ ansible_kernel }},{{ active.stdout }},{{ ansible_fqdn }},{{ ansible_processor_vcpus }},{{ ansible_memtotal_mb }},{{ ansible_swaptotal_mb }},{{ ansible_devices.vda.partitions.vda1.size }},{{ ansible_product_name }}"
      create: true
      state: present
    delegate_to: localhost

  - name: Read in CSV to variable
    community.general.read_csv:
      path: "{{ csv_path }}/{{ csv_filename }}"
    register: csv_file
    delegate_to: localhost
    run_once: true

#  - name: debug csv_file
#    debug:
#      var: csv_file
#    run_once: true

  - name: Send Email
    community.general.mail:
      host: "{{ email_host }}"
      from: "{{ email_from }}"
      port: 25
      to: "{{ email_to }}"
      subject: "[Ansible] {{ email_subject }}"
      body: "{{ lookup('template', 'report.html.j2') }}"
      attach: "{{ csv_path }}/{{ csv_filename }}"
      subtype: html
    delegate_to: localhost
    run_once: true



report.html.j2

<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
    {% for header in headers.split(",") %}
    <th style="border: 1px solid black; padding: 8px 16px;">{{ header }}</th>
    {% endfor %}
</tr>
{% for host in csv_file.list %}
<tr>
    {% for header in headers.split(",") %}
    <td style="border: 1px solid black; padding: 8px 16px;">{{ host[header] }}</td>
    {% endfor %}
</tr>
{% endfor %}
</table>
Ck_7
  • 469
  • 7
  • 12