Incorrect used disk space percentage when calculated using Ansible

666 views Asked by At

Can you please tell me why is the percentage of a used drive (disk space) shows 28.5% with ansible while the df -k shows only 19% used on remote Linux host?

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 }}'

Output of my playbook run:

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"

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

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?

If I try to divide by item.size_total instead of item.size_available

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

I get 22.2 % as the output which is still not the same as 19%. Can you please explain why the difference in numbers?

1

There are 1 answers

0
rolf82 On BEST ANSWER

The question is not really about ansible as you would have the same problem with manual calculation.

To get the exact same percentage as df, you'll have to add the reserved bloc count of the partition. You can get this value for example with tune2fs. In your case:

tune2fs -l /dev/mapper/ihs_vg_yt | grep 'Reserved block count'

Then if you do (block_used + reserved_block_count)*100 / block_total you should get the right value.