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
+27
View File
@@ -138,3 +138,30 @@ def test_describe_scope_names_what_a_run_targeted():
assert describe_scope("steward:group:g1", targets, groups) == "group Docker Swarm"
assert describe_scope("steward:target:abcdef0123", targets, groups) == "target abcdef01 (deleted)"
assert describe_scope("repo:homelab:hosts.yml", targets, groups) == "homelab/hosts.yml"
# ── Group names Ansible accepts (#5334) ───────────────────────────────────────
def test_a_group_name_is_cleaned_for_ansible():
from steward.ansible.inventory_gen import ansible_group_name
assert ansible_group_name("Docker Swarm") == "docker_swarm"
assert ansible_group_name("web-servers (prod)") == "web_servers_prod"
assert ansible_group_name("2nd floor") == "g_2nd_floor"
assert ansible_group_name("!!!") == "group"
assert ansible_group_name("already_fine") == "already_fine"
def test_the_inventory_uses_the_clean_names_and_keeps_clashing_groups_apart():
swarm = _group("Docker Swarm", {"role": "swarm"})
a, b = _group("Web servers", {"tier": "a"}), _group("web-servers", {"tier": "b"})
inv = generate_inventory([_target("d1", "10.0.0.1", groups=[swarm]),
_target("w1", "10.0.0.2", groups=[a]),
_target("w2", "10.0.0.3", groups=[b])])
assert "Docker Swarm" not in inv and inv["docker_swarm"]["hosts"] == ["d1"]
assert inv["docker_swarm"]["vars"] == {"role": "swarm"}
# "Web servers" sorts before "web-servers", so it keeps the plain name.
assert inv["web_servers"] == {"hosts": ["w1"], "vars": {"tier": "a"}}
assert inv["web_servers_2"] == {"hosts": ["w2"], "vars": {"tier": "b"}}
import yaml
children = yaml.safe_load(inventory_to_yaml(inv))["all"]["children"]
assert set(children) == {"docker_swarm", "web_servers", "web_servers_2"}