Add optional attribute arg for min and max filters (#50909)
* Pass **kwargs to min and max filters * Use the jinja2 filters if available * Add unit tests * Add examples to docs passing attribute
This commit is contained in:
parent
709484969c
commit
3db08adbb1
4 changed files with 52 additions and 12 deletions
2
changelogs/fragments/50909-min-max-attrs.yml
Normal file
2
changelogs/fragments/50909-min-max-attrs.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- Allow an attribute to be passed to the min and max filters with Jinja 2.10+
|
|
@ -882,10 +882,22 @@ To get the minimum value from list of numbers::
|
||||||
|
|
||||||
{{ list1 | min }}
|
{{ list1 | min }}
|
||||||
|
|
||||||
|
.. versionadded:: 2.11
|
||||||
|
|
||||||
|
To get the minimum value in a list of objects::
|
||||||
|
|
||||||
|
{{ [{'val': 1}, {'val': 2}] | min(attribute='val') }}
|
||||||
|
|
||||||
To get the maximum value from a list of numbers::
|
To get the maximum value from a list of numbers::
|
||||||
|
|
||||||
{{ [3, 4, 2] | max }}
|
{{ [3, 4, 2] | max }}
|
||||||
|
|
||||||
|
.. versionadded:: 2.11
|
||||||
|
|
||||||
|
To get the maximum value in a list of objects::
|
||||||
|
|
||||||
|
{{ [{'val': 1}, {'val': 2}] | max(attribute='val') }}
|
||||||
|
|
||||||
.. versionadded:: 2.5
|
.. versionadded:: 2.5
|
||||||
|
|
||||||
Flatten a list (same thing the `flatten` lookup does)::
|
Flatten a list (same thing the `flatten` lookup does)::
|
||||||
|
|
|
@ -42,6 +42,12 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_UNIQUE = False
|
HAS_UNIQUE = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
from jinja2.filters import do_max, do_min
|
||||||
|
HAS_MIN_MAX = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_MIN_MAX = False
|
||||||
|
|
||||||
display = Display()
|
display = Display()
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,14 +129,28 @@ def union(environment, a, b):
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
def min(a):
|
@environmentfilter
|
||||||
_min = __builtins__.get('min')
|
def min(environment, a, **kwargs):
|
||||||
return _min(a)
|
if HAS_MIN_MAX:
|
||||||
|
return do_min(environment, a, **kwargs)
|
||||||
|
else:
|
||||||
|
if kwargs:
|
||||||
|
raise AnsibleFilterError("Ansible's min filter does not support any keyword arguments. "
|
||||||
|
"You need Jinja2 2.10 or later that provides their version of the filter.")
|
||||||
|
_min = __builtins__.get('min')
|
||||||
|
return _min(a)
|
||||||
|
|
||||||
|
|
||||||
def max(a):
|
@environmentfilter
|
||||||
_max = __builtins__.get('max')
|
def max(environment, a, **kwargs):
|
||||||
return _max(a)
|
if HAS_MIN_MAX:
|
||||||
|
return do_max(environment, a, **kwargs)
|
||||||
|
else:
|
||||||
|
if kwargs:
|
||||||
|
raise AnsibleFilterError("Ansible's max filter does not support any keyword arguments. "
|
||||||
|
"You need Jinja2 2.10 or later that provides their version of the filter.")
|
||||||
|
_max = __builtins__.get('max')
|
||||||
|
return _max(a)
|
||||||
|
|
||||||
|
|
||||||
def logarithm(x, base=math.e):
|
def logarithm(x, base=math.e):
|
||||||
|
|
|
@ -64,16 +64,22 @@ class TestSymmetricDifference:
|
||||||
|
|
||||||
class TestMin:
|
class TestMin:
|
||||||
def test_min(self):
|
def test_min(self):
|
||||||
assert ms.min((1, 2)) == 1
|
assert ms.min(env, (1, 2)) == 1
|
||||||
assert ms.min((2, 1)) == 1
|
assert ms.min(env, (2, 1)) == 1
|
||||||
assert ms.min(('p', 'a', 'w', 'b', 'p')) == 'a'
|
assert ms.min(env, ('p', 'a', 'w', 'b', 'p')) == 'a'
|
||||||
|
assert ms.min(env, ({'key': 'a'}, {'key': 'b'}, {'key': 'c'}), attribute='key') == {'key': 'a'}
|
||||||
|
assert ms.min(env, ({'key': 1}, {'key': 2}, {'key': 3}), attribute='key') == {'key': 1}
|
||||||
|
assert ms.min(env, ('a', 'A', 'b', 'B'), case_sensitive=True) == 'A'
|
||||||
|
|
||||||
|
|
||||||
class TestMax:
|
class TestMax:
|
||||||
def test_max(self):
|
def test_max(self):
|
||||||
assert ms.max((1, 2)) == 2
|
assert ms.max(env, (1, 2)) == 2
|
||||||
assert ms.max((2, 1)) == 2
|
assert ms.max(env, (2, 1)) == 2
|
||||||
assert ms.max(('p', 'a', 'w', 'b', 'p')) == 'w'
|
assert ms.max(env, ('p', 'a', 'w', 'b', 'p')) == 'w'
|
||||||
|
assert ms.max(env, ({'key': 'a'}, {'key': 'b'}, {'key': 'c'}), attribute='key') == {'key': 'c'}
|
||||||
|
assert ms.max(env, ({'key': 1}, {'key': 2}, {'key': 3}), attribute='key') == {'key': 3}
|
||||||
|
assert ms.max(env, ('a', 'A', 'b', 'B'), case_sensitive=True) == 'b'
|
||||||
|
|
||||||
|
|
||||||
class TestLogarithm:
|
class TestLogarithm:
|
||||||
|
|
Loading…
Reference in a new issue