Last Updated on 1 month by Sachin G
Managing disk space is critical for any system administrator or DevOps engineer. If a server runs out of space, services may crash, databases can become corrupted, and downtime can lead to financial loss. Traditionally, Linux admins run df -h
manually, while Windows admins rely on chkdsk command or check disk command. But what if you manage multiple servers? Manual checks aren’t practical.
This tutorial demonstrates how to check disk space usage through an Ansible playbook. It utilizes the df -h
command, registers the output, and employs the debug module to display the results, facilitating efficient system administration and explaining how to handle Windows disk health checks using chkdsk. We’ll cover everything, including Ansible + Terraform integration, AWX scheduling, and disk health monitoring for HDD and SSD.
These tasks can range from simple shell commands to complex configuration management actions. Playbooks are at the heart of Ansible’s automation capabilities, enabling administrators to define infrastructure as code and maintain consistency across their environments.
This playbook should work on RPM-based Linux operating systems like Rocky, Alma Oracle Linux, etc. In our playbook, I’ll utilize a register variable to store the output. Then, I’ll employ the debug module to display the content stored within that variable.
Why Disk Space Monitoring Matters
Running out of disk space can cause:
- Service outages (web servers, DB crashes)
- Application corruption
- Security vulnerabilities (attackers exploiting full disks)
On Linux, admins often use:
df -h
du -sh /var/log
Some Ansible-related posts will help in setting up Ansible Navigator, and some helpful content is below.
On Windows, the equivalent health check uses:
chkdsk C:
or check disk command variations. However, manual checks on multiple servers are time-consuming and error-prone. That’s where Ansible comes in.
Why Use Ansible Instead of Manual Commands?
Ansible is agentless and automates repetitive tasks across multiple systems. Key benefits:
- Run commands on 100+ servers simultaneously
- Consistency: Same check everywhere
- Integration: Combine with CI/CD, AWX, and Terraform
- Cross-platform: Linux, Windows, cloud VMs
Compare that to the chkdsk command on Windows:
- Runs on one machine at a time
- Requires manual execution
- Often needs a reboot for chkdsk /f or chkdsk /r
Prerequisites
- Ansible installed on the control node (
sudo apt install ansible
orpip install ansible
) - SSH access to Linux servers
- Inventory file prepared
- For Windows: Enable WinRM for Ansible
Install Ansible Navigator And Execution Environment
Learn Smarter. Level Up Faster →
Want to master Linux, DevOps, Ansible, or Cloud workflows the smart way? I’ve curated a list of top-rated, real-world Udemy courses — based on student reviews and practical feedback.
Visit the Recommended Courses page to explore and enroll in courses trusted by the community.
See Curated Courses →Check Disk Space Usage Through Ansible Playbook
Register Variable
In the register variable, we’ll employ the keyword “register” to capture the output of a task. Below the playbook, the code illustrates the structure of the register variable used within a task.
debug module
The debug module to print the standard output of the task commands.
Below is the full playbook with the register command and the debug module. One thing to notice is that I am using stdout_lines and not stdout. The reason is that I like the output to be in lines. According to an accessible version or platform, you can run your playbook.
ansible-navigator run -m stdout playbook_name.yml OR ansible-playbook playbook_name.yml
Make sure you have SSH access to the target Linux systems and that Ansible is installed on the system from where you run the playbook. Additionally, ensure that your SSH keys are properly set up for passwordless authentication, or you may need to provide the SSH password through Ansible’s
--ask-pass
option.
Next i am providing two playbook only two minor differences . You can use any one which you feel better to use .
Check Disk Space Usage Playbook
Create a playbook inside your project directory and verify every managed host are running and working .
---
- name: Check Disk Space Usage
hosts: srv1.example.com
tasks:
- name: Disk usage from command module
ansible.builtin.command: df -h
register: space
- ansible.builtin.debug:
var: space
The playbook’s output will encompass the entirety of the captured output stored in the register variable. Specifically, it will include both the stdout and stdout_lines components.
The below playbook snippet will show only variable space.stdout_lines, which is automatically arranged in lines but you can also show complete information with stdout.
---
- name: Check Disk Space Usage
hosts: srv1.example.com
tasks:
- name: Disk usage from command module
ansible.builtin.command: df -h
register: space
- ansible.builtin.debug:
var: space.stdout_lines
Output :
I have done the above task through the playbook, but we can do this task through the Ansible ad-hoc command for a temporary purposes playbook. We can do it through different modes according to your needs.
Windows Disk Checks (CHKDSK) vs Linux Disk Checks
Windows approach uses:
chkdsk C:
check disk chkdsk
check disk command
chkdsk /f
orchkdsk /r
for repairs
Problems:
- One machine at a time
- Requires downtime for repairs
- Limited automation unless paired with Ansible
Linux + Ansible approach:
- Single playbook for hundreds of servers
- No reboot needed
- Works with AWX (Ansible Tower) for scheduled runs
How to Run CHKDSK on Windows via Ansible
- name: Run CHKDSK on Windows system drive
hosts: windows
gather_facts: no
vars:
drive_letter: C:
tasks:
- name: Execute chkdsk command
ansible.windows.win_shell: "chkdsk {{ drive_letter }}"
register: chkdsk_result
- debug:
var: chkdsk_result.stdout_lines
Disk Health Monitoring (Linux & Windows)
Linux HDD/SSD Health
Use smartctl for:
- hdd health check
- ssd disk health check
- check solid state drive health
- name: Check disk health with smartctl
hosts: linux_servers
become: yes
tasks:
- name: SMART check
ansible.builtin.command: smartctl -H /dev/sda
register: smart_health
- debug:
var: smart_health.stdout_lines
FAQs
Linux uses df -h
for usage; Windows uses chkdsk for file system repair.
/f
fixes errors/r
repairs sectors
The volume is RAW (unformatted). Format it first.
Yes—create workflows in AWX for multi-OS checks
I’m Sachin Gupta — a freelance IT support specialist and founder of techtransit.org. I’m certified in Linux, Ansible, OpenShift (Red Hat), cPanel, and ITIL, with over 15 years of hands-on experience. I create beginner-friendly Linux tutorials, help with Ansible automation, and offer IT support on platforms like Upwork, Freelancer, and PeoplePerHour. Follow Tech Transit for practical tips, hosting guides, and real-world Linux expertise!