-
Notifications
You must be signed in to change notification settings - Fork 1
/
inventory.py
executable file
·227 lines (189 loc) · 8.12 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
import argparse
import os
import re
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
def _get_ssh_params(inventory_file_name):
hosts = {}
data_loader = DataLoader()
inventory = InventoryManager(loader=data_loader,
sources=[inventory_file_name])
for host, val in inventory.hosts.items():
vars = val.get_vars()
hosts[host] = {
"HostName": vars.get("ansible_host", host),
"Port": vars.get("ansible_port"),
"User": vars.get("ansible_user"),
"SshPrivateKeyFile": vars.get("ansible_ssh_private_key_file"),
"Options": vars.get("ansible_ssh_common_args")
}
return hosts
def _main(mode, inventory_path, target_client=None):
clients = {}
inventory_path_full = os.path.expanduser(inventory_path)
for root, dirs, files in os.walk(inventory_path_full):
for name in files:
basename = os.path.basename(root)
if (basename not in ["vaults", "vars", "group_vars", "host_vars"]):
client = root.replace(inventory_path_full,
"").split(os.sep)[0].split("-")[0]
if mode == "all":
clients.setdefault(client, {}).update(
_get_ssh_params(os.path.join(root, name)))
elif mode == "clients_only":
clients.setdefault(client, {})
elif mode == "client_hosts":
if client == target_client:
clients.setdefault(client, {}).update(
_get_ssh_params(os.path.join(root, name)))
return clients
def _get_flat_hosts(client_hosts):
flat_list = []
for sublist in client_hosts.values():
for item in sublist.keys():
flat_list.append(item)
return flat_list
def _replace_ansible_host_with_ip(client_hosts, ssh_opts):
proxycommand_re = re.compile(r"ProxyCommand=[\"|'](.*)[\"|']")
host_re = re.compile(r"([a-zA-Z][\w\.-]+)")
proxycommand = proxycommand_re.findall(ssh_opts)
if proxycommand:
host = host_re.findall(proxycommand[0])
if "ssh" in host:
host.remove("ssh")
if host:
if host[0] in client_hosts:
ip = client_hosts[host[0]]["HostName"]
ssh_opts = ssh_opts.replace(host[0], ip)
return ssh_opts
def get_clients(inventory_path):
print(" ".join(_main("clients_only", inventory_path).keys()))
def get_hosts_all(inventory_path):
client_hosts = _main("all", inventory_path)
print(" ".join(_get_flat_hosts(client_hosts)))
def get_client_hosts(client, inventory_path):
client_hosts = _main("client_hosts", inventory_path, target_client=client)
print(" ".join(client_hosts.get(client, {}).keys()))
def get_client_hosts_all(inventory_path):
client_hosts = _main("all", inventory_path)
clients = client_hosts.keys()
hosts = _get_flat_hosts(client_hosts)
print(" ".join(list(clients) + hosts))
def get_ssh_string(host, inventory_path, client=None, quote_opts_quotes=False):
if client:
client_hosts = _main("client_hosts",
inventory_path,
target_client=client)[client]
else:
client_hosts = {}
for hosts in _main("all", inventory_path).values():
client_hosts.update(hosts)
host_vars = client_hosts.get(host)
if host_vars:
port_str = ("-p " +
str(host_vars["Port"]) if host_vars["Port"] else None)
user_str = ("-l " +
str(host_vars["User"]) if host_vars["User"] else "")
pkey_str = ("-i " + str(host_vars["SshPrivateKeyFile"])
if host_vars["SshPrivateKeyFile"] else None)
ssh_opts = host_vars["Options"]
if host_vars["Options"]:
ssh_opts = _replace_ansible_host_with_ip(client_hosts, ssh_opts)
if quote_opts_quotes:
ssh_opts = re.sub('("[^"]+")', r"'\g<1>'",
ssh_opts.replace("'", '"'))
ssh_args = [
x for x in
[port_str, user_str, pkey_str, ssh_opts, host_vars['HostName']]
if x
]
ssh_str = f"{' '.join(ssh_args)}"
print(ssh_str)
def config_update(config_main, config_dir, inventory_path, client=None):
config_dir_full = os.path.expanduser(config_dir)
if not os.path.isdir(config_dir_full):
try:
os.mkdir(config_dir_full, 0o700)
except OSError:
print(f"Directory creation failed: {config_dir_full}")
if client:
client_hosts = _main("client_hosts",
inventory_path,
target_client=client)
else:
client_hosts = _main("all", inventory_path)
for client, hosts in client_hosts.items():
config_lines = {}
print(f"{client}: {len(hosts)} hosts")
for host, host_vars in hosts.items():
lines = [f"Host {host}"]
print(f"{client}: adding {host} with {host_vars}")
for param, value in host_vars.items():
if value:
lines.append(f" {param} {value}")
if len(lines) > 1:
config_lines[host] = lines.copy()
client_ssh_conf = os.path.join(config_dir_full, client)
with open(client_ssh_conf, "w") as f:
for host, val in config_lines.items():
f.writelines([x + "\n" for x in val])
f.write("\n")
os.chmod(client_ssh_conf, 0o600)
config_main_full = os.path.expanduser(config_main)
relative_dir_path = os.path.relpath(config_dir_full,
os.path.dirname(config_main_full))
option = f"Include {relative_dir_path}/*"
with open(config_main_full, "r") as f:
ssh_config_conts = f.readlines()
if option not in [x.rstrip() for x in ssh_config_conts]:
print(f"Adding '{option}' to {config_main}")
with open(config_main_full, "w") as f:
f.write(option + "\n\n")
f.writelines(ssh_config_conts)
def _argparser():
parser = argparse.ArgumentParser()
parser.add_argument("--inventory",
help="Path to ansible inventory",
required=True)
parser.add_argument("--client", help="Search in client hosts only")
parser.add_argument("--config-dir",
help="Path to the ssh clients config dir",
default="~/.ssh/config.d/")
parser.add_argument("--config-main",
help="Path to the main ssh config",
default="~/.ssh/config")
# workaround for xxh
parser.add_argument("--quote-opts-quotes",
help="Place quoted ssh options in quotes",
action="store_true")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--string", help="Get ssh string")
group.add_argument("--config",
help="Update ssh configs",
action="store_true")
group.add_argument("--completion",
help="Get hosts completion",
choices=["hosts", "clients", "both"])
args = parser.parse_args()
return args
if __name__ == "__main__":
args = _argparser()
if not os.path.exists(args.inventory):
raise FileNotFoundError(f"{args.inventory} does not exist!")
if args.completion:
if args.completion == "hosts":
if args.client:
get_client_hosts(args.client, args.inventory)
else:
get_hosts_all(args.inventory)
elif args.completion == "clients":
get_clients(args.inventory)
elif args.completion == "both":
get_client_hosts_all(args.inventory)
elif args.string:
get_ssh_string(args.string, args.inventory, args.client,
args.quote_opts_quotes)
elif args.config:
config_update(args.config_main, args.config_dir, args.inventory,
args.client)