- Add merge_inventories() to combine hosts.yaml and hosts-local.yaml - Auto-detect and include hosts-local.yaml if present - Maintain separation of company and local infrastructure in source - Deploy includes both central and legacy infrastructure aliases Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@@ -17,6 +17,25 @@ def load_inventory(path: Path) -> dict: |
||
| 17 | 17 |
return data |
| 18 | 18 |
|
| 19 | 19 |
|
| 20 |
+def merge_inventories(paths: list) -> dict: |
|
| 21 |
+ """Merge multiple inventory files, with later ones overriding earlier ones.""" |
|
| 22 |
+ merged = {}
|
|
| 23 |
+ for path in paths: |
|
| 24 |
+ data = load_inventory(path) |
|
| 25 |
+ for key in ("facts", "ssh_options", "defaults", "entrypoints", "jumps", "groups", "company_managed", "access_policies"):
|
|
| 26 |
+ if key not in data: |
|
| 27 |
+ continue |
|
| 28 |
+ if key not in merged: |
|
| 29 |
+ merged[key] = {}
|
|
| 30 |
+ if key in ("facts", "defaults", "company_managed"):
|
|
| 31 |
+ merged[key].update(data[key]) |
|
| 32 |
+ elif key in ("ssh_options", "entrypoints", "jumps", "groups", "access_policies"):
|
|
| 33 |
+ merged[key].update(data[key]) |
|
| 34 |
+ if "version" not in merged: |
|
| 35 |
+ merged["version"] = 1 |
|
| 36 |
+ return merged |
|
| 37 |
+ |
|
| 38 |
+ |
|
| 20 | 39 |
def fmt_bool(value: bool) -> str: |
| 21 | 40 |
return "yes" if value else "no" |
| 22 | 41 |
|
@@ -235,11 +254,17 @@ def generate(data, output_dir: Path): |
||
| 235 | 254 |
|
| 236 | 255 |
def main(): |
| 237 | 256 |
parser = argparse.ArgumentParser() |
| 238 |
- parser.add_argument("--inventory", default=ROOT / "inventory" / "hosts.yaml", type=Path)
|
|
| 257 |
+ parser.add_argument("--inventory", action="append", type=Path, help="Inventory file(s) to load (can be used multiple times)")
|
|
| 239 | 258 |
parser.add_argument("--output-dir", default=ROOT / "generated", type=Path)
|
| 240 | 259 |
args = parser.parse_args() |
| 241 | 260 |
|
| 242 |
- generate(load_inventory(args.inventory), args.output_dir) |
|
| 261 |
+ inventories = args.inventory if args.inventory else [ROOT / "inventory" / "hosts.yaml"] |
|
| 262 |
+ local_inventory = ROOT / "inventory" / "hosts-local.yaml" |
|
| 263 |
+ if local_inventory.exists() and local_inventory not in inventories: |
|
| 264 |
+ inventories.append(local_inventory) |
|
| 265 |
+ |
|
| 266 |
+ data = merge_inventories(inventories) if len(inventories) > 1 else load_inventory(inventories[0]) |
|
| 267 |
+ generate(data, args.output_dir) |
|
| 243 | 268 |
|
| 244 | 269 |
|
| 245 | 270 |
if __name__ == "__main__": |