Skip to content

Commit

Permalink
Enable FQCNs for import_playbook to have subdirs (#4362)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby committed Nov 16, 2024
1 parent e62ef52 commit 7bc83c5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Fixture
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Another task
ansible.builtin.debug:
msg: debug message
3 changes: 3 additions & 0 deletions examples/playbooks/import_playbook_fqcn.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/ansiblelint/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 11 additions & 5 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -460,7 +460,7 @@ def append_playbook_path(loc: str, playbook_name: str) -> None:
namespace_name,
collection_name,
"playbooks",
playbook_name,
*playbook_path,
),
),
),
Expand All @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 7bc83c5

Please sign in to comment.