-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpass_inventory.py
executable file
·139 lines (129 loc) · 7.27 KB
/
pass_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#! /usr/bin/python3
# -*- coding:utf-8 -*-
# This module reads information from a passwordstore database and turns it into an ansible dynamic inventory.
"""
This module reads information from a passwordstore database and turns it into an ansible dynamic inventory.
It gathers information on the passwordstore directory, from the `PASSWORD_STORE_DIR` environment variable.
It assumes that the passwordstore database us organized into the following layers
+ --- providers_1 (hetzner)
| + --- host_1
| | + variable_1
| | + variable_2
| | + variable_3
| |
| + --- host_2
| |
| + variable_1
| + variable_2
| + variable_3
+ --- providers_2 (openstak)
+ --- ansible/inventory
+ --- group_1
| + host_1
| + host_2
| + host_3
|
+ --- group_2
|
+ host_1
+ host_3
+ host_4
"""
from os import walk, listdir, environ, path
from subprocess import Popen, PIPE
import sys
import json
result = {}
result['all'] = {}
result['all']['hosts'] = []
result['all']['vars'] = {}
result['_meta'] = {}
result['_meta']['hostvars'] = {}
# ansible_connection = 'passwordstore'
password_store_dir = environ.get('PASSWORD_STORE_DIR')
f = []
# Navigate through the passwordstore folder
for (dirpath, dirnames, filenames) in walk(password_store_dir):
for (dirname) in dirnames:
# Filter the folders that might contain hosts.
if (dirname in ['hetzner','openstack']):
result[dirname] = {}
result[dirname]['hosts'] = []
# list all folders inside a provider
for (provDirPath, provDirNames, provFileNames) in walk(password_store_dir + '/' + dirname):
for (vmName) in provDirNames:
# Filter out subfolders that don't contain hosts
if (vmName not in ['openshift-accounts', 'console']):
result[dirname]['hosts'].append(vmName)
# Get all hosts for that provider.
for (hostDirPath, hostDirNames, hostFileNames) in walk(password_store_dir + '/' + dirname + '/' + vmName):
# Init host_vars variable with the location of the SSH RSA Private Key
host_vars = {}
for (hostFileName) in hostFileNames:
passEntryName = hostFileName.split('.')[0]
# Esclude some entries that won't be included in the inventory
if ('id_rsa' not in passEntryName and 'os_password' not in passEntryName):
passEntry = dirname +'/' + vmName + '/' + passEntryName
pipe = Popen(['pass', 'show', passEntry], stdout=PIPE, universal_newlines=True)
passLines = pipe.stdout.readlines()
passEntry = passLines[0].replace('\n', '')
if ('os_user' == passEntryName):
host_vars.update({passEntryName:passEntry})
host_vars.update({'ansible_user':passEntry})
elif ('ip_address' == passEntryName):
host_vars.update({passEntryName:passEntry})
if (not 'floating_ip' in host_vars):
host_vars.update({'ansible_ssh_host':passEntry})
elif ('floating_ip' == passEntryName):
host_vars.update({passEntryName:passEntry})
# floating_ip overrides any other host variable
host_vars.update({'ansible_ssh_host':passEntry})
host_vars.update({'floating_ip':passEntry})
elif ('ansible_ssh_host' == passEntryName):
if (not 'ansible_ssh_host' in host_vars):
host_vars.update({'ansible_ssh_host':passEntry})
# elif ('ssh_port' == passEntryName):
# host_vars.update({'ansible_ssh_port':passEntry})
else:
host_vars.update({passEntryName:passEntry})
# for (hostDirName) in hostDirNames:
# if (vmName not in ['openshift-accounts', 'console']):
elif ('id_rsa' in passEntryName):
host_vars.update({'ansible_ssh_private_key_file':'~/.ssh/id_rsa_snowdrop_' + dirname + '_' + vmName})
if ('ansible_ssh_private_key_file' not in host_vars):
host_vars.update({'ansible_ssh_private_key_file':'~/.ssh/id_rsa_snowdrop_' + dirname})
for (hostGroupDirPath, hostGroupDirNames, hostGroupFileNames) in walk(path.join(hostDirPath, 'groups')):
for (hostGroupFileName) in hostGroupFileNames:
hostGroupFileName = hostGroupFileName.split('.')[0]
# print(hostGroupFileName)
if (not hostGroupFileName in result):
result[hostGroupFileName] = {}
result[hostGroupFileName]['hosts'] = []
result[hostGroupFileName]['hosts'].append(vmName)
break
result['_meta']['hostvars'].update({vmName:host_vars})
break
# ansible folder
# elif (dirname == 'ansible'):
# for (ansibleInventoryDirPath, ansibleInventoryGroupNames, ansibleInventoryFileNames) in walk(password_store_dir + '/ansible/inventory'):
# # Each folder is an ansible group
# for (ansibleInventoryGroupName) in ansibleInventoryGroupNames:
# result[ansibleInventoryGroupName] = []
# # Each file inside a group is a host belonging to that group.
# for (hostDirPath, subgroupDirNames, hostFileNames) in walk(password_store_dir + '/ansible/inventory/' + ansibleInventoryGroupName):
# # for (subgroupDirName) in subgroupDirNames:
# # if (subgroupDirName == 'vars' ):
# # TODO: Process group variables in here
# # else
# # TODO: Process as subgroup folder
# for (hostFileName) in hostFileNames:
# result[ansibleInventoryGroupName].append(hostFileName.split('.')[0])
# break
# break
break
if len(sys.argv) == 2 and sys.argv[1] == '--list':
print(json.dumps(result))
elif len(sys.argv) == 3 and sys.argv[1] == '--host':
print(json.dumps(result['_meta']['hostvars'][sys.argv[2]]))
else:
sys.stderr.write("Need an argument, either --list or --host <host>\n")