-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.py
executable file
·88 lines (77 loc) · 2.91 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
Based on Etcd structure which root key is hostvars, hostvars will return
"hostvars": {
"192.168.1.182": {
"labels": {
"redis": ""
},
"hostname": "m-web-1-182-prd"
},
"192.168.1.215": {
"hostname": "www-db-1-215-prd"
},
...
'''
import yaml
import json
import etcd
class EtcdInventory():
def __init__(self):
self.inventory = {}
config = self.read_settings()
client = etcd.Client(host=config.get('etcd_host'), port=int(config.get('etcd_port')))
self.get_hostvars(client)
self.iterate_hosts()
print json.dumps(self.inventory)
def assemble_elements(self, my_string):
if my_string is None:
return []
else:
elements = my_string.replace('-','.').split('.')
elements = filter(lambda s: not str(s).isdigit(), elements)
return elements
def read_settings(self):
config = yaml.safe_load(open("vars/etcd_host.yml"))
return config
def get_hostvars(self, etcd):
# hostvars is inventory _meta
my_hostvars = etcd.read('/hostvars', recursive=True)
self.hostvars = self.parse_hostvars(my_hostvars._children)
self.inventory["_meta"] = {'hostvars': self.hostvars}
def parse_hostvars(self, data):
hostvars = {}
for d in data:
key = d['key'].split('/')[-1]
if key == '':
key = '/'
if 'dir' in d and d['dir'] and 'nodes' in d:
hostvars[key] = self.parse_hostvars(d['nodes'])
else:
if 'value' not in d:
d['value'] = ''
hostvars[key] = d['value']
return hostvars
def iterate_hosts(self):
''' iterate hosts( i.e. ip_address) contains 2 parts '''
for (ip_address, value) in self.hostvars.items():
if isinstance(value, dict):
# part 1: hostname as group name, One-to-One
if(value.get('hostname') != 'localhost'):
self.inventory[value.get('hostname')] = { "hosts": [ip_address] }
# part 2: self defined elements as group name, Many-to-Many
elements = []
if(value.get('labels')):
my_labels = value.get('labels').keys()
elements += my_labels
elements = elements + self.assemble_elements(value.get('hostname'))
for my_element in elements:
self.inventory.setdefault(my_element, {})
self.inventory[my_element].setdefault("hosts", []).append(ip_address)
host_list = list(set(self.inventory[my_element]["hosts"]))
self.inventory[my_element] = { "hosts" : host_list }
else:
continue
# Run
EtcdInventory()