-
Notifications
You must be signed in to change notification settings - Fork 3
/
ansible-groups
executable file
·45 lines (33 loc) · 1.07 KB
/
ansible-groups
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.6
from subprocess import call, sys, os
from ansible.parsing.dataloader import DataLoader
from ansible.inventory import Inventory
from ansible.vars import VariableManager
def usage():
prog = os.path.basename(__file__)
sys.stderr.write('Usage: {0} PATTERN\n'.format(prog))
sys.exit(1)
def get_groups(pattern):
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader, variable_manager=variable_manager)
groups = inventory.get_groups()
if len(pattern) == 0:
return groups
else:
return [group for group in groups if pattern in group.lower()]
def main(pattern):
groups = get_groups(pattern)
if len(groups) == 0:
sys.stderr.write('No groups found.\n')
sys.exit(1)
print('\n'.join(sorted([group for group in groups])))
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-h':
usage()
else:
pattern = ' '.join(sys.argv[1:])
try:
main(pattern)
except KeyboardInterrupt:
pass