feat(slice-1): implement tracer bullet pipeline with PyYAML check and pure-python fallback
This commit is contained in:
1 parent
937acc2548
commit
68802710b0
12 files changed
+331
-1
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from inventory_collector import __version__
|
||||
from inventory_collector.models import HostRecord, InventoryData
|
||||
from inventory_collector.exporters.ansible_yaml import AnsibleYamlExporter
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="inventory-collector",
|
||||
description="Universal homelab and fleet inventory collector and deduplicator."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-V", "--version",
|
||||
action="version",
|
||||
version=f"%(prog)s {__version__}"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o", "--output",
|
||||
type=Path,
|
||||
help="Target output file path. Defaults to stdout."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f", "--format",
|
||||
choices=["ansible-yaml", "ansible-json", "linux-connector", "yaml", "xml", "ini"],
|
||||
default="ansible-yaml",
|
||||
help="Export format. Inferred from output file extension if omitted."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--check", "--dry-run",
|
||||
dest="dry_run",
|
||||
action="store_true",
|
||||
help="Inspect inventory without writing any files."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--list",
|
||||
action="store_true",
|
||||
help="Ansible dynamic inventory mode: outputs JSON to stdout."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--sync",
|
||||
action="store_true",
|
||||
help="Synchronize inventory to both Ansible and linux-connector default paths."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c", "--config",
|
||||
type=Path,
|
||||
help="Path to configuration file (config.yaml)."
|
||||
)
|
||||
return parser
|
||||
|
||||
def main(argv: Optional[List[str]] = None) -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
# Tracer bullet sample data when no source is loaded yet
|
||||
inv = InventoryData()
|
||||
inv.add_host(HostRecord(
|
||||
name="k3s-master-0",
|
||||
ansible_host="192.168.5.70",
|
||||
ansible_user="lain",
|
||||
groups=["k3s_master", "k3s_cluster"],
|
||||
tags=["control-plane", "proxmox"],
|
||||
source="tracer"
|
||||
))
|
||||
inv.add_host(HostRecord(
|
||||
name="node0",
|
||||
ansible_host="192.168.0.101",
|
||||
ansible_user="root",
|
||||
groups=["proxmox_nodes"],
|
||||
tags=["hypervisor", "pve"],
|
||||
source="tracer"
|
||||
))
|
||||
|
||||
if args.dry_run:
|
||||
sys.stderr.write(f"[INFO] Discovered {len(inv.hosts)} hosts across {len(inv.groups)} groups (tracer mode).\n")
|
||||
print("NAME IP ADDRESS SOURCE GROUPS")
|
||||
for h in inv.hosts.values():
|
||||
groups_str = ", ".join(h.groups)
|
||||
print(f"{h.name:<17} {h.ansible_host:<15} {h.source:<9} {groups_str}")
|
||||
return 0
|
||||
|
||||
exporter = AnsibleYamlExporter()
|
||||
rendered = exporter.export(inv)
|
||||
|
||||
if args.output:
|
||||
exporter.write_to_file(inv, args.output)
|
||||
sys.stderr.write(f"[SUCCESS] Written inventory to {args.output}\n")
|
||||
else:
|
||||
sys.stdout.write(rendered)
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user