-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
64 lines (48 loc) · 1.68 KB
/
mongo.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
61
62
63
64
#!/usr/bin/env python3
import json
import os
from argparse import ArgumentParser
from pymongo import MongoClient
# get mongo
mongo = MongoClient(os.getenv('INV_MONGO_URI'))
db = mongo.get_database(os.getenv('INV_MONGO_DB'))
# define parser
parser = ArgumentParser()
parser.add_argument('--list',
action="store_true",
help='Print a JSON-encoded hash or dictionary '
'that contains all the groups to be managed')
parser.add_argument('--host',
metavar='hostname',
nargs=1,
help='Print either an empty JSON hash/dictionary, '
'or a hash/dictionary of variables')
def main():
args = parser.parse_args()
if args.list:
return get_inventory()
if args.host:
return get_host(args.host[0])
return {}
def get_inventory():
""" Get all inventory. """
inv = get_data('groups')
if os.getenv('INV_INCLUDE_META'):
inv['_meta'] = {'hostvars': get_data('hosts')}
return inv
def get_host(name):
""" Get dict with one host variables. """
col = db.get_collection('hosts')
data = col.find_one({'name': name}) or {}
return _prepare(data)
def get_data(collection):
""" Get all items from collection and return object {name: dict_with_other_vars}. """
col = db.get_collection(collection)
return {data['name']: _prepare(data) for data in col.find()}
def _prepare(data: dict):
""" Remove fields `_id` and `name` and return data. """
return {k: v for k, v in data.items() if k not in ('_id', 'name')}
if __name__ == "__main__":
result = main()
print(json.dumps(result))
mongo.close()