Skip to content

Commit

Permalink
Fix error for imported playbooks w/invalid syntax (#4257)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby committed Aug 13, 2024
1 parent 5ce6ce4 commit 1708ba0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 888
PYTEST_REQPASS: 889
steps:
- uses: actions/checkout@v4
with:
Expand Down
3 changes: 3 additions & 0 deletions examples/playbooks/import-failed-syntax-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: Import syntax-error.yml playbook
import_playbook: syntax-error.yml
3 changes: 3 additions & 0 deletions examples/playbooks/import-nonexistent-playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: Import nonexistent playbook
import_playbook: i-do-not-exist.yml
11 changes: 6 additions & 5 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ def include_children( # pylint: disable=too-many-return-statements

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."
if not included.exists():
msg = f"Failed to find {v} playbook."
elif not self.app.runtime.has_playbook(v, basedir=Path(basedir)):
msg = f"Failed to load {v} playbook due to failing syntax check."
else:
return [Lintable(included, kind=parent_type)]
logging.error(msg)
return []

Expand Down
13 changes: 13 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from ansiblelint.constants import RC
from ansiblelint.file_utils import Lintable, cwd
from ansiblelint.runner import Runner
from ansiblelint.testing import run_ansible_lint

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -490,3 +491,15 @@ def test_find_children_in_playbook(default_rules_collection: RulesCollection) ->
).find_children(lintable)
assert len(children) == 1
assert children[0].role == "bug4095"


def test_include_children_load_playbook_failed_syntax_check() -> None:
"""Verify include_children() logs playbook failed to load due to syntax-check."""
result = run_ansible_lint(
Path("playbooks/import-failed-syntax-check.yml"),
cwd=Path(__file__).resolve().parent.parent / "examples",
)
assert (
"Failed to load syntax-error.yml playbook due to failing syntax check."
in result.stderr
)

0 comments on commit 1708ba0

Please sign in to comment.