Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
68808854
Commit
68808854
authored
Jul 07, 2015
by
Brian Coca
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new human_readable filter to transform bits and bytes into cake (not really)
parent
08fcd823
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
0 deletions
+37
-0
lib/ansible/plugins/filter/mathstuff.py
+29
-0
test/integration/roles/test_filters/tasks/main.yml
+8
-0
No files found.
lib/ansible/plugins/filter/mathstuff.py
View file @
68808854
...
@@ -101,6 +101,32 @@ def inversepower(x, base=2):
...
@@ -101,6 +101,32 @@ def inversepower(x, base=2):
raise
errors
.
AnsibleFilterError
(
'root() can only be used on numbers:
%
s'
%
str
(
e
))
raise
errors
.
AnsibleFilterError
(
'root() can only be used on numbers:
%
s'
%
str
(
e
))
def
human_readable
(
size
,
isbits
=
False
,
unit
=
None
):
base
=
'bits'
if
isbits
else
'Bytes'
suffix
=
''
ranges
=
(
(
1
<<
70L
,
'Z'
),
(
1
<<
60L
,
'E'
),
(
1
<<
50L
,
'P'
),
(
1
<<
40L
,
'T'
),
(
1
<<
30L
,
'G'
),
(
1
<<
20L
,
'M'
),
(
1
<<
10L
,
'K'
),
(
1
,
base
)
)
for
limit
,
suffix
in
ranges
:
if
(
unit
is
None
and
size
>=
limit
)
or
\
unit
is
not
None
and
unit
.
upper
()
==
suffix
:
break
if
limit
!=
1
:
suffix
+=
base
[
0
]
return
'
%.2
f
%
s'
%
(
float
(
size
)
/
limit
,
suffix
)
class
FilterModule
(
object
):
class
FilterModule
(
object
):
''' Ansible math jinja2 filters '''
''' Ansible math jinja2 filters '''
...
@@ -123,4 +149,7 @@ class FilterModule(object):
...
@@ -123,4 +149,7 @@ class FilterModule(object):
'symmetric_difference'
:
symmetric_difference
,
'symmetric_difference'
:
symmetric_difference
,
'union'
:
union
,
'union'
:
union
,
# computer theory
'human_readable'
:
human_readable
,
}
}
test/integration/roles/test_filters/tasks/main.yml
View file @
68808854
...
@@ -41,3 +41,11 @@
...
@@ -41,3 +41,11 @@
that
:
that
:
-
'
diff_result.stdout
==
""'
-
'
diff_result.stdout
==
""'
-
name
:
Verify human_readable
assert
:
that
:
-
'
"10.00
KB"
==
10240|human_readable'
-
'
"97.66
MB"
==
102400000|human_readable'
-
'
"0.10
GB"
==
102400000|human_readable(unit="G")'
-
'
"0.10
Gb"
==
102400000|human_readable(isbits=True,
unit="G")'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment