This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
one-inv
executable file
·104 lines (83 loc) · 3.37 KB
/
one-inv
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------- #
# Copyright 2020, FeldHost™ (feldhost.net) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# -------------------------------------------------------------------------- #
import sys
import argparse
import json
import pyone
import functions
import config
# ----------------------------
# Define parser
# ----------------------------
parser = argparse.ArgumentParser(description='OpenNebula Ansible Dynamic Inventory')
parser.add_argument('--list', help='Get list of all groups', action='store_true')
parser.add_argument('--host', help='Get specific host vars')
# -------------------------------------
# Print help if no args passed
# -------------------------------------
if len(sys.argv[1:]) == 0:
parser.print_help()
parser.exit()
# -------------------------------------
# Parse args and proceed with execution
# -------------------------------------
args = parser.parse_args()
# -----------------------
# Connect to OpenNebula
# -----------------------
one = pyone.OneServer(config.ONE['address'], session='%s:%s' % (config.ONE['username'], config.ONE['password']))
# Init vars
groups = {}
groups['All'] = []
hosts = {}
# get hosts (VMs)
vm_pool = one.vmpool.infoextended(-2, -1, -1, 3)
# iterate over hosts
for vm in vm_pool.VM:
labels = []
if vm.USER_TEMPLATE.get('LABELS'):
labels = vm.USER_TEMPLATE.get('LABELS').split(',')
# get hostname
hostname = functions.get_hostname(vm)
for label in labels:
# skips hosts by excluded labels
if label in config.SKIP_LABELS:
continue
# create new group if doesn't exist
if not groups.get(label):
groups[label] = []
# add host to group
groups[label].append(hostname)
# add host to default group
groups['All'].append(hostname)
# save host vars
hosts[hostname] = vm.USER_TEMPLATE
# custom ssh port
ssh_port = functions.get_ssh_port(vm)
if ssh_port:
hosts[hostname]['ansible_port'] = ssh_port
# prepare final groups data
groups['_meta'] = {}
groups['_meta']['hostvars'] = hosts
if args.list:
print(json.dumps(groups, sort_keys=False, indent=4, separators=(',', ': ')))
if args.host:
if not args.host in hosts:
print('Host with name %s does\'t exists!' % args.host)
exit(1)
print(json.dumps(hosts[args.host], sort_keys=False, indent=4, separators=(',', ': ')))