Unable to get correct disk space utilized percentage using Ansible API

36 views
Skip to first unread message

Shifa Shaikh

unread,
Mar 25, 2020, 10:15:51 AM3/25/20
to Ansible Project

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:
- name: Generate JSON data
  set_fact:
    "{{ ansible_host }}_{{ item.mount }}: {{ (100 * ((item.size_total - item.size_available) / item.size_available)) | round(1, 'common') }}"
  when: item.mount == '/ihs'
  with_items: '{{ ansible_mounts }}'

However, the output of my playbook run shows 28.5 % usage instead of 19%:
ok: [myhostone] => (item={u'block_used': 2857014, u'uuid': u'3fa1ec29-aca5-476b-8041-6a7bc6b1efc2', u'size_total': 52701921280, u'block_total': 12866680, u'mount': u'/ihs', u'block_available': 10009666, u'size_available': 40999591936, u'fstype': u'ext4', u'inode_total': 3276800, u'inode_available': 3264353, u'device': u'/dev/mapper/ihs_vg_yt', u'inode_used': 12447, u'block_size': 4096, u'options': u'rw,seclabel,relatime,stripe=256,data=ordered'}) => {
   
"ansible_facts": {
       
"_raw_params": "myhostone_/ihs: 28.5"
   
},
   
"ansible_loop_var": "item",
   
"changed": false,
   
"item": {
       
"block_available": 10009666,
       
"block_size": 4096,
       
"block_total": 12866680,
       
"block_used": 2857014,
       
"device": "/dev/mapper/ihs_vg_yt",
       
"fstype": "ext4",
       
"inode_available": 3264353,
       
"inode_total": 3276800,
       
"inode_used": 12447,
       
"mount": "/ihs",
       
"options": "rw,seclabel,relatime,stripe=256,data=ordered",
       
"size_available": 40999591936,
       
"size_total": 52701921280,
       
"uuid": "3fa1ec29-aca5-476b-8041-6a7bc6b1efc2"


Can you please tell me why is the percentage of the used disk shows 28.5% with ansible while the df -k shows only 19% used? How can I get the correct usage i.e 19% to s

Kai Stian Olstad

unread,
Mar 25, 2020, 12:40:24 PM3/25/20
to ansible...@googlegroups.com
On Wed, Mar 25, 2020 at 07:15:51AM -0700, Shifa Shaikh wrote:
>
> "{{ ansible_host }}_{{ item.mount }}: {{ (100 * ((item.size_total - item.size_available) / item.size_available)) | round(1, 'common') }}"

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

--
Kai Stian Olstad

Shifa Shaikh

unread,
Mar 25, 2020, 12:54:07 PM3/25/20
to Ansible Project
@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?

klingac

unread,
Mar 25, 2020, 1:49:18 PM3/25/20
to Ansible Project
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):

Dan Linder

unread,
Mar 25, 2020, 1:56:22 PM3/25/20
to Ansible Project
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.

Kai Stian Olstad

unread,
Mar 25, 2020, 3:11:00 PM3/25/20
to ansible...@googlegroups.com
On Wed, Mar 25, 2020 at 09:54:07AM -0700, Shifa Shaikh wrote:
> @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?

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.


--
Kai Stian Olstad

patrick patra

unread,
Mar 25, 2020, 3:28:15 PM3/25/20
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200325191042.cbd5wu7asdjw5o2z%40olstad.com.

Kai Stian Olstad

unread,
Mar 25, 2020, 3:56:39 PM3/25/20
to ansible...@googlegroups.com
On Wed, Mar 25, 2020 at 09:27:40PM +0200, patrick patra wrote:
> 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.

--
Kai Stian Olstad

Shifa Shaikh

unread,
Mar 26, 2020, 2:09:07 AM3/26/20
to Ansible Project
Thank you @Kai for the excellent explanation :)
Reply all
Reply to author
Forward
0 new messages