-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new filter human_bytes: convert a string (ex: 1Mo, 1K) into bytes #12074
Conversation
9f9c8fd
to
20571a1
Compare
What is the use case for this filter? |
@amenonsen I wrote several examples in test/integration/roles/test_filters/tasks/main.yml - 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" I'm using this with getmountfrompath (#12066) to check if I have enough space on a given path before trying to do something on it. This way, I can avoid filesystem full. Here's an example of possible combination: - 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 If you wan't, I can rework my PR to resolv conflicts against last version of Ansible. |
I did see the examples, but didn't find |
Is there's any way to make a survey and get some feedback? |
I guess you could do worse than to ask on the mailing list, https://groups.google.com/forum/#!forum/ansible-project I'm assuming that it's only a matter of time before someone comes along with a requirement for k representing 10³ rather than 2¹⁰, so we should probably pick a name that accommodates that. How about |
return pretty_bytes(size, isbits, unit) | ||
|
||
def string_to_bytes(self, size, default_unit = None): | ||
return string_to_bytes(size, default_unit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a problem with _check_type_bytes simply calling the toplevel string_to_bytes(); no need for this method. (Wish we could do the same with pretty_bytes() but backwards compat... )
@Yannig we recently merged something similar from @bcoca. But when bcoca saw this he commented that he liked your code better. If you'd like to update this PR against what we have in the repo now we can look at merging it. (If no one responds after updating, feel free to ping on IRC -- I'm abadger1999) |
@abadger I'll take a look. |
New filter human_to_bytes.
2740369
to
e5a5363
Compare
@abadger ready for review |
https://app.shippable.com/runs/57bd83031419e20f001b3adc/30/tests <= seems like it breaks the usage in facts.py |
base = 'bits' | ||
suffix = '' | ||
|
||
for suffix, limit in sorted(SIZE_RANGES.iteritems(), key=lambda item: -item[1]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to change this for python3 compatibility. We've imported the iteritems function from six earlier so we can just do this::
for suffix, limit in sorted(iteritems(SIZE_RANGES), key=lambda item: -item[1]):
It's breakage when run on python3. I added a line note on how to fix it. |
@abadger: I was not aware of this change in python 3. Fixed ! |
lgtm, +1 |
merged! Thanks @Yannig for the PR and everyone that's taken the time to review! |
…sible#12074) * Rework human_readable and human_to_bytes. New filter human_to_bytes. * Fix for python 3.
Thanks for this great filter. I just used it to set quotas for Ceph pools.
|
This PR replace a part of this one: #11896
Add human_bytes filter. This filter take an input string and convert it into bytes. Here's some examples:
As a side note, add tests for human_readable and human_bytes filters in test_filters.yml playbook.