-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnb_path.py
executable file
·56 lines (42 loc) · 1.55 KB
/
nb_path.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
#!/usr/bin/env python
import argparse
import requests
import sys
try:
import json
except ImportError:
import simplejson as json
class NetBrainInventory(object):
"""NetBrain as a dynamic inventory for Ansible.
Retrieves hosts list from netbrain API and returns Ansible dynamic inventory (JSON).
"""
def __init__(self, url):
self.api_url = url
self.inventory = {}
self.read_cli_args()
# Called with `--list`.
if self.args.list:
self.inventory = self.get_inventory()
# Called with `--host [hostname]`.
elif self.args.host:
# Not implemented, since we return _meta info `--list`.
self.inventory = self.empty_inventory()
# If no groups or vars are present, return an empty inventory.
else:
self.inventory = self.empty_inventory()
print(json.dumps(self.inventory))
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
def get_inventory(self):
if not self.api_url:
sys.exit("Please provide a url")
api_output = requests.get(self.api_url)
api_output.raise_for_status()
api_output_data = api_output.json()
return api_output_data
def read_cli_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action='store_true')
parser.add_argument('--host', action='store')
self.args = parser.parse_args()
NetBrainInventory("https://raw.githubusercontent.com/yushihui/ansible_inventory/master/sample.json")