acupedia-ops/acupedia_ops/ansible/runner.py

15 lines
972 B
Python

from __future__ import annotations
from typing import Any
from acupedia_ops.common.paths import ansible_root
def run_playbook(*, playbook: str, inventory: str, extra_vars: dict[str, Any] | None = None) -> None:
try:
import ansible_runner
except ImportError as exc:
raise RuntimeError('Install deployment support with: pip install -e ".[deployment]"') from exc
root = ansible_root(); inventory_path = root / "inventories" / inventory; playbook_path = root / "playbooks" / playbook
if not inventory_path.exists(): raise FileNotFoundError(f"Inventory not found: {inventory_path}")
if not playbook_path.exists(): raise FileNotFoundError(f"Playbook not found: {playbook_path}")
result = ansible_runner.run(private_data_dir=str(root), inventory=str(inventory_path), playbook=str(playbook_path), extravars=extra_vars or {})
if result.rc != 0: raise RuntimeError(f"Ansible playbook failed with exit code {result.rc}: {playbook_path}")