2014-10-06 15:12:03 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (C) 2014 Mathieu GAUTHIER-LAFAYE <gauthierl@lapth.cnrs.fr>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-24 08:57:04 +00:00
|
|
|
# Updated 2016 by Matt Harris <matthaeus.harris@gmail.com>
|
|
|
|
#
|
|
|
|
# Added support for Proxmox VE 4.x
|
|
|
|
# Added support for using the Notes field of a VM to define groups and variables:
|
|
|
|
# A well-formatted JSON object in the Notes field will be added to the _meta
|
|
|
|
# section for that VM. In addition, the "groups" key of this JSON object may be
|
|
|
|
# used to specify group membership:
|
|
|
|
#
|
|
|
|
# { "groups": ["utility", "databases"], "a": false, "b": true }
|
|
|
|
|
2018-08-10 16:13:29 +00:00
|
|
|
import json
|
2014-10-06 15:12:03 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2019-04-09 17:17:59 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
2015-09-03 06:23:27 +00:00
|
|
|
|
2015-09-15 14:58:52 +00:00
|
|
|
from ansible.module_utils.urls import open_url
|
2015-09-03 06:23:27 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
class ProxmoxNodeList(list):
|
|
|
|
def get_names(self):
|
|
|
|
return [node['node'] for node in self]
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2016-01-24 08:57:04 +00:00
|
|
|
class ProxmoxVM(dict):
|
2014-10-07 11:45:41 +00:00
|
|
|
def get_variables(self):
|
|
|
|
variables = {}
|
2015-09-03 06:23:27 +00:00
|
|
|
for key, value in iteritems(self):
|
2014-10-07 11:45:41 +00:00
|
|
|
variables['proxmox_' + key] = value
|
|
|
|
return variables
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2016-01-24 08:57:04 +00:00
|
|
|
class ProxmoxVMList(list):
|
2017-09-12 07:11:13 +00:00
|
|
|
def __init__(self, data=None):
|
|
|
|
data = [] if data is None else data
|
|
|
|
|
2014-10-07 11:45:41 +00:00
|
|
|
for item in data:
|
2016-01-24 08:57:04 +00:00
|
|
|
self.append(ProxmoxVM(item))
|
2014-10-07 11:45:41 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
def get_names(self):
|
2016-01-24 08:57:04 +00:00
|
|
|
return [vm['name'] for vm in self if vm['template'] != 1]
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2014-10-07 11:10:10 +00:00
|
|
|
def get_by_name(self, name):
|
2016-01-24 08:57:04 +00:00
|
|
|
results = [vm for vm in self if vm['name'] == name]
|
2014-10-07 11:10:10 +00:00
|
|
|
return results[0] if len(results) > 0 else None
|
|
|
|
|
2014-10-07 11:45:41 +00:00
|
|
|
def get_variables(self):
|
|
|
|
variables = {}
|
2016-01-24 08:57:04 +00:00
|
|
|
for vm in self:
|
|
|
|
variables[vm['name']] = vm.get_variables()
|
2014-10-07 11:45:41 +00:00
|
|
|
|
|
|
|
return variables
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
class ProxmoxPoolList(list):
|
|
|
|
def get_names(self):
|
|
|
|
return [pool['poolid'] for pool in self]
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
class ProxmoxPool(dict):
|
|
|
|
def get_members_name(self):
|
|
|
|
return [member['name'] for member in self['members'] if member['template'] != 1]
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
class ProxmoxAPI(object):
|
|
|
|
def __init__(self, options):
|
|
|
|
self.options = options
|
|
|
|
self.credentials = None
|
|
|
|
|
|
|
|
if not options.url:
|
|
|
|
raise Exception('Missing mandatory parameter --url (or PROXMOX_URL).')
|
|
|
|
elif not options.username:
|
|
|
|
raise Exception('Missing mandatory parameter --username (or PROXMOX_USERNAME).')
|
|
|
|
elif not options.password:
|
|
|
|
raise Exception('Missing mandatory parameter --password (or PROXMOX_PASSWORD).')
|
|
|
|
|
|
|
|
def auth(self):
|
2018-01-10 20:03:25 +00:00
|
|
|
request_path = '{0}api2/json/access/ticket'.format(self.options.url)
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2017-05-19 17:22:16 +00:00
|
|
|
request_params = urlencode({
|
2014-10-06 15:12:03 +00:00
|
|
|
'username': self.options.username,
|
|
|
|
'password': self.options.password,
|
|
|
|
})
|
|
|
|
|
2015-09-15 14:58:52 +00:00
|
|
|
data = json.load(open_url(request_path, data=request_params))
|
2014-10-06 15:12:03 +00:00
|
|
|
|
|
|
|
self.credentials = {
|
|
|
|
'ticket': data['data']['ticket'],
|
|
|
|
'CSRFPreventionToken': data['data']['CSRFPreventionToken'],
|
|
|
|
}
|
|
|
|
|
|
|
|
def get(self, url, data=None):
|
2018-01-10 20:03:25 +00:00
|
|
|
request_path = '{0}{1}'.format(self.options.url, url)
|
2015-09-15 14:58:52 +00:00
|
|
|
|
2018-01-10 20:03:25 +00:00
|
|
|
headers = {'Cookie': 'PVEAuthCookie={0}'.format(self.credentials['ticket'])}
|
2015-09-15 14:58:52 +00:00
|
|
|
request = open_url(request_path, data=data, headers=headers)
|
2014-10-06 15:12:03 +00:00
|
|
|
|
|
|
|
response = json.load(request)
|
|
|
|
return response['data']
|
|
|
|
|
|
|
|
def nodes(self):
|
|
|
|
return ProxmoxNodeList(self.get('api2/json/nodes'))
|
|
|
|
|
2016-01-24 08:57:04 +00:00
|
|
|
def vms_by_type(self, node, type):
|
2018-01-10 20:03:25 +00:00
|
|
|
return ProxmoxVMList(self.get('api2/json/nodes/{0}/{1}'.format(node, type)))
|
2016-01-24 08:57:04 +00:00
|
|
|
|
|
|
|
def vm_description_by_type(self, node, vm, type):
|
2018-01-10 20:03:25 +00:00
|
|
|
return self.get('api2/json/nodes/{0}/{1}/{2}/config'.format(node, type, vm))
|
2016-01-24 08:57:04 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
def node_qemu(self, node):
|
2016-01-24 08:57:04 +00:00
|
|
|
return self.vms_by_type(node, 'qemu')
|
|
|
|
|
|
|
|
def node_qemu_description(self, node, vm):
|
|
|
|
return self.vm_description_by_type(node, vm, 'qemu')
|
|
|
|
|
|
|
|
def node_lxc(self, node):
|
|
|
|
return self.vms_by_type(node, 'lxc')
|
|
|
|
|
|
|
|
def node_lxc_description(self, node, vm):
|
|
|
|
return self.vm_description_by_type(node, vm, 'lxc')
|
2014-10-06 15:12:03 +00:00
|
|
|
|
|
|
|
def pools(self):
|
|
|
|
return ProxmoxPoolList(self.get('api2/json/pools'))
|
|
|
|
|
|
|
|
def pool(self, poolid):
|
2018-01-10 20:03:25 +00:00
|
|
|
return ProxmoxPool(self.get('api2/json/pools/{0}'.format(poolid)))
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
def main_list(options):
|
2014-10-07 11:45:41 +00:00
|
|
|
results = {
|
|
|
|
'all': {
|
|
|
|
'hosts': [],
|
|
|
|
},
|
|
|
|
'_meta': {
|
|
|
|
'hostvars': {},
|
|
|
|
}
|
|
|
|
}
|
2014-10-06 15:12:03 +00:00
|
|
|
|
|
|
|
proxmox_api = ProxmoxAPI(options)
|
|
|
|
proxmox_api.auth()
|
|
|
|
|
|
|
|
for node in proxmox_api.nodes().get_names():
|
2014-10-07 11:45:41 +00:00
|
|
|
qemu_list = proxmox_api.node_qemu(node)
|
|
|
|
results['all']['hosts'] += qemu_list.get_names()
|
|
|
|
results['_meta']['hostvars'].update(qemu_list.get_variables())
|
2016-01-24 08:57:04 +00:00
|
|
|
lxc_list = proxmox_api.node_lxc(node)
|
|
|
|
results['all']['hosts'] += lxc_list.get_names()
|
|
|
|
results['_meta']['hostvars'].update(lxc_list.get_variables())
|
|
|
|
|
|
|
|
for vm in results['_meta']['hostvars']:
|
|
|
|
vmid = results['_meta']['hostvars'][vm]['proxmox_vmid']
|
|
|
|
try:
|
|
|
|
type = results['_meta']['hostvars'][vm]['proxmox_type']
|
|
|
|
except KeyError:
|
|
|
|
type = 'qemu'
|
|
|
|
try:
|
|
|
|
description = proxmox_api.vm_description_by_type(node, vmid, type)['description']
|
|
|
|
except KeyError:
|
|
|
|
description = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
metadata = json.loads(description)
|
|
|
|
except TypeError:
|
|
|
|
metadata = {}
|
2016-01-24 09:10:47 +00:00
|
|
|
except ValueError:
|
|
|
|
metadata = {
|
|
|
|
'notes': description
|
|
|
|
}
|
2016-01-24 08:57:04 +00:00
|
|
|
|
|
|
|
if 'groups' in metadata:
|
|
|
|
# print metadata
|
|
|
|
for group in metadata['groups']:
|
|
|
|
if group not in results:
|
|
|
|
results[group] = {
|
|
|
|
'hosts': []
|
|
|
|
}
|
|
|
|
results[group]['hosts'] += [vm]
|
|
|
|
|
|
|
|
results['_meta']['hostvars'][vm].update(metadata)
|
2014-10-06 15:12:03 +00:00
|
|
|
|
|
|
|
# pools
|
|
|
|
for pool in proxmox_api.pools().get_names():
|
2014-10-07 11:45:41 +00:00
|
|
|
results[pool] = {
|
|
|
|
'hosts': proxmox_api.pool(pool).get_members_name(),
|
|
|
|
}
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2014-10-07 11:58:01 +00:00
|
|
|
return results
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-07 11:10:10 +00:00
|
|
|
def main_host(options):
|
|
|
|
proxmox_api = ProxmoxAPI(options)
|
|
|
|
proxmox_api.auth()
|
|
|
|
|
|
|
|
for node in proxmox_api.nodes().get_names():
|
|
|
|
qemu_list = proxmox_api.node_qemu(node)
|
|
|
|
qemu = qemu_list.get_by_name(options.host)
|
|
|
|
if qemu:
|
2014-10-07 11:58:01 +00:00
|
|
|
return qemu.get_variables()
|
2014-10-07 11:10:10 +00:00
|
|
|
|
2014-10-07 11:58:01 +00:00
|
|
|
return {}
|
2014-10-06 15:12:03 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
def main():
|
|
|
|
parser = OptionParser(usage='%prog [options] --list | --host HOSTNAME')
|
|
|
|
parser.add_option('--list', action="store_true", default=False, dest="list")
|
|
|
|
parser.add_option('--host', dest="host")
|
|
|
|
parser.add_option('--url', default=os.environ.get('PROXMOX_URL'), dest='url')
|
|
|
|
parser.add_option('--username', default=os.environ.get('PROXMOX_USERNAME'), dest='username')
|
|
|
|
parser.add_option('--password', default=os.environ.get('PROXMOX_PASSWORD'), dest='password')
|
2014-10-07 11:58:01 +00:00
|
|
|
parser.add_option('--pretty', action="store_true", default=False, dest='pretty')
|
2014-10-06 15:12:03 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.list:
|
2014-10-07 11:58:01 +00:00
|
|
|
data = main_list(options)
|
2014-10-06 15:12:03 +00:00
|
|
|
elif options.host:
|
2014-10-07 11:58:01 +00:00
|
|
|
data = main_host(options)
|
2014-10-06 15:12:03 +00:00
|
|
|
else:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-10-07 11:58:01 +00:00
|
|
|
indent = None
|
|
|
|
if options.pretty:
|
|
|
|
indent = 2
|
|
|
|
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(data, indent=indent))
|
2014-10-07 11:45:41 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-06 15:12:03 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|