Add jinja2 groupby filter override to cast namedtuple to tuple. Fixes #20098 (#20362)

* Add jinja2 groupby filter override to cast namedtuple to tuple. Fixes #20098

* Address some of the requested changes

* Quoting

* Print the python path and version

* Be less explicitly verbose, rely on implicit verbosity
This commit is contained in:
Matt Martz 2017-01-19 13:39:59 -06:00 committed by Toshio Kuratomi
parent 0240ffe220
commit 7164956cc6

View file

@ -37,7 +37,7 @@ from datetime import datetime
import uuid
import yaml
from jinja2.filters import environmentfilter
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
try:
import passlib.hash
@ -431,11 +431,35 @@ def skipped(*a, **kw):
skipped = item.get('skipped', False)
return skipped
@environmentfilter
def do_groupby(environment, value, attribute):
"""Overridden groupby filter for jinja2, to address an issue with
jinja2>=2.9.0,<2.9.5 where a namedtuple was returned which
has repr that prevents ansible.template.safe_eval.safe_eval from being
able to parse and eval the data.
jinja2<2.9.0,>=2.9.5 is not affected, as <2.9.0 uses a tuple, and
>=2.9.5 uses a standard tuple repr on the namedtuple.
The adaptation here, is to run the jinja2 `do_groupby` function, and
cast all of the namedtuples to a regular tuple.
See https://github.com/ansible/ansible/issues/20098
We may be able to remove this in the future.
"""
return [tuple(t) for t in _do_groupby(environment, value, attribute)]
class FilterModule(object):
''' Ansible core jinja2 filters '''
def filters(self):
return {
# jinja2 overrides
'groupby': do_groupby,
# base 64
'b64decode': partial(unicode_wrap, base64.b64decode),
'b64encode': partial(unicode_wrap, base64.b64encode),