Get IP address via Qemu Agent (KVM)
This commit is contained in:
parent
d9541cc1c8
commit
2bd15b1bf7
34
proxmox.py
34
proxmox.py
|
@ -33,6 +33,7 @@ except ImportError:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import socket
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
@ -190,7 +191,32 @@ class ProxmoxAPI(object):
|
||||||
|
|
||||||
def pool(self, poolid):
|
def pool(self, poolid):
|
||||||
return ProxmoxPool(self.get('api2/json/pools/{0}'.format(poolid)))
|
return ProxmoxPool(self.get('api2/json/pools/{0}'.format(poolid)))
|
||||||
|
|
||||||
|
def qemu_agent(self, node, vm):
|
||||||
|
try:
|
||||||
|
info = self.get('api2/json/nodes/{0}/qemu/{1}/agent/info'.format(node, vm))
|
||||||
|
if info is not None:
|
||||||
|
return True
|
||||||
|
except HTTPError as error:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def qemu_ip_address(self, node, vm):
|
||||||
|
ip_address = None
|
||||||
|
networks = self.get('api2/json/nodes/{0}/qemu/{1}/agent/network-get-interfaces'.format(node, vm))['result']
|
||||||
|
if networks:
|
||||||
|
for network in networks:
|
||||||
|
for address in network['ip-addresses']:
|
||||||
|
ip_address = address['ip-address']
|
||||||
|
try:
|
||||||
|
# IP address validation
|
||||||
|
if socket.inet_aton(ip_address):
|
||||||
|
# Ignore localhost
|
||||||
|
if ip_address != '127.0.0.1':
|
||||||
|
return ip_address
|
||||||
|
except socket.error:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
def version(self):
|
def version(self):
|
||||||
return ProxmoxVersion(self.get('api2/json/version'))
|
return ProxmoxVersion(self.get('api2/json/version'))
|
||||||
|
|
||||||
|
@ -255,7 +281,11 @@ def main_list(options, config_path):
|
||||||
metadata = {
|
metadata = {
|
||||||
'notes': description
|
'notes': description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# If Qemu Agent is enabled, try to guess the IP address
|
||||||
|
if proxmox_api.qemu_agent(node, vmid):
|
||||||
|
results['_meta']['hostvars'][vm]['ansible_host'] = proxmox_api.qemu_ip_address(node, vmid)
|
||||||
|
|
||||||
if 'groups' in metadata:
|
if 'groups' in metadata:
|
||||||
# print metadata
|
# print metadata
|
||||||
for group in metadata['groups']:
|
for group in metadata['groups']:
|
||||||
|
|
Reference in New Issue