Files
computer-playbook/plugins/lookup/mxid.py
T
kevinveenbirkenbachandClaude Opus 5 db2a12566d fix(web-app-matrix): give Matrix user ids one definition and the admin MDAD needs
The variant-3 bootstrap aborts on an upstream guard:

    TASK [custom/matrix-bot-baibot : Fail if admin patterns list is empty]
    You need to define a required configuration setting
    (`matrix_bot_baibot_config_access_admin_patterns`)

which takes the whole playbook down and surfaces only as the generic
"matrix-mdad bootstrap failed inside the container". Nothing asks for that
setting directly: at the pinned MDAD ref 40179e6d4a it is derived from
matrix_admin, which matrix-base defaults to the empty string. The template
rendered every other baibot setting and never set the one they hang off.

matrix_admin could not simply be written out, because "@localpart:server" was
assembled by hand in nine places and the two flavors had already drifted apart
over it: the compose flavor built it from MATRIX_SERVER_NAME while the MDAD
template computed its own onion conditional off DOMAIN_PRIMARY, skipping the
services.matrix.server_name key the role declares. group_vars/all/14_about.yml
had drifted further still and used the raw clearnet name, so on an onion
deployment it published a contact handle that resolves nowhere.

The shape now lives in the mxid filter, which rejects an empty part or a
localpart that already carries a sigil and lowercases the result because
Synapse refuses anything else. The homeserver name lives in the
matrix_server_name lookup, which answers the Tor node when the role is
published under .onion and the declared name otherwise. mxid joins the two, so
every caller names only an account, needs no role variable in scope, and gets
an id that matches whichever network is published. MATRIX_SERVER_NAME now reads
that lookup rather than repeating the conditional, which leaves MATRIX_ONION_NODE
without a consumer, and it goes.

Rendering the replaced Jinja against the new resolver over eighteen input
combinations disagrees three times, each on input no inventory produces - a
whitespace-only Tor node, an uppercase .ONION domain, one with a trailing space -
and each time the new answer is the correct one.

Separately, Nextcloud#12 dies in the same role with

    /bin/sh: 1: sudo: not found

because matrix-synapse-usage-exporter pins self_build: true upstream and its
clone task becomes, which the runner image could not do. sudo joins its package
list; the container is already privileged, so this grants nothing new.

Static throughout: the baibot bootstrap had reached ok=196 of roughly 540 tasks
and the usage-exporter self-build never got to its docker build, so both fixes
clear the task that failed and nothing behind it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-06 13:52:36 +02:00

34 lines
1.3 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from plugins.filter.matrix import mxid
from plugins.lookup.matrix_server_name import resolve_server_name
class LookupModule(LookupBase):
"""
Usage:
{{ lookup('mxid', 'chatgptbot') }}
{{ lookup('mxid', lookup('users', 'contact').username) }}
Matrix user id of a localpart on this deployment's homeserver. The server
name comes from lookup('matrix_server_name'), which answers the Tor node on
an onion deployment and the declared name otherwise, so a caller names only
the account and gets an id that matches whichever network is published.
Needs no role var, so group_vars and other roles can use it as well.
"""
def run(self, terms, variables: dict[str, Any] | None = None, **kwargs):
if len(terms or []) != 1:
raise AnsibleError(
"mxid: expected exactly 1 term: lookup('mxid', localpart)"
)
templar = getattr(self, "_templar", None)
variables = variables or getattr(templar, "available_variables", {}) or {}
server = resolve_server_name(variables, self._loader, templar)
return [mxid(terms[0], server)]