-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_inventory.py
62 lines (47 loc) · 1.6 KB
/
generate_inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import json
def convert_to_ini(json_data):
ini_content = "[all]\n"
control_plane_count = 1
worker_count = 1
for node_type, ips in json_data.items():
for ip in ips:
if node_type == "kube-controlplane":
ini_content += (
f"control-plane-{control_plane_count} ansible_host={ip}\n"
)
control_plane_count += 1
elif node_type == "kube-node":
ini_content += f"worker-{worker_count} ansible_host={ip}\n"
worker_count += 1
ini_content += "\n"
ini_content += "[kube_control_plane]\n"
for i in range(1, control_plane_count):
ini_content += f"control-plane-{i}\n"
ini_content += "\n"
ini_content += "[etcd]\n"
for i in range(1, control_plane_count):
ini_content += f"control-plane-{i}\n"
ini_content += "\n"
ini_content += "[kube_node]\n"
for i in range(1, control_plane_count):
ini_content += f"control-plane-{i}\n"
for i in range(1, worker_count):
ini_content += f"worker-{i}\n"
ini_content += "\n"
ini_content += "[calico_rr]\n\n"
ini_content += "\n"
ini_content += "[k8s_cluster:children]\n"
ini_content += "kube_control_plane\n"
ini_content += "kube_node\n"
ini_content += "calico_rr\n"
return ini_content
def main():
# Read JSON data from stdin
json_data = json.load(sys.stdin)
# Convert JSON to INI
ini_content = convert_to_ini(json_data)
# Write the INI content to stdout
sys.stdout.write(ini_content)
if __name__ == "__main__":
main()