Skip to content

Commit

Permalink
Make import_playbook recognize playbooks from within collections (#4141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored May 9, 2024
1 parent 2f3cb30 commit aaff090
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .config/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile --all-extras --no-annotate --output-file=.config/constraints.txt --strip-extras --unsafe-package=resolvelib --unsafe-package=ruamel-yaml-clib --unsafe-package=wcmatch pyproject.toml
#
ansible-compat==4.1.11
ansible-compat==24.5.1a0
ansible-core==2.16.6
astroid==3.1.0
attrs==23.2.0
Expand Down
3 changes: 2 additions & 1 deletion .config/dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Adrián
Autobuild
audgirka
CLICOLOR
CODENOTIFY
CODEOWNERS
Expand Down Expand Up @@ -45,6 +44,7 @@ apport
argparsing
argspecs
arxcruz
audgirka
auditd
autobuild
autoclass
Expand Down Expand Up @@ -379,6 +379,7 @@ tmpfs
toctree
toidentifier
tomli
tomlsort
toolset
tripleo
tuco
Expand Down
2 changes: 1 addition & 1 deletion .config/requirements-lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile --no-annotate --output-file=.config/requirements-lock.txt --strip-extras --unsafe-package=resolvelib --unsafe-package=ruamel-yaml-clib pyproject.toml
#
ansible-compat==4.1.11
ansible-compat==24.5.1a0
ansible-core==2.16.6
attrs==23.2.0
black==24.4.2
Expand Down
2 changes: 1 addition & 1 deletion .config/requirements.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Special order section for helping pip:
will-not-work-on-windows-try-from-wsl-instead; platform_system=='Windows'
ansible-core>=2.13.0 # GPLv3
ansible-compat>=4.1.11 # GPLv3
ansible-compat>=24.5.0dev0 # GPLv3
# alphabetically sorted:
black>=24.3.0 # MIT (security)
filelock>=3.3.0 # The Unlicense
Expand Down
2 changes: 1 addition & 1 deletion .github/lower-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# automatically updated by dependabot. This should be kept in sync with
# minimal requirements configured inside .config/requirements.in
ansible-core==2.13.0
ansible-compat==4.1.11 # GPLv3
ansible-compat==24.5.1a0 # GPLv3
black==24.3.0 # MIT (security)
filelock==3.3.0 # The Unlicense
jsonschema==4.10.0 # MIT, version needed for improved errors
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 858
PYTEST_REQPASS: 859
steps:
- uses: actions/checkout@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ repos:
# empty args needed in order to match mypy cli behavior
args: [--strict]
additional_dependencies:
- ansible-compat>=4.1.11
- ansible-compat>=24.5.1a0
- black>=22.10.0
- cryptography>=39.0.1
- filelock>=3.12.2
Expand Down Expand Up @@ -182,7 +182,7 @@ repos:
args:
- --output-format=colorized
additional_dependencies:
- ansible-compat>=4.1.11
- ansible-compat>=24.5.1a0
- ansible-core>=2.14.0
- black>=22.10.0
- docutils
Expand Down
1 change: 1 addition & 0 deletions examples/.test_collection/.ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions examples/playbooks/test_import_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Fixture 1 for bug 4024
import_playbook: community.molecule.validate.yml
- name: Fixture 2 for bug 4024
ansible.builtin.import_playbook: community.molecule.validate.yml
9 changes: 5 additions & 4 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def __init__(
checked_files = set()
self.checked_files = checked_files

self.app = get_app(cached=True)

def _update_exclude_paths(self, exclude_paths: list[str]) -> None:
if exclude_paths:
# These will be (potentially) relative paths
Expand Down Expand Up @@ -232,12 +234,12 @@ def _run(self) -> list[MatchError]:

# -- phase 1 : syntax check in parallel --
if not self.skip_ansible_syntax_check:
app = get_app(cached=True)
# app = get_app(cached=True)

def worker(lintable: Lintable) -> list[MatchError]:
return self._get_ansible_syntax_check_matches(
lintable=lintable,
app=app,
app=self.app,
)

for lintable in self.lintables:
Expand Down Expand Up @@ -518,7 +520,6 @@ def find_children(self, lintable: Lintable) -> list[Lintable]:
if path != path_str:
child.path = Path(path)
child.name = child.path.name

results.append(child)
return results

Expand All @@ -532,7 +533,7 @@ def play_children(
"""Flatten the traversed play tasks."""
# pylint: disable=unused-argument

handlers = HandleChildren(self.rules)
handlers = HandleChildren(self.rules, app=self.app)

delegate_map: dict[
str,
Expand Down
14 changes: 10 additions & 4 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
AnsibleParserErrorRule,
RuntimeErrorRule,
)
from ansiblelint.app import get_app
from ansiblelint.app import App, get_app
from ansiblelint.config import Options, options
from ansiblelint.constants import (
ANNOTATION_KEYS,
Expand Down Expand Up @@ -290,6 +290,7 @@ class HandleChildren:
"""Parse task, roles and children."""

rules: RulesCollection = field(init=True, repr=False)
app: App

def include_children(
self,
Expand All @@ -307,9 +308,14 @@ def include_children(
if not v or "{{" in v:
return []

if "import_playbook" in k and COLLECTION_PLAY_RE.match(v):
# Any import_playbooks from collections should be ignored as ansible
# own syntax check will handle them.
if k in ("import_playbook", "ansible.builtin.import_playbook"):
included = Path(basedir) / v
if self.app.runtime.has_playbook(v, basedir=Path(basedir)):
if included.exists():
return [Lintable(included, kind=parent_type)]
return []
msg = f"Failed to find {v} playbook."
logging.error(msg)
return []

# handle include: filename.yml tags=blah
Expand Down
12 changes: 12 additions & 0 deletions test/test_import_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ def test_task_hook_import_playbook(default_rules_collection: RulesCollection) ->
assert "Commands should not change things" in results_text
assert "[name]" in results_text
assert "All tasks should be named" in results_text


def test_import_playbook_from_collection(
default_rules_collection: RulesCollection,
) -> None:
"""Assures import_playbook from collection."""
playbook_path = "examples/playbooks/test_import_playbook.yml"
runner = Runner(playbook_path, rules=default_rules_collection)
results = runner.run()

assert len(runner.lintables) == 1
assert len(results) == 0

0 comments on commit aaff090

Please sign in to comment.