Files
computer-playbook/cli/create/inventory/inventory_generator.py
T
kevinveenbirkenbach a354992f27 refactor(create-inventory): modularize CLI, stabilize path handling, add unit tests
- Split create-inventory logic into focused modules (cli, project, filters, host_vars, credentials, etc.)
- Replace fragile path construction with python -m module execution
- Add robust project root detection
- Centralize password generation
- Improve test coverage with per-module unit tests
- Preserve existing inventory and host_vars behavior

https://chatgpt.com/share/69471157-79c0-800f-8dec-25c0f8e38f03
2025-12-20 22:12:44 +01:00

46 lines
1.1 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from .subprocess_runner import run_subprocess
from .yaml_io import load_yaml
def generate_dynamic_inventory(
host: str,
roles_dir: Path,
categories_file: Path,
tmp_inventory: Path,
project_root: Path,
env: Optional[Dict[str, str]],
) -> Dict[str, Any]:
"""
Generate a dynamic inventory by executing the generator as a module:
python -m cli.build.inventory.full ...
This avoids fragile file path construction and fixes 'cli/cli/...' issues.
"""
cmd = [
sys.executable,
"-m",
"cli.build.inventory.full",
"--host",
host,
"--format",
"yaml",
"--inventory-style",
"group",
"-c",
str(categories_file),
"-r",
str(roles_dir),
"-o",
str(tmp_inventory),
]
run_subprocess(cmd, capture_output=False, env=env)
data = load_yaml(tmp_inventory)
tmp_inventory.unlink(missing_ok=True)
return data