From 6185f811d7f8a810ebe9f437e9f8f2daa69a46ce Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 6 Oct 2022 14:39:44 +0100 Subject: [PATCH] Ensure we detect templating errors (#2558) * Ensure we detect templating errors As https://github.com/ansible/ansible/pull/78913 changed behavior of Ansible on missing filters, we adapted the check so it would continue to work with newer versions. * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/ansiblelint/runner.py | 1 + src/ansiblelint/utils.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ansiblelint/runner.py b/src/ansiblelint/runner.py index fd0354a3fa..681701647c 100644 --- a/src/ansiblelint/runner.py +++ b/src/ansiblelint/runner.py @@ -188,6 +188,7 @@ def worker(lintable: Lintable) -> list[MatchError]: matches = list( filter( lambda match: not self.is_excluded(Lintable(match.filename)) + and hasattr(match, "lintable") and match.tag not in match.lintable.line_skips[match.linenumber], matches, ) diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index 1056848422..0569666d9e 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -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