diff --git a/examples/playbooks/import-failed-syntax-check.yml b/examples/playbooks/import-failed-syntax-check.yml new file mode 100644 index 0000000000..2db02fbacd --- /dev/null +++ b/examples/playbooks/import-failed-syntax-check.yml @@ -0,0 +1,3 @@ +--- +- name: Import syntax-error.yml playbook + import_playbook: syntax-error.yml diff --git a/examples/playbooks/import-nonexistent-playbook.yml b/examples/playbooks/import-nonexistent-playbook.yml new file mode 100644 index 0000000000..aa96caa9e5 --- /dev/null +++ b/examples/playbooks/import-nonexistent-playbook.yml @@ -0,0 +1,3 @@ +--- +- name: Import nonexistent playbook + import_playbook: i-do-not-exist.yml diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index 1446572e06..19c6356291 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -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 [] diff --git a/test/test_utils.py b/test/test_utils.py index 6a3b1c607e..09f623466a 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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 @@ -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 + )