forked from colinhoglund/yaml-vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vagrant.py
executable file
·60 lines (53 loc) · 1.87 KB
/
vagrant.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
#!/usr/bin/env python
import argparse
import json
import os
import yaml
from re import sub
def setup_cli():
''' setup CLI '''
parser = argparse.ArgumentParser(description='Ansible/Vagrant Dynamic Inventory Script')
group = parser.add_mutually_exclusive_group()
group.add_argument('--list', action="store_true", help='Return JSON for all groups.')
group.add_argument('--host', help='Return host specific, or empty, JSON')
return parser
def build_inventory(host=None):
''' build inventory from vagrant.yml '''
config = yaml.safe_load(open(os.path.join(os.path.dirname(__file__), 'vagrant.yml')))
domain = config['domain']
inventory = {}
# build groups for a specific host
def _gather_groups(inventory, host):
for group in host['ansible_groups']:
if group not in inventory:
inventory[group] = {}
inventory[group]['hosts'] = []
inventory[group]['vars'] = {}
inventory[group]['hosts'].append(host['name'] + domain)
# append box version
box = sub(r"[/]", "_", config['box'])
if 'box' in host:
box = sub(r"[/]", "_", host['box'])
if box not in inventory:
inventory[box] = {}
inventory[box]['hosts'] = []
inventory[box]['vars'] = {}
inventory[box]['hosts'].append(host['name'] + domain)
# build inventory object
if host:
_gather_groups(inventory, [i for i in config['vms'] if i['name'] == host][0])
else:
for host in config['vms']:
_gather_groups(inventory, host)
return inventory
def main():
''' main function '''
parser = setup_cli()
args = parser.parse_args()
if args.list:
print(json.dumps(build_inventory()))
elif args.host:
print(json.dumps(build_inventory(args.host)))
else:
parser.print_help()
main()