# ansible / monitoring / diagnostics

Automate Server Health Checks in seconds

A purely diagnostic playbook that instantly pulls vitals—CPU, RAM, Disk space, and Load averages—across your entire fleet and prints them directly to your console, without having to SSH into a single box.

$ ansible-playbook check_resources.yml
PlayCheck server resource usage
Hostsall (100% reachable)
Status0 changes, 0 failures
9 checks
metrics
command
module
18 tasks
playbook
# overview

Why this playbook holds up

When an alert fires or a user reports slowness, you need answers immediately. Instead of opening 10 terminal tabs, this playbook acts as a distributed stethoscope, registering raw bash outputs and cleanly presenting them in the Ansible debug log.

read-only

Strictly Safe Execution

Every single task is a read-only command. It makes zero state changes to the remote hosts, making it 100% safe to run blindly during an active incident.

human-readable

Familiar Outputs

By leveraging standard Linux commands like free -h and df -h, the debug outputs match exactly what a sysadmin is already used to reading.

# playbook

The full task list

check_resources.ymlYAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
---
- name: Check server resource usage
  hosts: all
  become: yes
  tasks:
    - name: Check hostname
      command: hostname
      register: hostname

    - name: Display hostname
      debug:
        var: hostname.stdout

    - name: Check RAM usage
      command: free -h
      register: memory

    - name: Display RAM usage
      debug:
        var: memory.stdout_lines

    - name: Check CPU usage
      command: top -bn1
      register: cpu

    - name: Display CPU usage
      debug:
        var: cpu.stdout_lines

    - name: Check disk usage
      command: df -h
      register: disk

    - name: Display disk usage
      debug:
        var: disk.stdout_lines

    - name: Check system uptime
      command: uptime
      register: uptime

    - name: Display uptime
      debug:
        var: uptime.stdout

    - name: Check load average
      shell: cat /proc/loadavg
      register: load

    - name: Display load average
      debug:
        var: load.stdout

    - name: Check logged-in users
      command: who
      register: users

    - name: Display logged-in users
      debug:
        var: users.stdout_lines

    - name: Check disk inode usage
      command: df -i
      register: inode

    - name: Display inode usage
      debug:
        var: inode.stdout_lines

    - name: Check mounted file systems
      command: mount
      register: mounts

    - name: Display mounted file systems
      debug:
        var: mounts.stdout_lines

Data Gathered

  • identity

    Identifies the host and checks for any actively logged-in users.

    hostname, who
  • compute

    Checks memory constraints, active processes, and overall CPU load over time.

    free -h, top, loadavg, uptime
  • storage

    Verifies disk space, active mount points, and file system inode availability.

    df -h, df -i, mount
# verify

What the output looks like

When executed, Ansible uses the debug module to print the stored stdout from your shell commands. stdout_lines is specifically used for multi-line outputs like `df -h` to format it cleanly as a list.

terminal snippet
TASK [Display RAM usage] ********************************
ok: [db-server-01] => {
    "memory.stdout_lines": [
        "              total       used       free     shared  buff/cache   available",
        "Mem:           15Gi      2.1Gi      9.3Gi      120Mi       3.6Gi        12Gi",
        "Swap:         4.0Gi         0B      4.0Gi"
    ]
}
 
$
# notes

Lessons from running this in production

WARN

The top -bn1 command generates a massive amount of output. Consider changing it to top -bn1 | head -n 15 so it doesn't flood your console.

INFO

Ansible actually natively gathers a lot of this data via gather_facts: yes! Hostname, uptime, memory, and mount data are available as built-in variables (e.g., ansible_memtotal_mb).

OK

Using stdout_lines instead of stdout for tabular data (like df or free) prevents Ansible from printing it as one long string with \n characters.