Files
computer-playbook/plugins/lookup/container.py
T
kevinveenbirkenbachandClaude Opus 4.7 ea5c8a8cbe fix(lookups): render Jinja in DIR_COMPOSITIONS + multi-pass templating in email lookup
`DIR_COMPOSITIONS` resolves to a Jinja expression in swarm mode
(`{{ DIR_VAR_LIB ~ "/compose/" if DEPLOYMENT_MODE == "swarm" else "/opt/compose/" }}`),
but Ansible passes variables un-templated into lookups. container, database,
and compose_file_args now call `templar.template()` when the raw value still
contains `{{`, so the resolved path reaches `get_docker_paths()`.

email's `_render` switches to `_templar_render_best_effort` (TrustedAsTemplate
for Ansible 2.19+) and loops up to 4 passes: nested inventory expressions like
`DOMAIN_PRIMARY="{{ lookup('env','INFINITO_DOMAIN') }}"` need multiple
substitution rounds before reaching the literal domain.

Drops a stale `import textwrap` in test_depends_on.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-14 19:08:46 +02:00

58 lines
1.8 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from utils.docker.paths_utils import get_docker_paths
def _as_str(v: Any) -> str:
return "" if v is None else str(v).strip()
def _deep_get(d: dict[str, Any], path: str) -> Any:
cur: Any = d
for key in path.split("."):
if not isinstance(cur, dict) or key not in cur:
raise AnsibleError(f"lookup('container'): invalid path '{path}'")
cur = cur[key]
return cur
class LookupModule(LookupBase):
"""
lookup('container', application_id[, path])
path can be:
- 'directories' / 'files' (returns sub-dict)
- 'directories.instance' (returns leaf)
"""
def run(self, terms, variables: dict[str, Any] | None = None, **kwargs):
if not terms or len(terms) > 2:
raise AnsibleError("lookup('container', application_id[, path])")
application_id = _as_str(terms[0])
if not application_id:
raise AnsibleError("lookup('container'): application_id is empty")
raw_base = (variables or {}).get("DIR_COMPOSITIONS")
# DIR_COMPOSITIONS is itself a Jinja expression in swarm mode; lookup vars arrive un-templated.
if isinstance(raw_base, str) and "{{" in raw_base and self._templar is not None:
raw_base = self._templar.template(raw_base)
base = _as_str(raw_base)
if not base:
raise AnsibleError("lookup('container'): DIR_COMPOSITIONS not set")
paths = get_docker_paths(application_id, base)
if len(terms) == 2:
path = _as_str(terms[1])
if not path:
raise AnsibleError("lookup('container'): path is empty")
return [_deep_get(paths, path)]
return [paths]