Ansible - Snippets to check for diskspace or show available space on harddisk:
- 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
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
Any questions?
Feel free to contact us. Find all contact information on our contact page.