From 7bc83c574e3ee3cd415d1eab605babc3cb9b7357 Mon Sep 17 00:00:00 2001 From: Conner Crosby Date: Sat, 16 Nov 2024 11:30:30 -0500 Subject: [PATCH] Enable FQCNs for import_playbook to have subdirs (#4362) --- .../testcollection/playbooks/test/bar/foo.yml | 9 +++++++++ examples/playbooks/import_playbook_fqcn.yml | 3 +++ src/ansiblelint/text.py | 2 +- src/ansiblelint/utils.py | 16 +++++++++++----- test/test_utils.py | 15 +++++++++++++++ tox.ini | 2 +- 6 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 collections/ansible_collections/local/testcollection/playbooks/test/bar/foo.yml diff --git a/collections/ansible_collections/local/testcollection/playbooks/test/bar/foo.yml b/collections/ansible_collections/local/testcollection/playbooks/test/bar/foo.yml new file mode 100644 index 0000000000..a465be95e6 --- /dev/null +++ b/collections/ansible_collections/local/testcollection/playbooks/test/bar/foo.yml @@ -0,0 +1,9 @@ +--- +- name: Fixture + hosts: localhost + connection: local + gather_facts: false + tasks: + - name: Another task + ansible.builtin.debug: + msg: debug message diff --git a/examples/playbooks/import_playbook_fqcn.yml b/examples/playbooks/import_playbook_fqcn.yml index 398f324125..ecc7af055d 100644 --- a/examples/playbooks/import_playbook_fqcn.yml +++ b/examples/playbooks/import_playbook_fqcn.yml @@ -1,3 +1,6 @@ --- - name: Import a playbook ansible.builtin.import_playbook: local.testcollection.foo + +- name: Import a playbook that is in a subdirectory + ansible.builtin.import_playbook: local.testcollection.test.bar.foo diff --git a/src/ansiblelint/text.py b/src/ansiblelint/text.py index 457bc55230..7fb8bcaa72 100644 --- a/src/ansiblelint/text.py +++ b/src/ansiblelint/text.py @@ -7,7 +7,7 @@ RE_HAS_JINJA = re.compile(r"{[{%#].*[%#}]}", re.DOTALL) RE_HAS_GLOB = re.compile("[][*?]") -RE_IS_FQCN_OR_NAME = re.compile(r"^\w+(\.\w+\.\w+)?$") +RE_IS_FQCN_OR_NAME = re.compile(r"^\w+((\.\w+){2,})*$") def strip_ansi_escape(data: str | bytes) -> str: diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index ec42ec3dd4..4af15c1770 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -450,7 +450,7 @@ def import_playbook_children( ) -> list[Lintable]: """Include import_playbook children.""" - def append_playbook_path(loc: str, playbook_name: str) -> None: + def append_playbook_path(loc: str, playbook_path: list[str]) -> None: possible_paths.append( Path( path_dwim( @@ -460,7 +460,7 @@ def append_playbook_path(loc: str, playbook_name: str) -> None: namespace_name, collection_name, "playbooks", - playbook_name, + *playbook_path, ), ), ), @@ -471,11 +471,17 @@ def append_playbook_path(loc: str, playbook_name: str) -> None: return [] possible_paths = [] - namespace_name, collection_name, playbook_name = parse_fqcn(v) + namespace_name, collection_name, *playbook_path = parse_fqcn(v) if namespace_name and collection_name: for loc in get_app(cached=True).runtime.config.collections_paths: - append_playbook_path(loc, f"{playbook_name}.yml") - append_playbook_path(loc, f"{playbook_name}.yaml") + append_playbook_path( + loc, + playbook_path[:-1] + [f"{playbook_path[-1]}.yml"], + ) + append_playbook_path( + loc, + playbook_path[:-1] + [f"{playbook_path[-1]}.yaml"], + ) else: possible_paths.append(lintable.path.parent / v) diff --git a/test/test_utils.py b/test/test_utils.py index bc0a115e24..5d2a26167c 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -520,3 +520,18 @@ def test_import_playbook_children() -> None: "Failed to load local.testcollection.foo playbook due to failing syntax check." not in result.stderr ) + + +def test_import_playbook_children_subdirs() -> None: + """Verify import_playbook_children() when playbook is in a subdirectory.""" + result = run_ansible_lint( + Path("playbooks/import_playbook_fqcn.yml"), + cwd=Path(__file__).resolve().parent.parent / "examples", + env={ + "ANSIBLE_COLLECTIONS_PATH": "../collections", + }, + ) + assert ( + "Failed to find local.testcollection.test.bar.foo playbook." + not in result.stderr + ) diff --git a/tox.ini b/tox.ini index bae58b10df..a683c88e6c 100644 --- a/tox.ini +++ b/tox.ini @@ -76,7 +76,7 @@ setenv = PRE_COMMIT_COLOR = always # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. (tox-extra) - PYTEST_REQPASS = 895 + PYTEST_REQPASS = 896 FORCE_COLOR = 1 pre: PIP_PRE = 1 allowlist_externals =