Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FQCNs for import_playbook to have subdirs #4412

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(r"[][*?]")
RE_IS_FQCN_OR_NAME = re.compile(r"^\w+(\.\w+\.\w+)?$")
RE_IS_FQCN_OR_NAME = re.compile(r"^\w+(\.\w+){2,100}$|^\w+$")


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 @@ -460,7 +460,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 @@ -470,7 +470,7 @@ def append_playbook_path(loc: str, playbook_name: str) -> None:
namespace_name,
collection_name,
"playbooks",
playbook_name,
*playbook_path,
),
),
),
Expand All @@ -481,11 +481,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 @@ -522,3 +522,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 @@ -75,7 +75,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 = 904
PYTEST_REQPASS = 905
FORCE_COLOR = 1
pre: PIP_PRE = 1
allowlist_externals =
Expand Down
Loading