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
7a43d400
Commit
7a43d400
authored
Dec 16, 2014
by
Brian Coca
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
math filters!
parent
fbadcfd4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
135 additions
and
0 deletions
+135
-0
docsite/rst/playbooks_variables.rst
+39
-0
lib/ansible/runner/filter_plugins/math.py
+96
-0
No files found.
docsite/rst/playbooks_variables.rst
View file @
7a43d400
...
...
@@ -310,6 +310,45 @@ To get a random list from an existing list::
{{ ['a','b','c']|shuffle }} => ['b','c','a']
note that when used with a non 'listable' item it is a noop, otherwise it always returns a list
j
.. _math_stuff:
Math
--------------------
.. versionadded:: 1.9
To get the absolute value of a number::
{{ -23 | abs }}
To see if something is actually a number::
{{ myvar | isnan }}
Rounding::
{{ myvar | ceil }}
{{ myvar | floor }}
Get the logarithm (default is e)::
{{ myvar | log }}
Get the base 10 logarithm::
{{ myvar | log(10) }}
Give me the power of 2! (or 5)::
{{ myvar | pow(2) }}
{{ myvar | pow(5) }}
Square root, or the 5th::
{{ myvar | root }}
{{ myvar | root(5) }}
.. _other_useful_filters:
...
...
lib/ansible/runner/filter_plugins/math.py
0 → 100644
View file @
7a43d400
# (c) 2014, Brian Coca <bcoca@ansible.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import
math
from
ansible
import
errors
def
absolute
(
x
):
if
isinstance
(
x
,
float
):
return
math
.
fabs
(
x
)
elif
isinstance
(
x
,
int
):
return
abs
(
x
)
else
raise
errors
.
AnsibleFilterError
(
'abs() can only be used on numbers'
)
def
cieling
(
x
):
try
:
return
math
.
ciel
(
x
)
except
TypeError
,
e
:
raise
errors
.
AnsibleFilterError
(
'ciel() can only be used on floats:
%
s'
%
str
(
e
))
def
flooring
(
x
):
try
:
return
math
.
floor
(
x
)
except
TypeError
,
e
:
raise
errors
.
AnsibleFilterError
(
'floor() can only be used on floats:
%
s'
%
str
(
e
))
def
isnotanumber
(
x
):
try
:
return
math
.
isnan
(
x
)
except
TypeError
,
e
:
return
False
def
logarithm
(
x
,
base
=
math
.
e
):
try
:
if
base
==
10
:
return
math
.
log10
(
x
)
else
:
return
=
math
.
log
(
x
,
base
)
except
TypeError
,
e
:
raise
errors
.
AnsibleFilterError
(
'log() can only be used on numbers:
%
s'
%
str
(
e
))
def
power
(
x
):
try
:
return
math
.
pow
(
x
,
y
)
except
TypeError
,
e
:
raise
errors
.
AnsibleFilterError
(
'pow() can only be used on numbers:
%
s'
%
str
(
e
))
def
inversepower
(
x
,
base
=
2
):
try
:
if
base
==
2
:
return
math
.
sqrt
(
x
)
else
:
return
math
.
pow
(
x
,
1.0
/
float
(
base
))
except
TypeError
,
e
:
raise
errors
.
AnsibleFilterError
(
'root() can only be used on numbers:
%
s'
%
str
(
e
))
class
FilterModule
(
object
):
''' Ansible math jinja2 filters '''
def
filters
(
self
):
return
{
# general math
'abs'
:
absolute
,
'isnan'
:
isnotanumber
,
# rounding
'ceil'
:
cieling
,
'floor'
:
flooring
,
# exponents and logarithms
'log'
:
logarithm
,
'pow'
:
power
,
'root'
:
inversepower
,
}
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