blob: 62f29d8429190d74bdbd2897d928c841ddf88625 (
plain)
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
|
import yaml
import sys
from jinja2 import Environment
INVENTORY_TEMPLATE = """
[all]
{% for host, ip in hosts.iteritems() %}
{{ host }} ansible_ssh_host={{ ip }} ansible_ssh_pass=root ansible_user=root
{% endfor %}
[kube-master]
host1
host2
[etcd]
host1
host2
host3
[kube-node]
host2
host3
host4
host5
[k8s-cluster:children]
kube-node
kube-master
[calico-rr]
[vault]
"""
def create_inventory_file(inventories_path, hosts):
content = Environment().from_string(INVENTORY_TEMPLATE).render(hosts=hosts)
with open(inventories_path, 'w+') as f:
f.write(content)
def fetch_all_sorted_external_ip(ip_cfg):
hosts = {}
for host, settings in ip_cfg.iteritems():
external = settings["external"]["ip"]
hosts[host] = external
return hosts
def main(inventories_path, ip_cfg):
hosts = fetch_all_sorted_external_ip(ip_cfg)
create_inventory_file(inventories_path, hosts)
if __name__ == "__main__":
path = yaml.load(sys.argv[1])
ipv_cfg = yaml.load(sys.argv[2])
main(path, ipv_cfg)
|