Skip to content

Commit

Permalink
role-name[path]: Now detects use of paths when including roles
Browse files Browse the repository at this point in the history
Fixes: #2502
  • Loading branch information
ssbarnea committed Oct 31, 2022
1 parent d941313 commit 909f204
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/roles/subfolder/other_role/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: Foo
debug:
msg: "Hello!"
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"name[play]",
"role-name",
"warning[empty-playbook]", # because ansible considers it warning only
"role-name[path]", # too new
]

DEFAULT_KINDS = [
Expand Down
6 changes: 4 additions & 2 deletions src/ansiblelint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ def main():
}

ROLE_IMPORT_ACTION_NAMES = {
"import_role",
"ansible.builtin.import_role",
"include_role",
"ansible.builtin.include_role",
"ansible.legacy.import_role",
"ansible.legacy.include_role",
"import_role",
"include_role",
}
4 changes: 4 additions & 0 deletions src/ansiblelint/rules/role_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Role names must also start with an alphabetic character.

For more information see the [roles directory](https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_structure.html#roles-directory) topic in Ansible documentation.

`role-name[path]` message tells you to avoid using paths when importing roles.
You should only rely on Ansible's ability to find the role and refer to them
using fully qualified names.

## Problematic Code

```yaml
Expand Down
23 changes: 21 additions & 2 deletions src/ansiblelint/rules/role_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import re
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import ROLE_IMPORT_ACTION_NAMES
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import parse_yaml_from_file
Expand Down Expand Up @@ -53,12 +54,30 @@ class RoleNames(AnsibleLintRule):
severity = "HIGH"
done: list[str] = [] # already noticed roles list
tags = ["deprecations", "metadata"]
version_added = "v4.3.0"
version_added = "v6.8.5"

def __init__(self) -> None:
"""Save precompiled regex."""
self._re = re.compile(ROLE_NAME_REGEX)

def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
) -> list[MatchError]:
results = []
if task["action"]["__ansible_module__"] in ROLE_IMPORT_ACTION_NAMES:
name = task["action"].get("name", "")
# breakpoint()
if "/" in name:
results.append(
self.create_matcherror(
"Avoid using paths when importing roles.",
filename=file,
linenumber=task["__line__"],
tag=f"{self.id}[path]",
)
)
return results

def matchdir(self, lintable: Lintable) -> list[MatchError]:
return self.matchyaml(lintable)

Expand Down

0 comments on commit 909f204

Please sign in to comment.