feat(ansible): a run's scope reads as the target or group it names
#5014 "Ansible run scope is shown as a raw id" - inventory_gen.describe_scope / scope_labels turn "steward:target:<uuid>" into "target web-1", "steward:group:<uuid>" into "group Docker Swarm", and a deleted one into "target abcdef01 (deleted)". - MCP run and schedule rows carry scope_label beside the stored scope; the ansible.run.started audit detail records it too. - The web run page and schedules table show the name, with the stored scope as the tooltip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,3 +106,39 @@ async def fetch_scope_targets(db, scope: str) -> list:
|
||||
|
||||
result = await db.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
def describe_scope(scope: str, targets: dict[str, str], groups: dict[str, str]) -> str:
|
||||
"""A run's inventory scope in words: "target web-1", "group Docker Swarm",
|
||||
"all inventory targets", "<source>/<inventory file>". `targets` and
|
||||
`groups` map id → name; a scope naming a deleted one says so."""
|
||||
scope = scope or ""
|
||||
if scope == "steward:all":
|
||||
return "all inventory targets"
|
||||
for prefix, kind, names in (("steward:target:", "target", targets),
|
||||
("steward:group:", "group", groups)):
|
||||
if scope.startswith(prefix):
|
||||
ident = scope[len(prefix):]
|
||||
name = names.get(ident)
|
||||
return f"{kind} {name}" if name else f"{kind} {ident[:8]} (deleted)"
|
||||
if scope.startswith("repo:"):
|
||||
source, _, inv = scope[len("repo:"):].rpartition(":")
|
||||
return f"{source}/{inv}" if source else inv
|
||||
return scope
|
||||
|
||||
|
||||
async def scope_labels(db, scopes) -> dict[str, str]:
|
||||
"""{scope: describe_scope(scope)} for the given scopes, in two queries."""
|
||||
from sqlalchemy import select
|
||||
from steward.models.ansible_inventory import AnsibleGroup, AnsibleTarget
|
||||
|
||||
scopes = {s for s in scopes if s}
|
||||
t_ids = {s[len("steward:target:"):] for s in scopes if s.startswith("steward:target:")}
|
||||
g_ids = {s[len("steward:group:"):] for s in scopes if s.startswith("steward:group:")}
|
||||
targets = dict((await db.execute(
|
||||
select(AnsibleTarget.id, AnsibleTarget.name).where(AnsibleTarget.id.in_(t_ids))
|
||||
)).all()) if t_ids else {}
|
||||
groups = dict((await db.execute(
|
||||
select(AnsibleGroup.id, AnsibleGroup.name).where(AnsibleGroup.id.in_(g_ids))
|
||||
)).all()) if g_ids else {}
|
||||
return {s: describe_scope(s, targets, groups) for s in scopes}
|
||||
|
||||
Reference in New Issue
Block a user