-
Notifications
You must be signed in to change notification settings - Fork 34
/
edit_all_cluster.py
58 lines (55 loc) · 1.83 KB
/
edit_all_cluster.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
#!/usr/bin/env python
from lndynamic import LNDynamic
import natsort
import time
# find lunanode credentials
with open(r"/home/hme/.lunanode/commands.txt") as hpass:
lines = hpass.readlines()
api = LNDynamic(lines[0].rstrip('\n'), lines[1].rstrip('\n'))
cluster_name = 'jenkins-pic'
results = api.request('vm', 'list')
f = open(r"/home/hme/inventory_villeurbanne", "w+")
hfile = open(r"/home/hme/user_inventory", "w+")
val = results.get("vms")
user_dic = {}
print
len(val)
for i in range(0, len(val)):
flag = 0
for key, value in val[i].items():
if key == 'name':
if cluster_name not in value:
break
print('name=', value)
user = value
if key == 'primaryip':
ip = value
print('ip=', value)
if key == 'plan_id':
print('plan_id=', value)
if key == 'vm_id':
print('vm_id=', value)
vm_info = api.request('vm', 'info', {'vm_id': value})
st = vm_info.get('info')
try:
print(st['login_details'])
user_login = st['login_details']
a = user_login.split()
print(str(ip), str(a[1]), str(a[3]))
gt = str(a[1])[:-1]
line = "{} ansible_ssh_user={} ansible_ssh_pass={} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'\n".format(
str(ip), str(gt), str(a[3]))
f.write(line)
user_dic[str(user)] = str(ip)
except KeyError as error:
pass
f.close()
# print (user_dic)
list_user = user_dic.keys();
natural = natsort.natsorted(list_user)
# print natural
for vts in range(0, len(natural)):
myip = user_dic[natural[vts]]
user_line = "{} \t {} \t centos \n".format(natural[vts], myip)
hfile.write(user_line)
hfile.close()