Files
computer-playbook/plugins/lookup/container_image.py
T
kevinveenbirkenbachandClaude Fable 5 e917f0860d refactor(lookups): split bare-ref image resolver out of container_image
New 'image' lookup is the SPOT for resolving <registry>/<image>:<version>
(swarm registry prefix in swarm mode, bare in compose; custom= accepts a
string base for a second distinct custom image inside one app).
container_image becomes a thin wrapper that forwards terms+kwargs and adds
the compose image: "<ref>" line, so all existing template call sites keep
their output byte-identical. The lookup-usage integration test now counts
lookup_loader.get() delegation as production usage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 19:48:53 +02:00

42 lines
1.3 KiB
Python

"""Lookup `container_image`: compose ``image: "<ref>"`` line for a
container service.
Thin wrapper around the `image` lookup (the SPOT for resolving the
``<registry>/<image>:<version>`` reference). ``container_image`` only adds
the ``image: "..."`` compose-line wrapping so templates can drop it in as
a standalone statement:
# roles/web-app-X/templates/compose.yml.j2
{{ lookup('container_image', application_id, 'x') }}
# Locally-built image (no registry upstream).
{{ lookup('container_image', application_id, 'x', custom=True) }}
All resolution kwargs (``image=``, ``version=``, ``custom=``, ``tag_only=``)
are forwarded to `image`. For a bare reference (no wrapping), call the
`image` lookup directly.
Both terms (application_id, service_key) are required; they are validated
by `image`.
"""
from __future__ import annotations
from typing import Any
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[str]:
ref = lookup_loader.get(
"image", loader=self._loader, templar=getattr(self, "_templar", None)
).run(terms, variables=variables, **kwargs)[0]
return [f'image: "{ref}"']