Files
computer-playbook/utils/inventory/groups.py
T
kevinveenbirkenbachandClaude Opus 5.5 d92375922d feat(roles): pull custom roles from inventory-declared sources
An inventory group web-app-cst-<name> with applications.<id>.custom: true and
a pinned source {url, version, path} is pulled into roles/ before the play,
read raw (Jinja, Ansible tags and credential URLs refused), placed through a
staging directory under an exclusive lock, and owned by a marker that names
its own directory, so a pull or roles-clean never replaces or removes a
directory it did not place and a failed pull leaves roles/ as it was.
Directory inventories, host lists and non-UTF-8 files behave as Ansible
reads them; generated test deploys pass --skip-pull. make roles-pull,
roles-clean and roles list, INFINITO_EXTRA_MOUNTS for local sources, the
web-app-cst category, the reserved prefix lint and a central
repository_files() that also yields the gitignored custom roles let every
lint scan them like in-tree roles. Supersedes the uncommitted 045 External
Roles draft and reuses its fetch, sync and mount design.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 00:02:58 +01:00

90 lines
2.9 KiB
Python

"""Static-inventory group lookups (YAML + INI)."""
from __future__ import annotations
from pathlib import Path
from utils.cache.yaml import load_yaml_any, load_yaml_tagged
def inventory_has_group(inventory_path: str, group_name: str) -> bool:
if Path(inventory_path).suffix in (".yml", ".yaml"):
data = load_yaml_any(inventory_path, default_if_missing={})
return _find_yaml_key(data, group_name)
return _find_ini_section(inventory_path, group_name)
def inventory_groups(inventory_path: str) -> set[str]:
"""Return every group a static inventory file declares.
Ansible tags such as ``!unsafe`` and ``!vault`` load as opaque values.
Args:
inventory_path: YAML (``.yml``/``.yaml``) or INI inventory file.
"""
if Path(inventory_path).suffix in (".yml", ".yaml"):
return _yaml_groups(load_yaml_tagged(inventory_path))
return _ini_groups(inventory_path)
def _yaml_groups(node) -> set[str]:
groups: set[str] = set()
if not isinstance(node, dict):
return groups
for name, body in node.items():
groups.add(str(name))
if isinstance(body, dict):
groups |= _yaml_groups(body.get("children"))
return groups
def _ini_groups(inventory_path: str) -> set[str]:
groups: set[str] = set()
in_children = False
with Path(inventory_path).open(encoding="utf-8") as f:
for raw in f:
line = raw.strip()
if not line or line.startswith(("#", ";")):
continue
if line.startswith("[") and line.endswith("]"):
name, _, kind = line[1:-1].strip().partition(":")
groups.add(name)
in_children = kind == "children"
continue
if in_children:
groups.add(line.split()[0])
return groups
def _find_yaml_key(node, key: str) -> bool:
if isinstance(node, dict):
for k, v in node.items():
if k == key and isinstance(v, (dict, list)):
return True
if _find_yaml_key(v, key):
return True
elif isinstance(node, list):
for item in node:
if _find_yaml_key(item, key):
return True
return False
def _find_ini_section(inventory_path: str, group_name: str) -> bool:
with Path(inventory_path).open(encoding="utf-8") as f:
current_section: str | None = None
for raw in f:
line = raw.strip()
if not line or line.startswith(("#", ";")):
continue
if line.startswith("[") and line.endswith("]"):
current_section = line[1:-1].strip()
if current_section == group_name:
return True
continue
if current_section:
for part in line.replace(",", " ").split():
if part.strip() == group_name:
return True
return False