Bootstrap FreeKB - Ansible - df module (disk filesystem usage ansible_mounts)
Ansible - df module (disk filesystem usage ansible_mounts)

Updated:   |  Ansible articles

Ansible does not have a "df" module to capture the output of the df command. Instead, facts can be used to capture the df output.

Let's say the df command on server1 returns the following.

[john.doe@server1 ~]# df
Filesystem                           1K-blocks     Used Available Use% Mounted on
/dev/sda1                               372607   170989    177862  50% /boot
/dev/sda2                               129774     6994    122780   6% /

 

Here is how you can get the df output using facts.

---
- hosts: all
  tasks:
  - name: display ansible_mounts
    debug:
      var: ansible_mounts
...

 

Something like this should be returned. Notice here that size_available and size_total are in bytes, not kilobytes (KB).

  • size_available or size_total / 1024 = size in kilobytes (KB)
  • size_available or size_total / 1024 / 1024 = size in megabytes (MB)
  • size_available or size_total / 1024 / 1024 / 1024 = size in gigabytes (GB)
TASK [display ansible_mounts]
ok: [server1] => {
    "ansible_mounts": [
        {
            "block_available": 9162312, 
            "block_size": 4096, 
            "block_total": 9170560, 
            "block_used": 8248, 
            "device": "/dev/sda1", 
            "fstype": "xfs", 
            "inode_available": 18350077, 
            "inode_total": 18350080, 
            "inode_used": 3, 
            "mount": "/boot", 
            "options": "", 
            "size_available": 182130688, 
            "size_total": 381549568, 
            "uuid": "5629a4fd-3e07-4bfa-a4f7-5b2cccfd6e6e"
        }, 
        {
            "block_available": 9207314, 
            "block_size": 65536, 
            "block_total": 13578240, 
            "block_used": 4370926, 
            "device": "/dev/sda2", 
            "fstype": "xfs", 
            "inode_available": 20597477, 
            "inode_total": 21251126, 
            "inode_used": 653649, 
            "mount": "/", 
            "options": "", 
            "size_available": 125726720, 
            "size_total": 132888576, 
            "uuid": "N/A"
        }
  ]
}

 

Almost always, there are multiple mounts, which means you'll want to use the loop, with_items or with_list to loop through each item in the ansible_mounts list.

---
- hosts: all
  tasks:
  - name: loop through each ansible mount
    debug:
      msg: "{{ item }}"
    with_items: "{{ ansible_mounts }}"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


July 16 2022 by Amit Das
Hi, Thanks for sharing this. Could you please suggest how to customize this playbook in order to generate an alert if the disk space utilization exceeds the threshold (70% for example):

July 17 2022 by Jeremy (moderator)
You could use the when parameter (see http://www.freekb.net/Article?id=2251) to determine when the disk space threshold has been exceeded and then use the mail module (see http://www.freekb.net/Article?id=923) to create an email alert.

Add a Comment


Please enter b4a51f in the box below so that we can be sure you are a human.