Skip to content

Commit

Permalink
Determine if passed arguments are playbooks or not
Browse files Browse the repository at this point in the history
From now on, ansible-lint will no longer assume that a passed argument
must be a playbook. This addressed multiple bug reports where people
were confused that the linter reported errors when they passed a
taskfile as an argument.

The downside is that an invalid playbook that is a valid YAML file
might not raise an error and just report a warning and be treated as
a generic yaml file.
  • Loading branch information
ssbarnea committed Jan 18, 2023
1 parent 5e04de2 commit d1d174a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ robertdebock
rolepath
roundtrip
ruamel
rulebook
rulebooks
ruledirs
rulesdir
rulesdirs
Expand Down
21 changes: 21 additions & 0 deletions examples/playbooks/rulebook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# That file is not a valid playbook but it is a valid rulebook that was
# mistakenly put under a playbook directory.
- name: Demo rules with kafka as source
hosts: localhost
sources:
- name: kafka
kafka:
topic: eda
host: localhost
port: 9092
group_id: testing
rules:
- name:
condition: event.i is defined
action:
debug:
- name:
condition: event.stop == true
action:
shutdown:
19 changes: 19 additions & 0 deletions examples/rulebooks/rulebook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Demo rules with kafka as source
hosts: localhost
sources:
- name: kafka
kafka:
topic: eda
host: localhost
port: 9092
group_id: testing
rules:
- name:
condition: event.i is defined
action:
debug:
- name:
condition: event.stop == true
action:
shutdown:
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
{"galaxy": "**/galaxy.yml"}, # Galaxy collection meta
{"reno": "**/releasenotes/*/*.{yaml,yml}"}, # reno release notes
{"tasks": "**/tasks/**/*.{yaml,yml}"},
{"rulebook": "**/rulebooks/*.{yml,yaml"},
{"playbook": "**/playbooks/*.{yml,yaml}"},
{"playbook": "**/*playbook*.{yml,yaml}"},
{"role": "**/roles/*/"},
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def main():

FileType = Literal[
"playbook",
"rulebook",
"meta", # role meta
"meta-runtime",
"tasks", # includes pre_tasks, post_tasks
Expand Down
18 changes: 18 additions & 0 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ def __init__(
self.base_kind = base_kind or kind_from_path(self.path, base=True)
self.abspath = self.path.expanduser().absolute()

if self.kind in ("yaml", None):
self._guess_kind()

def _guess_kind(self) -> None:
if self.kind == "yaml":
if isinstance(self.data, list) and "hosts" in self.data[0]:
if "rules" not in self.data[0]:
self.kind = "playbook"
else:
self.kind = "rulebook"
# we we failed to guess the more specific kind, we warn user
if self.kind == "yaml":
_logger.warning(
"Passed '%s' positional argument was identified as generic '%s' file kind.",
self.name,
self.kind,
)

def __getitem__(self, key: Any) -> Any:
"""Provide compatibility subscriptable support."""
if key == "path":
Expand Down
8 changes: 0 additions & 8 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,6 @@ def get_lintables(
if args:
for arg in args:
lintable = Lintable(arg)
if lintable.kind in ("yaml", None):
_logger.warning(
"Overriding detected file kind '%s' with 'playbook' "
"for given positional argument: %s",
lintable.kind,
arg,
)
lintable = Lintable(arg, kind="playbook")
lintables.append(lintable)
else:

Expand Down

0 comments on commit d1d174a

Please sign in to comment.