2026-02-28 20:24:55 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_terraform_outputs():
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
["terraform", "output", "-json"],
|
|
|
|
|
cwd="../terraform",
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0:
|
|
|
|
|
print(f"Error running terraform output: {result.stderr}")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
return json.loads(result.stdout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
outputs = get_terraform_outputs()
|
|
|
|
|
|
2026-03-01 04:04:56 +00:00
|
|
|
control_plane_names = outputs["control_plane_names"]["value"]
|
2026-03-01 03:08:56 +00:00
|
|
|
control_plane_ips = outputs["control_plane_ips"]["value"]
|
|
|
|
|
control_plane_private_ips = outputs["control_plane_private_ips"]["value"]
|
2026-03-01 04:04:56 +00:00
|
|
|
worker_names = outputs["worker_names"]["value"]
|
2026-03-01 03:08:56 +00:00
|
|
|
worker_ips = outputs["worker_ips"]["value"]
|
|
|
|
|
worker_private_ips = outputs["worker_private_ips"]["value"]
|
|
|
|
|
|
|
|
|
|
control_planes = [
|
|
|
|
|
{
|
2026-03-01 04:04:56 +00:00
|
|
|
"name": name,
|
2026-03-01 14:08:08 +00:00
|
|
|
"public_ip": public_ip,
|
2026-03-01 03:08:56 +00:00
|
|
|
"private_ip": private_ip,
|
|
|
|
|
}
|
2026-03-01 04:04:56 +00:00
|
|
|
for name, public_ip, private_ip in zip(
|
|
|
|
|
control_plane_names, control_plane_ips, control_plane_private_ips
|
2026-03-01 03:08:56 +00:00
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
workers = [
|
|
|
|
|
{
|
2026-03-01 04:04:56 +00:00
|
|
|
"name": name,
|
2026-03-01 14:08:08 +00:00
|
|
|
"public_ip": public_ip,
|
2026-03-01 03:08:56 +00:00
|
|
|
"private_ip": private_ip,
|
|
|
|
|
}
|
2026-03-01 04:04:56 +00:00
|
|
|
for name, public_ip, private_ip in zip(
|
|
|
|
|
worker_names, worker_ips, worker_private_ips
|
|
|
|
|
)
|
2026-03-01 03:08:56 +00:00
|
|
|
]
|
|
|
|
|
|
2026-02-28 20:24:55 +00:00
|
|
|
data = {
|
2026-03-01 03:08:56 +00:00
|
|
|
"control_planes": control_planes,
|
|
|
|
|
"workers": workers,
|
2026-02-28 20:24:55 +00:00
|
|
|
"private_key_file": outputs["ssh_private_key_path"]["value"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env = Environment(loader=FileSystemLoader("."))
|
|
|
|
|
template = env.get_template("inventory.tmpl")
|
|
|
|
|
inventory = template.render(**data)
|
|
|
|
|
|
|
|
|
|
Path("inventory.ini").write_text(inventory)
|
|
|
|
|
print("Generated inventory.ini")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|