-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansigraph.py
67 lines (52 loc) · 2.11 KB
/
ansigraph.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
import networkx as nx
import json
class ansigraph:
def __init__(self):
self.graph = nx.DiGraph()
self.inventory = {}
def add_host(self, host, group):
if isinstance(host, basestring):
self.graph.add_node(host, type='host')
self.graph.add_edge(group,host)
else:
self.graph.add_nodes_from(host, type='host')
self.add_group(group)
for item in host:
self.graph.add_edge(group, item)
def add_group(self, group):
self.graph.add_node(group, type='group')
def add_child_group(self, parent, child):
self.graph.add_node(parent, type='group')
self.graph.add_node(child, type='group')
self.graph.add_edge(parent,child)
def add_parent_group(self, parent, child):
self.graph.add_edge(parent,child)
def get_hosts(self, group=None, recurse=True):
if group:
if recurse:
tree = nx.DiGraph(list(nx.dfs_edges(self.graph,group)))
return [x for x in tree.nodes() if self.graph.node[x]['type'] == 'host' ]
else:
return [x for x in list(self.graph.successors(group)) if self.graph.node[x]['type'] == 'host']
else:
return [x for x in self.graph.nodes() if self.graph.node[x]['type'] == 'host']
def build_inventory(self, start):
parents = self.graph.predecessors(start)
if parents:
for parent in parents:
if parent not in self.inventory.keys():
self.inventory[parent] = {
'children': [],
'hosts': []
}
if self.graph.node[start]['type'] == 'host':
if start not in self.inventory[parent]['hosts']:
self.inventory[parent]['hosts'].append(start)
elif self.graph.node[start]['type'] == 'group':
if start not in self.inventory[parent]['children']:
self.inventory[parent]['children'].append(start)
self.build_inventory(parent)
def dump_json(self, hosts):
for host in hosts:
self.build_inventory(host)
return json.dumps(self.inventory)