Skip to content

Commit

Permalink
feat: extend fqcn rule to complain on collections keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgruenhage committed Dec 15, 2022
1 parent 40155c8 commit 81ac8b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ansiblelint/rules/fqcn.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The `fqcn` rule has the following checks:
- `fqcn[action]` - Use FQCN for module actions, such ...
- `fqcn[action-core]` - Checks for FQCNs from the `ansible.legacy` or `ansible.builtin` collection.
- `fqcn[canonical]` - You should use canonical module name ... instead of ...
- `fqcn[keyword]` - You shouldn't use the `collections` keyword, use the FQCNs instead.

```{note}
In most cases you should declare the `ansible.builtin` collection for internal Ansible actions.
Expand Down
15 changes: 15 additions & 0 deletions src/ansiblelint/rules/fqcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ansible.plugins.loader import module_loader

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, RulesCollection
Expand Down Expand Up @@ -159,6 +160,20 @@ def matchtask(
)
return result

def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
if file.kind != "playbook":
return []
if "collections" in data:
return [
self.create_matcherror(
message="Don't use collections keyword.",
linenumber=data[LINE_NUMBER_KEY],
tag="fqcn[keyword]",
filename=file,
)
]
return []


# testing code to be loaded only with pytest or when executed the rule file
if "pytest" in sys.modules:
Expand Down

0 comments on commit 81ac8b2

Please sign in to comment.