From ee4c3b106d97cd930f9f056b3d0c465478c466d5 Mon Sep 17 00:00:00 2001 From: harwinder Date: Sat, 16 May 2026 17:34:36 -0400 Subject: [PATCH] Add disk and RAM health check playbook Reports disk usage, memory, and swap per host with warnings for partitions exceeding 85% usage. Co-Authored-By: Claude Opus 4.6 --- playbooks/health_check.yml | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 playbooks/health_check.yml diff --git a/playbooks/health_check.yml b/playbooks/health_check.yml new file mode 100644 index 0000000..b52f3d5 --- /dev/null +++ b/playbooks/health_check.yml @@ -0,0 +1,62 @@ +--- +- name: Check Host Availability + hosts: all + gather_facts: false + ignore_unreachable: true + + tasks: + - name: Ping hosts to check availability + ansible.builtin.ping: + register: ping_result + ignore_errors: true + ignore_unreachable: true + + - name: Add reachable hosts to group + ansible.builtin.group_by: + key: reachable_hosts + when: ping_result is succeeded + + - name: Report offline hosts + ansible.builtin.debug: + msg: "{{ inventory_hostname }} - OFFLINE" + when: ping_result is not succeeded + delegate_to: localhost + +- name: Disk and RAM Usage Report + hosts: reachable_hosts + become: true + gather_facts: true + + tasks: + - name: Get disk usage + ansible.builtin.shell: df -h --output=target,size,used,avail,pcent -x tmpfs -x devtmpfs | tail -n +2 + register: disk_usage + changed_when: false + + - name: Calculate memory percentage + ansible.builtin.set_fact: + mem_total_mb: "{{ ansible_memtotal_mb }}" + mem_used_mb: "{{ ansible_memtotal_mb - ansible_memfree_mb - (ansible_memory_mb.nocache.free | default(0)) }}" + mem_pct: "{{ (((ansible_memtotal_mb - ansible_memfree_mb) / ansible_memtotal_mb) * 100) | round(1) }}" + swap_total_mb: "{{ ansible_swaptotal_mb }}" + swap_used_mb: "{{ ansible_swaptotal_mb - ansible_swapfree_mb }}" + + - name: Print report + ansible.builtin.debug: + msg: | + ══════════════════════════════════════════════════════ + HOST: {{ inventory_hostname }} ({{ ansible_host }}) + OS: {{ ansible_distribution }} {{ ansible_distribution_version }} + ═���════════════════════════════════════════════════════ + RAM: {{ mem_used_mb }}MB / {{ mem_total_mb }}MB ({{ mem_pct }}%) + SWAP: {{ swap_used_mb }}MB / {{ swap_total_mb }}MB + ────���─────────────────────────────────��─────────────── + DISK USAGE: + {{ disk_usage.stdout }} + ══════════════════════════════════════════════════════ + + - name: Warn on high disk usage (>85%) + ansible.builtin.debug: + msg: "WARNING: {{ item.split()[-1] }} usage on {{ item.split()[0] }} of {{ inventory_hostname }}" + loop: "{{ disk_usage.stdout_lines }}" + when: (item.split()[-1] | replace('%','') | int) > 85