Skip to content

Instantly share code, notes, and snippets.

@alexanderadam
Forked from mahemoff/Ansible Disk Check
Created September 15, 2018 12:40
Show Gist options
  • Save alexanderadam/5661307ef6ad1f4f42fa954524104219 to your computer and use it in GitHub Desktop.
Save alexanderadam/5661307ef6ad1f4f42fa954524104219 to your computer and use it in GitHub Desktop.
Show disk space and warn about disk full in Ansible
* Shows a message while asserting like:
ok: [host] => {
"msg": "disk usage 4.2B of total 20.0GB (21.0%) (should exceed limit 90.0%)"
}
* Note this only looks at first mount point on current node
* Fails if disk is near-full
* Last step pushes to a push-based monitoring service, which will alert us if it doesn't get there after some time
* Need to setup a variable `disk_limit`, which is the max acceptable usage ratio, e.g. set it to 0.8 if you want to keep disks within 80% of max size
- set_fact:
mount: "{{ ansible_mounts | first }}"
- set_fact: disk_usage="{{ mount.size_total - mount.size_available }}"
- set_fact: disk_usage_ratio="{{ disk_usage|float / mount.size_total }}"
- set_fact: disk_usage_s="{{ (disk_usage|float / 1000000000) | round(1, 'common') }}GB"
- set_fact: disk_total_s="{{ (mount.size_total / 1000000000) | round(1, 'common') }}GB"
- set_fact: disk_usage_ratio_s="{{ 100 * (disk_usage_ratio|float) | round(1, 'common') }}%"
- set_fact: disk_limit_ratio_s="{{ (100 * disk_limit|float) |round }}%"
- debug:
msg: "disk usage {{ disk_usage_s }} of total {{ disk_total_s }} ({{ disk_usage_ratio_s }}) (should be within limit {{ disk_limit_ratio_s }})"
- name:
assert:
that: ( (disk_usage|float)/mount.size_total ) < disk_limit|float
msg: "Disk usage {{ disk_usage_ratio_s }} exceeds {{ disk_limit_ratio_s }}"
tags: disk
any_errors_fatal: true
- name: indicate disks okay to statuscake
local_action: command /usr/bin/wget 'https://push.statuscake.com/?PK=abcdefghi&TestID=123456&time=0'
run_once: true
@bajajamit01
Copy link

df --output=avail /tmp

@percygrunwald
Copy link

I found this helpful to determine the size of /run on some hosts I manage, here's how I adapted this:

- name: Get size of /run
  shell: "df --block-size 1m /run --output=size | tail -1"
  register: shell_df_result

- name: Set facts
  set_fact:
    run_tmpfs_size_mb: "{{ shell_df_result.stdout | int }}"

- debug: var=run_tmpfs_size_mb

@GiovanniParisi
Copy link

GiovanniParisi commented Jun 14, 2021

To include mount point, deice and available space I used this task ( what do you think?):

---
- name: Check VM Infrastrutturali
  hosts: all
  vars:
     mount_all: []
  tasks:

    # Get List of device
    - set_fact:
        mount_all: "{{ mount_all + [{'host':ansible_host,'dev':item.device,'mount':item.mount,'free':(((item.size_available/1024)/1024)/1024)|round(2,'common'),'total':(((item.size_total/1024)/1024)/1024)|round(2,'common')}] }}"
      when: (item.size_total - item.size_available) > (item.size_total|float * 0.85)
      with_items: "{{ ansible_mounts | list }}"

    - name: Stato dei dischi > 85%
      debug:
         msg: "{{ mount_all }} "

@DaVince
Copy link

DaVince commented Sep 28, 2022

Hello,

I would like to share with you another way to perform this operation (only on Linux):


tasks:
  - name: Check /tmp freespace
    shell: df /tmp --output\=avail | tail -1
    register: tmp_freespace
      
  - fail:
      msg: /tmp does not have the minimum space required to continue (3Gb requested). 
    when: tmp_freespace.stdout|float is lt 3000000

Best regards,

Just a question, but why is lt rather than the more readable <?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment