Skip to content

Commit

Permalink
name[template]: recommend to use templating as suffix on names (ansib…
Browse files Browse the repository at this point in the history
…le#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]>
  • Loading branch information
5 people authored and davedittrich committed Sep 27, 2022
1 parent ca58469 commit b18a687
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
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]"

0 comments on commit b18a687

Please sign in to comment.