Files
computer-playbook/cli/administration/inventory/devices/command.py
kevinveenbirkenbachandClaude Fable 5 40e553069d refactor(structure): nest prefix-grouped sibling files into subfolders
Move files that share a delimiter-bounded name prefix within a folder (threshold 2) into a subfolder named after the prefix, dropping the prefix from the filename. Covers plugins/filter (docker, generate, has, merge, get/all), env handler subpackages, utils/roles/entity, utils/tests/swarm/write, role SQL/scripts/templates, keycloak/mailu task-includes, shared tasks/utils/load, and CI scripts. Update every import, query_file/script/include_tasks/src/lookup target, compose bind mount, and python -m path to the new locations.

Extend the prefix-grouped-files lint to threshold 2 with dir-level .nocheck and file-head nocheck opt-outs, and skip groups whose shortest member's stem equals the shared prefix. Keep flat only where a loader forbids nesting: WordPress mu-plugins and the docs/requirements namespace. Ansible filter discovery is recursive, so nested filters stay Jinja-resolvable.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 20:00:03 +02:00

109 lines
2.9 KiB
Python

from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from plugins.filter.get.all.invokable_apps import get_all_invokable_apps
from utils.cache.yaml import dump_yaml_str
def build_group_inventory(apps: list[str], host: str) -> dict:
"""
Build an Ansible inventory in which each application is a group containing the given host.
"""
groups = {app: {"hosts": [host]} for app in apps}
return {
"all": {
"hosts": [host],
"children": {app: {} for app in apps},
},
**groups,
}
def build_hostvar_inventory(apps: list[str], host: str) -> dict:
"""
Alternative: Build an inventory where all invokable apps are set as a host variable (as a list).
"""
return {
"all": {"hosts": [host]},
"_meta": {
"hostvars": {
host: {
"invokable_applications": apps,
}
}
},
}
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Build a dynamic Ansible inventory for a given host with all invokable applications."
)
parser.add_argument(
"--host",
required=True,
help="Hostname to assign to all invokable application groups",
)
parser.add_argument(
"-f",
"--format",
choices=["json", "yaml"],
default="yaml",
help="Output format (yaml [default], json)",
)
parser.add_argument(
"--inventory-style",
choices=["group", "hostvars"],
default="group",
help="Inventory style: group (default, one group per app) or hostvars (list as hostvar)",
)
parser.add_argument(
"-o",
"--output",
help="Write output to file instead of stdout",
)
parser.add_argument(
"-i",
"--ignore",
action="append",
default=[],
help="Application ID(s) to ignore (can be specified multiple times or comma-separated)",
)
args = parser.parse_args(argv)
try:
apps = get_all_invokable_apps()
except Exception as exc:
sys.stderr.write(f"Error: {exc}\n")
return 1
ignore_ids: set[str] = set()
for entry in args.ignore:
ignore_ids.update(i.strip() for i in entry.split(",") if i.strip())
if ignore_ids:
apps = [app for app in apps if app not in ignore_ids]
if args.inventory_style == "group":
inventory = build_group_inventory(apps, args.host)
else:
inventory = build_hostvar_inventory(apps, args.host)
if args.format == "json":
output = json.dumps(inventory, indent=2)
else:
output = dump_yaml_str(inventory)
if args.output:
with Path(args.output).open("w", encoding="utf-8") as fh:
fh.write(output)
else:
print(output)
return 0