Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #37881 for 2.5.1 #37937

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- filters - Don't overwrite builtin jinja2 filters with tests (https://github.com/ansible/ansible/pull/37881)
2 changes: 1 addition & 1 deletion lib/ansible/playbook/conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def generic_visit(self, node, inside_call=False, inside_yield=False):
)
try:
e = templar.environment.overlay()
e.filters.update(templar._get_filters())
e.filters.update(templar._get_filters(e.filters))
e.tests.update(templar._get_tests())

res = e._parse(conditional, None, None)
Expand Down
7 changes: 5 additions & 2 deletions lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def __init__(self, loader, shared_loader_obj=None, variables=None):
))
self._no_type_regex = re.compile(r'.*\|\s*(?:%s)\s*(?:%s)?$' % ('|'.join(C.STRING_TYPE_FILTERS), self.environment.variable_end_string))

def _get_filters(self):
def _get_filters(self, builtin_filters):
'''
Returns filter plugins, after loading and caching them if need be
'''
Expand All @@ -308,6 +308,9 @@ def _get_filters(self):

# TODO: Remove registering tests as filters in 2.9
for name, func in self._get_tests().items():
if name in builtin_filters:
# If we have a custom test named the same as a builtin filter, don't register as a filter
continue
self._filters[name] = tests_as_filters_warning(name, func)

return self._filters.copy()
Expand Down Expand Up @@ -681,7 +684,7 @@ def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=
setattr(myenv, key, ast.literal_eval(val.strip()))

# Adds Ansible custom filters and tests
myenv.filters.update(self._get_filters())
myenv.filters.update(self._get_filters(myenv.filters))
myenv.tests.update(self._get_tests())

if escape_backslashes:
Expand Down
7 changes: 6 additions & 1 deletion test/units/template/test_tests_as_filters_warning.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from ansible.template import Templar, display
from units.mock.loader import DictDataLoader
from jinja2.filters import FILTERS
from os.path import isabs


def test_tests_as_filters_warning(mocker):
fake_loader = DictDataLoader({
"/path/to/my_file.txt": "foo\n",
})
templar = Templar(loader=fake_loader, variables={})
filters = templar._get_filters()
filters = templar._get_filters(templar.environment.filters)

mocker.patch.object(display, 'deprecated')

Expand All @@ -28,3 +30,6 @@ def test_tests_as_filters_warning(mocker):
display.deprecated.reset_mock()
filters['bool'](True)
assert display.deprecated.call_count == 0

# Ensure custom test does not override builtin filter
assert filters.get('abs') != isabs