diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index a959313085..8b1e2c2499 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -166,7 +166,7 @@ jobs: WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 701 + PYTEST_REQPASS: 702 steps: - name: Activate WSL1 diff --git a/examples/playbooks/name-templated.yml b/examples/playbooks/name-templated.yml new file mode 100644 index 0000000000..1a089912fe --- /dev/null +++ b/examples/playbooks/name-templated.yml @@ -0,0 +1,10 @@ +--- +- name: Fixture for src/ansiblelint/rules/name.py::test_name_template( + hosts: all + tasks: + - name: This task {{ sampleService }} name is not correctly templated + ansible.builtin.command: echo "Hello World" + changed_when: false + - name: This task is correctly templated {{ sampleService }} + ansible.builtin.command: echo "Hello World" + changed_when: false diff --git a/src/ansiblelint/rules/name.md b/src/ansiblelint/rules/name.md index 1e9cbc3daa..67fa711a23 100644 --- a/src/ansiblelint/rules/name.md +++ b/src/ansiblelint/rules/name.md @@ -10,6 +10,10 @@ This rule can produce messages such: languages that support it. - `name[missing]` - All tasks should be named. - `name[play]` - All plays should be named. +- `name[template]` - Jinja templates should only be at the end of 'name'. This + helps with the identification of tasks inside the source code when they fail. + The use of templating inside `name` keys is discouraged as there + are multiple cases where the rendering of the name template is not possible. If you want to ignore some of the messages above, you can add any of them to the `skip_list`. diff --git a/src/ansiblelint/rules/name.py b/src/ansiblelint/rules/name.py index 9d03173db1..5446337e2e 100644 --- a/src/ansiblelint/rules/name.py +++ b/src/ansiblelint/rules/name.py @@ -1,6 +1,7 @@ """Implementation of NameRule.""" from __future__ import annotations +import re import sys from typing import TYPE_CHECKING, Any @@ -23,6 +24,7 @@ class NameRule(AnsibleLintRule): severity = "MEDIUM" tags = ["idiom"] version_added = "v6.5.0 (last update)" + _re_templated_inside = re.compile(r".*\{\{.*\}\}(.+)$") def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: """Return matches found for a specific play (entry in playbook).""" @@ -81,6 +83,15 @@ def _check_name( filename=lintable, ) ) + if self._re_templated_inside.match(name): + results.append( + self.create_matcherror( + message="Jinja templates should only be at the end of 'name'", + linenumber=linenumber, + tag="name[template]", + filename=lintable, + ) + ) return results @@ -126,3 +137,13 @@ def test_name_play() -> None: assert len(errs) == 1 assert errs[0].tag == "name[play]" assert errs[0].rule.id == "name" + + def test_name_template() -> None: + """Negative test for name[templated].""" + collection = RulesCollection() + collection.register(NameRule()) + failure = "examples/playbooks/name-templated.yml" + bad_runner = Runner(failure, rules=collection) + errs = bad_runner.run() + assert len(errs) == 1 + assert errs[0].tag == "name[template]"