Files
computer-playbook/plugins/lookup/bounded_name.py
T
kevinveenbirkenbachandClaude Opus 5 a05071fe78 refactor(roles): let a role declare its entity name and drop the get_ prefix
The entity name keys a role's instance directory, its compose project and its container name, and was derived only by stripping the longest matching category prefix. Two roles under different categories whose names share a suffix therefore collapse onto one entity and overwrite each other's rendered stack, which no provides: can repair.

A literal entity_name in vars/main.yml now wins over the derivation. web-app-seaweedfs-console takes its shorter name back and declares the entity seaweedfs-console, which frees the seaweedfs key in its own services map for the engine consumer and removes the provides:, covers: and alias entries that stood in for it. A new lint pairs the declaration with the primary key it has to match, because a drift between them would drop the role from the provider set in silence.

Compared across all 288 roles, exactly one entity changes, and it restores what main already keeps on disk, so no instance directory moves.

The function and the filter lose their get_ prefix. Two lints read that prefix as the discriminator between the variable and the filter that fills it; both discriminate on the pipe now. Ansible's templar resolves a filter and a same-named variable independently, verified against ansible-core 2.20.2.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-03 19:40:53 +02:00

71 lines
2.4 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
from utils.domains.primary_domain import get_domain
from utils.roles.entity.name import entity_name
class LookupModule(LookupBase):
"""
Usage:
{{ lookup('bounded_name', application_id, max_length) }}
An application's canonical primary domain when it has at most max_length
characters, otherwise its short entity name (e.g. ``keycloak``).
Args:
application_id: the application whose domain names the value.
max_length: the longest value the consumer accepts; a positive integer.
"""
def run(self, terms, variables: dict[str, Any] | None = None, **kwargs):
if not terms or len(terms) != 2:
raise AnsibleError(
"lookup('bounded_name', application_id, max_length) expects exactly 2 terms"
)
application_id, max_length = terms
if not isinstance(application_id, str) or not application_id.strip():
raise AnsibleError(
"lookup('bounded_name'): application_id must be a "
f"non-empty string, got {application_id!r}"
)
application_id = application_id.strip()
try:
limit = int(max_length)
except (TypeError, ValueError) as e:
raise AnsibleError(
f"lookup('bounded_name'): max_length must be an integer, got {max_length!r}"
) from e
if limit < 1:
raise AnsibleError(
f"lookup('bounded_name'): max_length must be positive, got {limit}"
)
variables = variables or getattr(self._templar, "available_variables", {}) or {}
domains = lookup_loader.get(
"domains",
loader=getattr(self, "_loader", None),
templar=getattr(self, "_templar", None),
).run([], variables=variables, roles_dir=kwargs.get("roles_dir"))[0]
try:
domain = get_domain(domains, application_id)
except Exception as e:
raise AnsibleError(
f"lookup('bounded_name'): failed to resolve domain for "
f"'{application_id}': {e}"
) from e
name = str(domain or "")
if 0 < len(name) <= limit:
return [name]
return [entity_name(application_id)]