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

name[template]: recommend to use templating as suffix on names #2483

Merged
merged 5 commits into from
Sep 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions examples/playbooks/name-templated.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions src/ansiblelint/rules/name.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
21 changes: 21 additions & 0 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of NameRule."""
from __future__ import annotations

import re
import sys
from typing import TYPE_CHECKING, Any

Expand All @@ -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)."""
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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]"