fix(ansible): group names Ansible accepts, and interpreter discovery without the warning (#5334)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 57s
CI / unit (push) Successful in 1m24s
CI / publish (push) Successful in 1m26s

Every run warned twice because of Steward's own inventory:
- 'Invalid characters were found in group names': the group 'Docker
  Swarm' went into the inventory as typed. ansible_group_name() cleans it
  to docker_swarm (lower case, [a-z0-9_], never a leading digit); two
  names that clean alike get _2, _3 so neither folds into the other. The
  group page says what plays call it. The display name is unchanged.
- Python interpreter discovery: ansible_env sets
  ANSIBLE_PYTHON_INTERPRETER=auto_silent unless the environment already
  sets one; a target or group's ansible_python_interpreter still wins.

Runs are scoped by target set (steward:group:<id>), not by group name,
and --limit is free text, so neither path depends on the name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 00:01:54 -04:00
co-authored by Claude Opus 5.5
parent 7c346354f2
commit c678379646
8 changed files with 80 additions and 7 deletions
+35 -3
View File
@@ -1,6 +1,36 @@
"""Ansible --list inventory generation from DB records."""
from __future__ import annotations
import re
_UNSAFE = re.compile(r"[^a-z0-9_]+")
def ansible_group_name(name: str) -> str:
"""A Steward group's name as Ansible accepts it (#5334): lower case, each
run of anything but letters, digits and _ made one _, never starting with
a digit. "Docker Swarm" is docker_swarm, which a play can name in
`hosts:`; the display name stays as typed everywhere else."""
safe = _UNSAFE.sub("_", name.lower()).strip("_") or "group"
return f"g_{safe}" if safe[0].isdigit() else safe
def _group_names(targets: list) -> dict[str, str]:
"""{display name: Ansible name} for every group these targets are in. Two
names that clean to the same thing ("Web servers", "web-servers") get _2,
_3 in name order, so neither group's hosts or vars fold into the other's."""
out: dict[str, str] = {}
taken: set[str] = set()
for name in sorted({g.name for t in targets for g in t.groups}):
base = safe = ansible_group_name(name)
n = 1
while safe in taken or safe in ("all", "ungrouped", "_meta"):
n += 1
safe = f"{base}_{n}"
taken.add(safe)
out[name] = safe
return out
def generate_inventory(targets: list) -> dict:
"""Build an Ansible --list JSON inventory from a list of AnsibleTarget objects.
@@ -13,6 +43,7 @@ def generate_inventory(targets: list) -> dict:
group vars (sorted alphabetically by group name) → target ansible_vars → ansible_host
"""
inv: dict = {"all": {"hosts": []}, "_meta": {"hostvars": {}}}
names = _group_names(targets)
for target in targets:
inv["all"]["hosts"].append(target.name)
@@ -27,12 +58,13 @@ def generate_inventory(targets: list) -> dict:
inv["_meta"]["hostvars"][target.name] = merged
for group in target.groups:
if group.name not in inv:
inv[group.name] = {
key = names[group.name]
if key not in inv:
inv[key] = {
"hosts": [],
"vars": dict(group.ansible_vars or {}),
}
inv[group.name]["hosts"].append(target.name)
inv[key]["hosts"].append(target.name)
return inv