Unable to get correct disk space utilized percentage using Ansible API

Here is the output of df -k from the target server:

# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1905280 0 1905280 0% /dev
tmpfs 1922024 0 1922024 0% /dev/shm
/dev/sda1 1942528 295616 1646912 16% /boot
/dev/mapper/OMT-home 15718400 832120 14886280 6% /home
/dev/mapper/OMT-tmp 6133760 33160 6100600 1% /tmp
/dev/mapper/OMT-opt 10475520 4658356 5817164 45% /opt
/dev/mapper/OMT-var 16734208 8803440 7930768 53% /var
/dev/loop0 3704296 3704296 0 100% /mnt/media
/dev/mapper/ihs_vg_yt 51466720 8790352 40038956 19% /ihs
tmpfs 384408 0 384408 0% /run/user/0

I wish to get Use percentage for /ihs mount which is 19%.

Here is my playbook code:

That is not how you calculate percentage, you need to divide on item.size_total
and not item.size_available.

@Kia Hi,

As suggested I tried

item.size_total instead of item.size_available

"{{ ansible_host }}_{{ item.mount }}: {{ (100 * ((item.size_total - item.size_available) / item.size_total)) | round(1, 'common') }}"

but it still gives me 22.2 % value instead of 19%

Can you please let me know what’s wrong and why the difference?

It could be this kind of problem http://linuxshellaccount.blogspot.com/2008/12/why-du-and-df-display-different-values.html

df reads only and trusts completely to superblocks.

Another reason could be, that you can have other running process that can keep a deleted file open.

Do you have any process

Dne středa 25. března 2020 17:54:07 UTC+1 Shifa Shaikh napsal(a):

I’d suggest putting a debug showing you the three numbers Ansible is using for item.size_total and item.size_available. As @klingac mentioned, it could be that the values that "“df -k” reports or uses in it’s computation aren’t the exact same ones that Ansible uses.

For instance, using the numbers shown in the sample “df -k” output - (51466720 - 40038956) / 51466720 - I calculate "22.2% not the 19% that “df” reports.

This is a little out of scope of this list, so I'll keep it brief.

When you create a filesystem mkfs reserve some block on the device.
df output available block but this value doesn't count the reserved blocks.

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/ihs_vg_yt 51466720 8790352 40038956 19% /ihs

Just to illustrate on your df output of /ihs by doing Total - Used - Available should equal 0 if no blocks was reserved.

51466720 - 8790352 - 40038956 = 82715324
So you have about 82.7 million 1K blocks reserved.

df uses the "Used" / ( "Used" + "Available") to calculate used
8790352 / (8790352 + 40038956) = approx. 0,18002205

So a little over 18 percent which is rounded up to 19%

When you in Ansible do size_total - size_available you are including the
reserved blocks in used and that is why you get a higher number than df.

hello everyone can someone help me with documentatiions on ansible network automation
(routers,switches,…)
for example if i want to make a LAN then i want to deploy configurations automatically with ansible playbooks

thanks to help me am just a biginner

Can you please not hijacking other people thread and make your own.

This consider a very rude behavior.

Thank you @Kai for the excellent explanation :slight_smile: