Skip to content

Commit 7258dba

Browse files
vineethreddykaturuvineethreddykaturupre-commit-ci[bot]ssbarnea
authored
name[template]: recommend to use templating as suffix on names (#2483)
* add named templated * added named template play book * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed code Co-authored-by: vineethreddykaturu <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea <[email protected]> Co-authored-by: Sorin Sbarnea <[email protected]>
1 parent e248b64 commit 7258dba

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ jobs:
166166
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
167167
# Number of expected test passes, safety measure for accidental skip of
168168
# tests. Update value if you add/remove tests.
169-
PYTEST_REQPASS: 701
169+
PYTEST_REQPASS: 702
170170

171171
steps:
172172
- name: Activate WSL1

examples/playbooks/name-templated.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Fixture for src/ansiblelint/rules/name.py::test_name_template(
3+
hosts: all
4+
tasks:
5+
- name: This task {{ sampleService }} name is not correctly templated
6+
ansible.builtin.command: echo "Hello World"
7+
changed_when: false
8+
- name: This task is correctly templated {{ sampleService }}
9+
ansible.builtin.command: echo "Hello World"
10+
changed_when: false

src/ansiblelint/rules/name.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This rule can produce messages such:
1010
languages that support it.
1111
- `name[missing]` - All tasks should be named.
1212
- `name[play]` - All plays should be named.
13+
- `name[template]` - Jinja templates should only be at the end of 'name'. This
14+
helps with the identification of tasks inside the source code when they fail.
15+
The use of templating inside `name` keys is discouraged as there
16+
are multiple cases where the rendering of the name template is not possible.
1317

1418
If you want to ignore some of the messages above, you can add any of them to
1519
the `skip_list`.

src/ansiblelint/rules/name.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Implementation of NameRule."""
22
from __future__ import annotations
33

4+
import re
45
import sys
56
from typing import TYPE_CHECKING, Any
67

@@ -23,6 +24,7 @@ class NameRule(AnsibleLintRule):
2324
severity = "MEDIUM"
2425
tags = ["idiom"]
2526
version_added = "v6.5.0 (last update)"
27+
_re_templated_inside = re.compile(r".*\{\{.*\}\}(.+)$")
2628

2729
def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
2830
"""Return matches found for a specific play (entry in playbook)."""
@@ -81,6 +83,15 @@ def _check_name(
8183
filename=lintable,
8284
)
8385
)
86+
if self._re_templated_inside.match(name):
87+
results.append(
88+
self.create_matcherror(
89+
message="Jinja templates should only be at the end of 'name'",
90+
linenumber=linenumber,
91+
tag="name[template]",
92+
filename=lintable,
93+
)
94+
)
8495
return results
8596

8697

@@ -126,3 +137,13 @@ def test_name_play() -> None:
126137
assert len(errs) == 1
127138
assert errs[0].tag == "name[play]"
128139
assert errs[0].rule.id == "name"
140+
141+
def test_name_template() -> None:
142+
"""Negative test for name[templated]."""
143+
collection = RulesCollection()
144+
collection.register(NameRule())
145+
failure = "examples/playbooks/name-templated.yml"
146+
bad_runner = Runner(failure, rules=collection)
147+
errs = bad_runner.run()
148+
assert len(errs) == 1
149+
assert errs[0].tag == "name[template]"

0 commit comments

Comments
 (0)