diff --git a/changelogs/fragments/du_size.yml b/changelogs/fragments/du_size.yml new file mode 100644 index 00000000000..064aabf1610 --- /dev/null +++ b/changelogs/fragments/du_size.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - file - return disk_usage_bytes fact (https://github.com/ansible/ansible/issues/70834). + - stat - return disk_usage_bytes fact (https://github.com/ansible/ansible/issues/70834). diff --git a/lib/ansible/modules/find.py b/lib/ansible/modules/find.py index 970eb0cf929..2cdfac5a2f4 100644 --- a/lib/ansible/modules/find.py +++ b/lib/ansible/modules/find.py @@ -430,7 +430,7 @@ def statinfo(st): except Exception: pass - return { + output = { 'mode': "%04o" % stat.S_IMODE(st.st_mode), 'isdir': stat.S_ISDIR(st.st_mode), 'ischr': stat.S_ISCHR(st.st_mode), @@ -463,6 +463,12 @@ def statinfo(st): 'isgid': bool(st.st_mode & stat.S_ISGID), } + if hasattr(st, 'st_blocks'): + output['blocks'] = st.st_blocks + output['disk_usage_bytes'] = st.st_blocks * 512 + + return output + def main(): module = AnsibleModule( diff --git a/lib/ansible/modules/stat.py b/lib/ansible/modules/stat.py index a1eb14e4030..2a68d4a3d89 100644 --- a/lib/ansible/modules/stat.py +++ b/lib/ansible/modules/stat.py @@ -366,6 +366,12 @@ stat: type: str sample: "381700746" version_added: 2.3 + disk_usage_bytes: + description: The disk usage of a path in bytes + returned: success, path exists, user can execute the path, filesystem supports st_blocks + type: int + sample: 1024 + version_added: '2.21' """ import grp @@ -436,6 +442,8 @@ def format_output(module, path, st): ]: if hasattr(st, other[0]): output[other[1]] = getattr(st, other[0]) + if other[0] == 'st_blocks': + output['disk_usage_bytes'] = st.st_blocks * 512 return output diff --git a/test/integration/targets/stat/tasks/main.yml b/test/integration/targets/stat/tasks/main.yml index a79ca9195e5..4563f34df06 100644 --- a/test/integration/targets/stat/tasks/main.yml +++ b/test/integration/targets/stat/tasks/main.yml @@ -61,6 +61,7 @@ - "'xgrp' in stat_result.stat" - "'xoth' in stat_result.stat" - "'xusr' in stat_result.stat" + - "'disk_usage_bytes' in stat_result.stat" - name: make a symlink file: