getmountfrompath and human to bytes converter filter name survey

Hi all,

Recently, I was talking with amenonsen about a PR with the aim to provide a way to convert a string into bytes. You can check it out at the following location: https://github.com/ansible/ansible/pull/12074

Here is a simple example:

- name: Verify human_bytes
  tags: "human_bytes"
  assert:
    that:
        - "{{'0'|human_bytes}}        == 0"
        - "{{'0.1'|human_bytes}}      == 0"
        - "{{'0.9'|human_bytes}}      == 1"
        - "{{'1'|human_bytes}}        == 1"
        - "{{'10.00 KB'|human_bytes}} == 10240"
        - "{{   '11 MB'|human_bytes}} == 11534336"
        - "{{  '1.1 GB'|human_bytes}} == 1181116006"

The filter is not quite usefull but combined with another PR of mine (https://github.com/ansible/ansible/pull/12066), it can help people to retrieve available space of a given path. Here’s an example showing a way to use it in order to check available space on /tmp before trying to do anything else:

- name: "Check /tmp space"
  hosts: localhost
  tasks:
    - debug: msg={{'/tmp'|getmountfrompath(ansible_mounts)}}
    - name: "Check there's enough space"
      fail: msg="Not enough space available"
      when: ('/tmp'|getmountfrompath(ansible_mounts)).size_available < '1M' | human_bytes

Actually, amenonsen found that human_bytes is not a very good name and that we may found better alternative. So here we are people, what do you think about? Any advice?

What it’s really doing is converting bytes (including in abbreviated formats, like “1 GB”) to the full integer representation of number of bytes. So, yeah, I’d call it something like bytes_to_int.

Here is something I wrote to do something similar as well as the other way around. I called mine ‘human’ to be consistent with options like ‘ls -hal’ and ‘df -h’ where the ‘-h’ is for ‘human readable’.

`
def fmtsize(val,targ,case=‘lower’,base=10):
‘’’
fmtsize will take a string representing a size and convert it to or from
human-readable format.

Args:
val (str): The value to convert. Usually, this passed via pipe.
targ (str): The target style for conversion.

Options: [ ‘human’, ‘raw’ ] ## ‘float’ (not yet implemented)

case (Optional[str]): The case of the human style result to be returned. Defaults to ‘lower’.

Options: [ ‘upper’, ‘lower’ ]

base (Optional[int]): Specify the numerical base used in calculations. Defaults to 10.

e.g. if base == 10, 1k = 1000;
if base == 2, 1k = 1024

Returns:
str: (if targ==‘human’)
e.g. 100g, 10m, 1k, etc

Returns:
int: (if targ==‘raw’)
e.g. 100000000000, 10000000, 1000, etc

Example:
Playbook Example::