From 7a43d4005026234cc8227147387d4782e2289d9e Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 16 Dec 2014 17:47:50 -0500 Subject: [PATCH 1/3] math filters! --- docsite/rst/playbooks_variables.rst | 39 +++++++++ lib/ansible/runner/filter_plugins/math.py | 96 +++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 lib/ansible/runner/filter_plugins/math.py diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index 3a522613607..e42fdce22d2 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -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: diff --git a/lib/ansible/runner/filter_plugins/math.py b/lib/ansible/runner/filter_plugins/math.py new file mode 100644 index 00000000000..f49635af721 --- /dev/null +++ b/lib/ansible/runner/filter_plugins/math.py @@ -0,0 +1,96 @@ +# (c) 2014, Brian Coca +# +# 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 . + +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, + } From 6a3c26eb7022af5e78e23b44df738083d459a7a6 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 16 Dec 2014 17:49:32 -0500 Subject: [PATCH 2/3] removed stray j --- docsite/rst/playbooks_variables.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index e42fdce22d2..a341fa44e73 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -310,7 +310,7 @@ 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: From b07ce8b942d1d659257b0aebb6a17e1425e583d1 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 16 Dec 2014 17:57:21 -0500 Subject: [PATCH 3/3] removed redundant math functions as jinja2 provides abs() and round() already --- docsite/rst/playbooks_variables.rst | 10 ++------ lib/ansible/runner/filter_plugins/math.py | 29 ----------------------- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index a341fa44e73..c272f160a5f 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -318,19 +318,11 @@ 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 }} @@ -349,6 +341,8 @@ Square root, or the 5th:: {{ myvar | root }} {{ myvar | root(5) }} +Note that jinja2 already provides some like abs() and round(). + .. _other_useful_filters: diff --git a/lib/ansible/runner/filter_plugins/math.py b/lib/ansible/runner/filter_plugins/math.py index f49635af721..ce01ae573b5 100644 --- a/lib/ansible/runner/filter_plugins/math.py +++ b/lib/ansible/runner/filter_plugins/math.py @@ -18,30 +18,6 @@ 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) @@ -82,13 +58,8 @@ class FilterModule(object): def filters(self): return { # general math - 'abs': absolute, 'isnan': isnotanumber, - # rounding - 'ceil': cieling, - 'floor': flooring, - # exponents and logarithms 'log': logarithm, 'pow': power,