Skip to content

Commit

Permalink
Ensure we detect templating errors
Browse files Browse the repository at this point in the history
As ansible/ansible#78913 changed behavior
of Ansible on missing filters, we adapted the check so it would
continue to work with newer versions.
  • Loading branch information
ssbarnea committed Oct 6, 2022
1 parent b3e26a7 commit 06b5257
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def worker(lintable: Lintable) -> list[MatchError]:
matches = list(
filter(
lambda match: not self.is_excluded(Lintable(match.filename))
and match.tag not in match.lintable.line_skips[match.linenumber],
and hasattr(match, "lintable") and match.tag not in match.lintable.line_skips[match.linenumber],
matches,
)
)
Expand Down
11 changes: 8 additions & 3 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,24 @@ def ansible_template(
) -> Any:
"""Render a templated string by mocking missing filters."""
templar = ansible_templar(basedir=basedir, templatevars=templatevars)
while True:
# pylint: disable=unused-variable
for i in range(3):
try:
return templar.template(varname, **kwargs)
except AnsibleError as exc:
if exc.message.startswith(
"template error while templating string: No filter named"
if (
exc.message.startswith("template error while templating string:")
and "'" in exc.message
):
missing_filter = exc.message.split("'")[1]
if missing_filter == "end of print statement":
raise
# Mock the filter to avoid and error from Ansible templating
# pylint: disable=protected-access
templar.environment.filters._delegatee[missing_filter] = lambda x: x
# Record the mocked filter so we can warn the user
if missing_filter not in options.mock_filters:
_logger.debug("Mocking missing filter %s", missing_filter)
options.mock_filters.append(missing_filter)
continue
raise
Expand Down

0 comments on commit 06b5257

Please sign in to comment.