Check Free Disk Space on a Single or Multiple Ubuntu Servers Using Ansible

This blog post will show you how to check how much disk space you have on a Ubuntu 20.04 server using an Ansible playbook.

Register

To achieve this task, I will be using two Ansible module, and the first is the Register variable. Register variables allow us to save the output of an Ansible task. In our case, I will use the register command to save the output of the dh -h command.

Debug

Using the debug module and the stdout_lines command I will output the result to my terminal screen.

Using this method allows me to check the disk space usage of one or multiple Ubuntu servers without needing to log in to each one of them and check it.

Playbook

Below is the full playbook with the register command 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.

---
- hosts: servers
  tasks:
    - name:
      command: df -h
      register: storage
    - debug: var=storage.stdout_lines

You can see the results below.

TASK [debug] 
 ok: [ubuntu-server-01] => {
     "storage.stdout_lines": [
         "Filesystem      Size  Used Avail Use% Mounted on",
         "udev            2.0G     0  2.0G   0% /dev",
         "tmpfs           394M  684K  393M   1% /run",
         "/dev/sda1        29G  2.1G   27G   8% /",
         "tmpfs           2.0G     0  2.0G   0% /dev/shm",
         "tmpfs           5.0M     0  5.0M   0% /run/lock",
         "tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup",
         "/dev/sda15      105M  6.1M   99M   6% /boot/efi",
         "/dev/sdb1       7.9G   36M  7.4G   1% /mnt",
         "tmpfs           394M     0  394M   0% /run/user/1000"

For more Ansible articles visit the main category page.

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.