-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.py
executable file
·45 lines (29 loc) · 900 Bytes
/
hosts.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
#!/usr/bin/env python3
from argparse import ArgumentParser
from collections import defaultdict
import json
import socket
GROUPS = {
'home': ['area11', 'osmc', 'shinkiro', 'sibylpad'],
}
def main(host, list_all):
groups = {}
all_hosts = set()
for (group, hosts) in GROUPS.items():
groups[group] = sorted(hosts)
all_hosts.update(hosts)
groups['all'] = {'children': ['ungrouped'] + list(groups)}
groups['_meta'] = {'hostvars': {}}
for host in all_hosts:
config = groups['_meta']['hostvars'].setdefault(host, {})
if host == socket.gethostname():
config['ansible_connection'] = 'local'
print(json.dumps(groups, indent=4, sort_keys=True))
def get_args():
ap = ArgumentParser()
add = ap.add_argument
add('--host')
add('--list', action='store_true', dest='list_all')
return ap.parse_args()
if __name__ == '__main__':
main(**vars(get_args()))