forked from device42/ansible_device42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd42_ansible_dynamic_inventory.py
50 lines (37 loc) · 1.18 KB
/
d42_ansible_dynamic_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
#!/usr/bin/env python
import argparse
import imp
import sys
from lib import *
conf = imp.load_source('conf', 'conf')
try:
import json
except ImportError:
import simplejson as json
class Inventory(object):
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action='store_true')
self.args = parser.parse_args()
# Called with `--list`.
if self.args.list:
self.inventory = self.inventory()
# Called with `--host [hostname]`.
# If no groups or vars are present, return an empty inventory.
else:
self.inventory = self.empty_inventory()
print json.dumps(self.inventory)
# Example inventory for testing.
def inventory(self):
rest = REST(conf)
returned_devices = rest.get_devices()
if conf.GROUP_BY not in available_groups:
print '\n[!] ERROR: wrong grouping name'
sys.exit()
ansible = Ansible(conf)
return ansible.get_grouping(returned_devices)
# Empty inventory for testing.
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
# Get the inventory.
Inventory()