-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaultinventory.py
executable file
·84 lines (64 loc) · 2.06 KB
/
vaultinventory.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
#!/usr/bin/env python
import argparse
import json
import requests
import os
class VaultClient():
def __init__(self, addr, token):
self.addr = addr
self.token = token
def list(self, path):
resp = requests.request(
method = 'LIST',
url = self.addr + '/v1/' + path,
headers = { "X-Vault-Token": self.token })
if resp.status_code == 200:
return resp.json()['data']['keys']
return {}
def read(self, path):
resp = requests.request(
method = 'GET',
url = self.addr + '/v1/' + path,
headers = { "X-Vault-Token": self.token })
if resp.status_code == 200:
return resp.json()['data']
return {}
class VaultInventory():
def __init__(self):
self.inventory = {
'all': {
'hosts': [], 'children': []
},
'_meta': {
'hostvars': {}
}
}
addr = os.environ['VAULT_ADDR']
token = os.environ['VAULT_TOKEN']
if not addr or not token:
return False
self.client = VaultClient(
addr = addr, token = token)
def createInventory(self):
grouplist = self.client.list(path = 'ansible/inventory')
for g in grouplist:
self.inventory[g] = self.client.read(path = 'ansible/inventory/' + g)
self.inventory[g]['vars'] = self.client.read(path = 'ansible/group_vars/' + g)
self.inventory['all']['children'].append(g)
if 'hosts' in self.inventory[g]:
for h in self.inventory[g]['hosts']:
if h not in self.inventory['all']['hosts']:
self.inventory['all']['hosts'].append(h)
self.inventory['_meta']['hostvars'][h] = self.client.read(
path = 'ansible/host_vars/' + h)
self.inventory['all']['vars'] = self.client.read(path = 'ansible/group_vars/all')
def run(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action= 'store_true')
parser.add_argument('--host', action= 'store')
args = parser.parse_args()
if args.list:
self.createInventory()
print(json.dumps(self.inventory))
if __name__ == '__main__':
VaultInventory().run()