Skip to content

Commit 71f725f

Browse files
committed
Enable FQCNs for import_playbook to have subdirs (#4362)
The max repetitions of 100 for the RE_IS_FQCN_OR_NAME regular expression was not chosen for any particular reason other than being a sane number with more than enough overhead to account for a long fully qualified collection name (FQCN).
1 parent 5ab7c7e commit 71f725f

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: Fixture
3+
hosts: localhost
4+
connection: local
5+
gather_facts: false
6+
tasks:
7+
- name: Another task
8+
ansible.builtin.debug:
9+
msg: debug message
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
---
22
- name: Import a playbook
33
ansible.builtin.import_playbook: local.testcollection.foo
4+
5+
- name: Import a playbook that is in a subdirectory
6+
ansible.builtin.import_playbook: local.testcollection.test.bar.foo

src/ansiblelint/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
RE_HAS_JINJA = re.compile(r"{[{%#].*[%#}]}", re.DOTALL)
99
RE_HAS_GLOB = re.compile("[][*?]")
10-
RE_IS_FQCN_OR_NAME = re.compile(r"^\w+(\.\w+\.\w+)?$")
10+
RE_IS_FQCN_OR_NAME = re.compile(r"^\w+(\.\w+){2,100}$|^\w+$")
1111

1212

1313
def strip_ansi_escape(data: str | bytes) -> str:

src/ansiblelint/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def import_playbook_children(
450450
) -> list[Lintable]:
451451
"""Include import_playbook children."""
452452

453-
def append_playbook_path(loc: str, playbook_name: str) -> None:
453+
def append_playbook_path(loc: str, playbook_path: list[str]) -> None:
454454
possible_paths.append(
455455
Path(
456456
path_dwim(
@@ -460,7 +460,7 @@ def append_playbook_path(loc: str, playbook_name: str) -> None:
460460
namespace_name,
461461
collection_name,
462462
"playbooks",
463-
playbook_name,
463+
*playbook_path,
464464
),
465465
),
466466
),
@@ -471,11 +471,17 @@ def append_playbook_path(loc: str, playbook_name: str) -> None:
471471
return []
472472

473473
possible_paths = []
474-
namespace_name, collection_name, playbook_name = parse_fqcn(v)
474+
namespace_name, collection_name, *playbook_path = parse_fqcn(v)
475475
if namespace_name and collection_name:
476476
for loc in get_app(cached=True).runtime.config.collections_paths:
477-
append_playbook_path(loc, f"{playbook_name}.yml")
478-
append_playbook_path(loc, f"{playbook_name}.yaml")
477+
append_playbook_path(
478+
loc,
479+
playbook_path[:-1] + [f"{playbook_path[-1]}.yml"],
480+
)
481+
append_playbook_path(
482+
loc,
483+
playbook_path[:-1] + [f"{playbook_path[-1]}.yaml"],
484+
)
479485
else:
480486
possible_paths.append(lintable.path.parent / v)
481487

test/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,18 @@ def test_import_playbook_children() -> None:
520520
"Failed to load local.testcollection.foo playbook due to failing syntax check."
521521
not in result.stderr
522522
)
523+
524+
525+
def test_import_playbook_children_subdirs() -> None:
526+
"""Verify import_playbook_children() when playbook is in a subdirectory."""
527+
result = run_ansible_lint(
528+
Path("playbooks/import_playbook_fqcn.yml"),
529+
cwd=Path(__file__).resolve().parent.parent / "examples",
530+
env={
531+
"ANSIBLE_COLLECTIONS_PATH": "../collections",
532+
},
533+
)
534+
assert (
535+
"Failed to find local.testcollection.test.bar.foo playbook."
536+
not in result.stderr
537+
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ setenv =
7676
PRE_COMMIT_COLOR = always
7777
# Number of expected test passes, safety measure for accidental skip of
7878
# tests. Update value if you add/remove tests. (tox-extra)
79-
PYTEST_REQPASS = 895
79+
PYTEST_REQPASS = 896
8080
FORCE_COLOR = 1
8181
pre: PIP_PRE = 1
8282
allowlist_externals =

0 commit comments

Comments
 (0)