mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 02:54:26 +00:00
`cli build include` generated one tasks/groups/<group>-roles.yml per invokable category during setup, and the stage plays included those files. They were written by the in-container UID, which is why a dedicated clean-container-owned helper had to delete them from inside the container.
The stage plays now loop over lookup('group_roles', <group>) and include tasks/utils/setup/{group,role}.yml directly. ordered_roles() in utils/roles/order.py holds the run_after topological sort the generator used to perform at build time.
utils/roles/stage.py:_group_names() no longer globs the generated files; it reads get_invokable_paths(), the same categories.yml source those files were derived from.
Measured: handlers still flush after every role (ansible-core 2.20.2, three loop items, three separate handler runs), and resolving all 18 groups costs 0.60s cold against 1.4s for the generator.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""Lookup `group_roles`: the roles of a role group in run_after order.
|
|
|
|
Returns one ``{'role': …, 'app': …}`` mapping per role, so a stage can loop
|
|
over the group instead of including a generated task file per group:
|
|
|
|
loop: "{{ lookup('group_roles', 'web-app') }}"
|
|
|
|
`role` is the directory under ``roles/``, `app` the role's application_id --
|
|
the value the deployment whitelist and `application_allowed` speak in.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from utils.roles.order import ordered_roles
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(
|
|
self,
|
|
terms: list[Any] | None,
|
|
variables: dict[str, Any] | None = None,
|
|
**kwargs: Any,
|
|
) -> list[list[dict[str, str]]]:
|
|
if not terms or len(terms) != 1:
|
|
raise AnsibleError(
|
|
"group_roles lookup requires exactly one term: the group"
|
|
)
|
|
|
|
group = str(terms[0]).strip()
|
|
if not group:
|
|
raise AnsibleError("group_roles lookup: the group must not be empty")
|
|
|
|
roles_dir = str(kwargs.get("roles_dir") or Path.cwd() / "roles")
|
|
try:
|
|
return [[dict(entry) for entry in ordered_roles(roles_dir, group)]]
|
|
except Exception as exc:
|
|
raise AnsibleError(f"group_roles: {exc}") from exc
|