# Unable to get correct disk space utilized percentage using Ansible API

**URL:** <https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531>\
**Category:** Ansible Project\
**Created:** [March 25, 2020, 2:15pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531 "2020-03-25T14:15:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 25, 2020, 2:15pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/1 "2020-03-25T14:15:51Z")

</div>

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%.

```auto
Here is my playbook code:

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 25, 2020, 4:39pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/2 "2020-03-25T16:39:59Z")

</div>

That is not how you calculate percentage, you need to divide on item.size\_total  
and not item.size\_available.

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 25, 2020, 4:54pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/3 "2020-03-25T16:54:07Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![klingac](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klingac](https://forum.ansible.com/u/klingac)\
**Post date:** [March 25, 2020, 5:49pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/4 "2020-03-25T17:49:18Z")

</div>

It could be this kind of problem [http://linuxshellaccount.blogspot.com/2008/12/why-du-and-df-display-different-values.html](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):

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [March 25, 2020, 5:56pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/5 "2020-03-25T17:56:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 25, 2020, 7:10pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/6 "2020-03-25T19:10:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patrick\_patra](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@patrick\_patra](https://forum.ansible.com/u/patrick_patra)\
**Post date:** [March 25, 2020, 7:27pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/7 "2020-03-25T19:27:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 25, 2020, 7:56pm UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/8 "2020-03-25T19:56:14Z")

</div>

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

This consider a very rude behavior.

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 26, 2020, 6:09am UTC](https://forum.ansible.com/t/unable-to-get-correct-disk-space-utilized-percentage-using-ansible-api/32531/9 "2020-03-26T06:09:07Z")

</div>

Thank you @Kai for the excellent explanation 🙂
