2012-08-07 00:07:02 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
"""
|
|
|
|
Virt management features
|
|
|
|
|
|
|
|
Copyright 2007, 2012 Red Hat, Inc
|
2012-08-03 01:29:10 +00:00
|
|
|
Michael DeHaan <michael.dehaan@gmail.com>
|
2012-04-04 18:30:24 +00:00
|
|
|
Seth Vidal <skvidal@fedoraproject.org>
|
|
|
|
|
|
|
|
This software may be freely redistributed under the terms of the GNU
|
|
|
|
general public license.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2012-04-13 19:10:45 +00:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-04 18:30:24 +00:00
|
|
|
"""
|
|
|
|
|
2012-09-30 10:40:11 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: virt
|
|
|
|
short_description: Manages virtual machines supported by libvirt
|
|
|
|
description:
|
|
|
|
- Manages virtual machines supported by I(libvirt).
|
2013-11-28 02:23:03 +00:00
|
|
|
version_added: "0.2"
|
2012-09-30 10:40:11 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2013-04-08 20:14:37 +00:00
|
|
|
- name of the guest VM being managed. Note that VM must be previously
|
|
|
|
defined with xml.
|
2012-09-30 10:40:11 +00:00
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Note that there may be some lag for state requests like C(shutdown)
|
|
|
|
since these refer only to VM states. After starting a guest, it may not
|
|
|
|
be immediately accessible.
|
|
|
|
required: false
|
2013-11-27 16:58:34 +00:00
|
|
|
choices: [ "running", "shutdown", "destroyed", "paused" ]
|
2012-09-30 10:40:11 +00:00
|
|
|
default: "no"
|
|
|
|
command:
|
|
|
|
description:
|
|
|
|
- in addition to state management, various non-idempotent commands are available. See examples
|
|
|
|
required: false
|
2013-02-28 23:17:50 +00:00
|
|
|
choices: ["create","status", "start", "stop", "pause", "unpause",
|
|
|
|
"shutdown", "undefine", "destroy", "get_xml", "autostart",
|
2013-04-17 15:46:09 +00:00
|
|
|
"freemem", "list_vms", "info", "nodeinfo", "virttype", "define"]
|
2013-04-08 20:14:37 +00:00
|
|
|
uri:
|
|
|
|
description:
|
|
|
|
- libvirt connection uri
|
|
|
|
required: false
|
2013-04-09 18:47:39 +00:00
|
|
|
defaults: qemu:///system
|
2013-04-17 15:46:09 +00:00
|
|
|
xml:
|
|
|
|
description:
|
|
|
|
- XML document used with the define command
|
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-30 10:40:11 +00:00
|
|
|
requirements: [ "libvirt" ]
|
2012-09-30 12:08:47 +00:00
|
|
|
author: Michael DeHaan, Seth Vidal
|
2012-09-30 10:40:11 +00:00
|
|
|
'''
|
|
|
|
|
2013-04-27 15:20:35 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# a playbook task line:
|
2013-06-14 09:53:43 +00:00
|
|
|
- virt: name=alpha state=running
|
2013-04-27 15:20:35 +00:00
|
|
|
|
|
|
|
# /usr/bin/ansible invocations
|
|
|
|
ansible host -m virt -a "name=alpha command=status"
|
|
|
|
ansible host -m virt -a "name=alpha command=get_xml"
|
|
|
|
ansible host -m virt -a "name=alpha command=create uri=lxc:///"
|
|
|
|
|
|
|
|
# a playbook example of defining and launching an LXC guest
|
|
|
|
tasks:
|
|
|
|
- name: define vm
|
2013-06-17 06:53:46 +00:00
|
|
|
virt: name=foo
|
|
|
|
command=define
|
|
|
|
xml="{{ lookup('template', 'container-template.xml.j2') }}"
|
|
|
|
uri=lxc:///
|
2013-04-27 15:20:35 +00:00
|
|
|
- name: start vm
|
|
|
|
virt: name=foo state=running uri=lxc:///
|
|
|
|
'''
|
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
VIRT_FAILED = 1
|
|
|
|
VIRT_SUCCESS = 0
|
|
|
|
VIRT_UNAVAILABLE=2
|
|
|
|
|
2012-09-07 20:05:42 +00:00
|
|
|
import sys
|
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
try:
|
|
|
|
import libvirt
|
|
|
|
except ImportError:
|
2012-07-31 01:16:29 +00:00
|
|
|
print "failed=True msg='libvirt python module unavailable'"
|
2012-04-04 18:30:24 +00:00
|
|
|
sys.exit(1)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-31 01:16:29 +00:00
|
|
|
ALL_COMMANDS = []
|
2012-08-07 00:07:02 +00:00
|
|
|
VM_COMMANDS = ['create','status', 'start', 'stop', 'pause', 'unpause',
|
2013-04-17 15:46:09 +00:00
|
|
|
'shutdown', 'undefine', 'destroy', 'get_xml', 'autostart', 'define']
|
2012-07-31 01:16:29 +00:00
|
|
|
HOST_COMMANDS = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
|
|
|
|
ALL_COMMANDS.extend(VM_COMMANDS)
|
|
|
|
ALL_COMMANDS.extend(HOST_COMMANDS)
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
VIRT_STATE_NAME_MAP = {
|
|
|
|
0 : "running",
|
|
|
|
1 : "running",
|
|
|
|
2 : "running",
|
|
|
|
3 : "paused",
|
|
|
|
4 : "shutdown",
|
|
|
|
5 : "shutdown",
|
|
|
|
6 : "crashed"
|
|
|
|
}
|
|
|
|
|
2013-05-11 21:23:32 +00:00
|
|
|
class VMNotFound(Exception):
|
|
|
|
pass
|
2013-04-17 15:46:09 +00:00
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
class LibvirtConnection(object):
|
|
|
|
|
2014-03-10 21:11:24 +00:00
|
|
|
def __init__(self, uri, module):
|
2012-04-04 18:30:24 +00:00
|
|
|
|
2014-03-10 21:11:24 +00:00
|
|
|
self.module = module
|
2012-04-04 18:30:24 +00:00
|
|
|
|
2014-03-10 21:11:24 +00:00
|
|
|
cmd = "uname -r"
|
|
|
|
rc, stdout, stderr = self.module.run_command(cmd)
|
|
|
|
|
2014-01-23 15:02:17 +00:00
|
|
|
if "xen" in stdout:
|
2012-04-04 18:30:24 +00:00
|
|
|
conn = libvirt.open(None)
|
|
|
|
else:
|
2013-04-08 20:14:37 +00:00
|
|
|
conn = libvirt.open(uri)
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
if not conn:
|
|
|
|
raise Exception("hypervisor connection failure")
|
|
|
|
|
|
|
|
self.conn = conn
|
|
|
|
|
|
|
|
def find_vm(self, vmid):
|
|
|
|
"""
|
|
|
|
Extra bonus feature: vmid = -1 returns a list of everything
|
|
|
|
"""
|
|
|
|
conn = self.conn
|
|
|
|
|
|
|
|
vms = []
|
|
|
|
|
|
|
|
# this block of code borrowed from virt-manager:
|
|
|
|
# get working domain's name
|
|
|
|
ids = conn.listDomainsID()
|
|
|
|
for id in ids:
|
|
|
|
vm = conn.lookupByID(id)
|
|
|
|
vms.append(vm)
|
|
|
|
# get defined domain
|
|
|
|
names = conn.listDefinedDomains()
|
|
|
|
for name in names:
|
|
|
|
vm = conn.lookupByName(name)
|
|
|
|
vms.append(vm)
|
|
|
|
|
|
|
|
if vmid == -1:
|
|
|
|
return vms
|
|
|
|
|
|
|
|
for vm in vms:
|
|
|
|
if vm.name() == vmid:
|
|
|
|
return vm
|
|
|
|
|
2013-04-17 15:46:09 +00:00
|
|
|
raise VMNotFound("virtual machine %s not found" % vmid)
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
def shutdown(self, vmid):
|
|
|
|
return self.find_vm(vmid).shutdown()
|
|
|
|
|
|
|
|
def pause(self, vmid):
|
|
|
|
return self.suspend(self.conn,vmid)
|
|
|
|
|
|
|
|
def unpause(self, vmid):
|
|
|
|
return self.resume(self.conn,vmid)
|
|
|
|
|
|
|
|
def suspend(self, vmid):
|
|
|
|
return self.find_vm(vmid).suspend()
|
|
|
|
|
|
|
|
def resume(self, vmid):
|
|
|
|
return self.find_vm(vmid).resume()
|
|
|
|
|
|
|
|
def create(self, vmid):
|
|
|
|
return self.find_vm(vmid).create()
|
|
|
|
|
|
|
|
def destroy(self, vmid):
|
|
|
|
return self.find_vm(vmid).destroy()
|
|
|
|
|
|
|
|
def undefine(self, vmid):
|
|
|
|
return self.find_vm(vmid).undefine()
|
|
|
|
|
|
|
|
def get_status2(self, vm):
|
|
|
|
state = vm.info()[0]
|
|
|
|
return VIRT_STATE_NAME_MAP.get(state,"unknown")
|
|
|
|
|
|
|
|
def get_status(self, vmid):
|
|
|
|
state = self.find_vm(vmid).info()[0]
|
|
|
|
return VIRT_STATE_NAME_MAP.get(state,"unknown")
|
|
|
|
|
|
|
|
def nodeinfo(self):
|
|
|
|
return self.conn.getInfo()
|
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
return self.conn.getType()
|
|
|
|
|
|
|
|
def get_maxVcpus(self, vmid):
|
|
|
|
vm = self.conn.lookupByName(vmid)
|
|
|
|
return vm.maxVcpus()
|
|
|
|
|
|
|
|
def get_maxMemory(self, vmid):
|
|
|
|
vm = self.conn.lookupByName(vmid)
|
|
|
|
return vm.maxMemory()
|
|
|
|
|
|
|
|
def getFreeMemory(self):
|
|
|
|
return self.conn.getFreeMemory()
|
|
|
|
|
|
|
|
def get_autostart(self, vmid):
|
|
|
|
vm = self.conn.lookupByName(vmid)
|
|
|
|
return vm.autostart()
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
def set_autostart(self, vmid, val):
|
|
|
|
vm = self.conn.lookupByName(vmid)
|
|
|
|
return vm.setAutostart(val)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-04-17 15:46:09 +00:00
|
|
|
def define_from_xml(self, xml):
|
|
|
|
return self.conn.defineXML(xml)
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Virt(object):
|
|
|
|
|
2014-03-10 21:11:24 +00:00
|
|
|
def __init__(self, uri, module):
|
|
|
|
self.module = module
|
2013-04-08 20:14:37 +00:00
|
|
|
self.uri = uri
|
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
def __get_conn(self):
|
2014-03-10 21:11:24 +00:00
|
|
|
self.conn = LibvirtConnection(self.uri, self.module)
|
2012-04-04 18:30:24 +00:00
|
|
|
return self.conn
|
|
|
|
|
|
|
|
def get_vm(self, vmid):
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.find_vm(vmid)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
def state(self):
|
|
|
|
vms = self.list_vms()
|
|
|
|
state = []
|
|
|
|
for vm in vms:
|
|
|
|
state_blurb = self.conn.get_status(vm)
|
|
|
|
state.append("%s %s" % (vm,state_blurb))
|
|
|
|
return state
|
|
|
|
|
|
|
|
def info(self):
|
|
|
|
vms = self.list_vms()
|
|
|
|
info = dict()
|
|
|
|
for vm in vms:
|
|
|
|
data = self.conn.find_vm(vm).info()
|
|
|
|
# libvirt returns maxMem, memory, and cpuTime as long()'s, which
|
|
|
|
# xmlrpclib tries to convert to regular int's during serialization.
|
|
|
|
# This throws exceptions, so convert them to strings here and
|
|
|
|
# assume the other end of the xmlrpc connection can figure things
|
|
|
|
# out or doesn't care.
|
|
|
|
info[vm] = {
|
|
|
|
"state" : VIRT_STATE_NAME_MAP.get(data[0],"unknown"),
|
|
|
|
"maxMem" : str(data[1]),
|
|
|
|
"memory" : str(data[2]),
|
|
|
|
"nrVirtCpu" : data[3],
|
|
|
|
"cpuTime" : str(data[4]),
|
|
|
|
}
|
2012-08-11 16:35:58 +00:00
|
|
|
info[vm]["autostart"] = self.conn.get_autostart(vm)
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
def nodeinfo(self):
|
|
|
|
self.__get_conn()
|
|
|
|
info = dict()
|
|
|
|
data = self.conn.nodeinfo()
|
|
|
|
info = {
|
|
|
|
"cpumodel" : str(data[0]),
|
|
|
|
"phymemory" : str(data[1]),
|
|
|
|
"cpus" : str(data[2]),
|
|
|
|
"cpumhz" : str(data[3]),
|
|
|
|
"numanodes" : str(data[4]),
|
|
|
|
"sockets" : str(data[5]),
|
|
|
|
"cpucores" : str(data[6]),
|
|
|
|
"cputhreads" : str(data[7])
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
|
2013-05-25 20:20:25 +00:00
|
|
|
def list_vms(self, state=None):
|
2012-04-04 18:30:24 +00:00
|
|
|
self.conn = self.__get_conn()
|
|
|
|
vms = self.conn.find_vm(-1)
|
|
|
|
results = []
|
|
|
|
for x in vms:
|
|
|
|
try:
|
2013-05-26 21:06:23 +00:00
|
|
|
if state:
|
|
|
|
vmstate = self.conn.get_status2(x)
|
|
|
|
if vmstate == state:
|
|
|
|
results.append(x.name())
|
|
|
|
else:
|
2013-05-25 20:20:25 +00:00
|
|
|
results.append(x.name())
|
2012-04-04 18:30:24 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return results
|
|
|
|
|
|
|
|
def virttype(self):
|
|
|
|
return self.__get_conn().get_type()
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
def autostart(self, vmid):
|
|
|
|
self.conn = self.__get_conn()
|
|
|
|
return self.conn.set_autostart(vmid, True)
|
|
|
|
|
|
|
|
def freemem(self):
|
|
|
|
self.conn = self.__get_conn()
|
|
|
|
return self.conn.getFreeMemory()
|
|
|
|
|
|
|
|
def shutdown(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Make the machine with the given vmid stop running. Whatever that takes. """
|
2012-04-04 18:30:24 +00:00
|
|
|
self.__get_conn()
|
|
|
|
self.conn.shutdown(vmid)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def pause(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Pause the machine with the given vmid. """
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.suspend(vmid)
|
|
|
|
|
|
|
|
def unpause(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Unpause the machine with the given vmid. """
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.resume(vmid)
|
|
|
|
|
|
|
|
def create(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Start the machine via the given vmid """
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.create(vmid)
|
|
|
|
|
|
|
|
def start(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Start the machine via the given id/name """
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.create(vmid)
|
|
|
|
|
|
|
|
def destroy(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Pull the virtual power from the virtual domain, giving it virtually no time to virtually shut down. """
|
2012-04-04 18:30:24 +00:00
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.destroy(vmid)
|
|
|
|
|
|
|
|
def undefine(self, vmid):
|
2012-07-31 01:16:29 +00:00
|
|
|
""" Stop a domain, and then wipe it from the face of the earth. (delete disk/config file) """
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.undefine(vmid)
|
|
|
|
|
|
|
|
def status(self, vmid):
|
|
|
|
"""
|
|
|
|
Return a state suitable for server consumption. Aka, codes.py values, not XM output.
|
|
|
|
"""
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.get_status(vmid)
|
|
|
|
|
|
|
|
def get_xml(self, vmid):
|
|
|
|
"""
|
2012-04-13 10:19:36 +00:00
|
|
|
Receive a Vm id as input
|
2012-04-04 18:30:24 +00:00
|
|
|
Return an xml describing vm config returned by a libvirt call
|
|
|
|
"""
|
2012-07-31 01:16:29 +00:00
|
|
|
|
2012-04-04 18:30:24 +00:00
|
|
|
conn = libvirt.openReadOnly(None)
|
2013-02-09 23:49:54 +00:00
|
|
|
if not conn:
|
2012-04-04 18:30:24 +00:00
|
|
|
return (-1,'Failed to open connection to the hypervisor')
|
|
|
|
try:
|
|
|
|
domV = conn.lookupByName(vmid)
|
|
|
|
except:
|
|
|
|
return (-1,'Failed to find the main domain')
|
|
|
|
return domV.XMLDesc(0)
|
|
|
|
|
|
|
|
def get_maxVcpus(self, vmid):
|
|
|
|
"""
|
|
|
|
Gets the max number of VCPUs on a guest
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.get_maxVcpus(vmid)
|
|
|
|
|
|
|
|
def get_max_memory(self, vmid):
|
|
|
|
"""
|
|
|
|
Gets the max memory on a guest
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.get_MaxMemory(vmid)
|
|
|
|
|
2013-04-17 15:46:09 +00:00
|
|
|
def define(self, xml):
|
|
|
|
"""
|
|
|
|
Define a guest with the given xml
|
|
|
|
"""
|
|
|
|
self.__get_conn()
|
|
|
|
return self.conn.define_from_xml(xml)
|
|
|
|
|
2012-07-31 01:16:29 +00:00
|
|
|
def core(module):
|
2012-04-04 18:30:24 +00:00
|
|
|
|
2012-07-31 01:16:29 +00:00
|
|
|
state = module.params.get('state', None)
|
|
|
|
guest = module.params.get('name', None)
|
|
|
|
command = module.params.get('command', None)
|
2013-04-08 20:14:37 +00:00
|
|
|
uri = module.params.get('uri', None)
|
2013-04-17 15:46:09 +00:00
|
|
|
xml = module.params.get('xml', None)
|
2012-04-04 21:17:29 +00:00
|
|
|
|
2014-03-10 21:11:24 +00:00
|
|
|
v = Virt(uri, module)
|
2012-04-04 21:17:29 +00:00
|
|
|
res = {}
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-05-25 20:20:25 +00:00
|
|
|
if state and command=='list_vms':
|
|
|
|
res = v.list_vms(state=state)
|
|
|
|
if type(res) != dict:
|
|
|
|
res = { command: res }
|
|
|
|
return VIRT_SUCCESS, res
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
if state:
|
|
|
|
if not guest:
|
2012-07-31 01:16:29 +00:00
|
|
|
module.fail_json(msg = "state change requires a guest specified")
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
res['changed'] = False
|
|
|
|
if state == 'running':
|
2013-11-27 16:58:34 +00:00
|
|
|
if v.status(guest) is 'paused':
|
|
|
|
res['changed'] = True
|
|
|
|
res['msg'] = v.unpause(guest)
|
|
|
|
elif v.status(guest) is not 'running':
|
2012-08-07 00:07:02 +00:00
|
|
|
res['changed'] = True
|
2012-04-04 21:17:29 +00:00
|
|
|
res['msg'] = v.start(guest)
|
|
|
|
elif state == 'shutdown':
|
|
|
|
if v.status(guest) is not 'shutdown':
|
|
|
|
res['changed'] = True
|
|
|
|
res['msg'] = v.shutdown(guest)
|
2013-11-27 16:58:34 +00:00
|
|
|
elif state == 'destroyed':
|
|
|
|
if v.status(guest) is not 'shutdown':
|
|
|
|
res['changed'] = True
|
|
|
|
res['msg'] = v.destroy(guest)
|
|
|
|
elif state == 'paused':
|
|
|
|
if v.status(guest) is 'running':
|
|
|
|
res['changed'] = True
|
|
|
|
res['msg'] = v.pause(guest)
|
2012-07-31 01:16:29 +00:00
|
|
|
else:
|
2012-08-07 00:07:02 +00:00
|
|
|
module.fail_json(msg="unexpected state")
|
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
return VIRT_SUCCESS, res
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
if command:
|
2012-07-31 01:16:29 +00:00
|
|
|
if command in VM_COMMANDS:
|
2012-04-04 21:17:29 +00:00
|
|
|
if not guest:
|
2012-07-31 01:16:29 +00:00
|
|
|
module.fail_json(msg = "%s requires 1 argument: guest" % command)
|
2013-04-17 15:46:09 +00:00
|
|
|
if command == 'define':
|
|
|
|
if not xml:
|
|
|
|
module.fail_json(msg = "define requires xml argument")
|
|
|
|
try:
|
|
|
|
v.get_vm(guest)
|
|
|
|
except VMNotFound:
|
|
|
|
v.define(xml)
|
|
|
|
res = {'changed': True, 'created': guest}
|
|
|
|
return VIRT_SUCCESS, res
|
2012-04-04 21:17:29 +00:00
|
|
|
res = getattr(v, command)(guest)
|
|
|
|
if type(res) != dict:
|
|
|
|
res = { command: res }
|
2012-08-02 21:46:21 +00:00
|
|
|
return VIRT_SUCCESS, res
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
elif hasattr(v, command):
|
|
|
|
res = getattr(v, command)()
|
|
|
|
if type(res) != dict:
|
|
|
|
res = { command: res }
|
2012-08-02 21:46:21 +00:00
|
|
|
return VIRT_SUCCESS, res
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-04-04 21:17:29 +00:00
|
|
|
else:
|
2012-07-31 01:16:29 +00:00
|
|
|
module.fail_json(msg="Command %s not recognized" % basecmd)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
|
|
|
module.fail_json(msg="expected state or command parameter to be specified")
|
|
|
|
|
2012-07-31 01:16:29 +00:00
|
|
|
def main():
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-31 01:16:29 +00:00
|
|
|
module = AnsibleModule(argument_spec=dict(
|
|
|
|
name = dict(aliases=['guest']),
|
2013-11-27 16:58:34 +00:00
|
|
|
state = dict(choices=['running', 'shutdown', 'destroyed', 'paused']),
|
2012-07-31 01:16:29 +00:00
|
|
|
command = dict(choices=ALL_COMMANDS),
|
2013-04-08 20:14:37 +00:00
|
|
|
uri = dict(default='qemu:///system'),
|
2013-04-17 15:46:09 +00:00
|
|
|
xml = dict(),
|
2012-07-31 01:16:29 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
rc = VIRT_SUCCESS
|
2012-04-04 18:30:24 +00:00
|
|
|
try:
|
2012-07-31 01:16:29 +00:00
|
|
|
rc, result = core(module)
|
2012-04-04 18:30:24 +00:00
|
|
|
except Exception, e:
|
2012-07-31 01:16:29 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2012-04-04 18:30:24 +00:00
|
|
|
|
|
|
|
if rc != 0: # something went wrong emit the msg
|
2012-07-31 01:16:29 +00:00
|
|
|
module.fail_json(rc=rc, msg=result)
|
2012-04-04 18:30:24 +00:00
|
|
|
else:
|
2012-07-31 01:16:29 +00:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
2013-12-02 20:13:49 +00:00
|
|
|
# import module snippets
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2012-08-07 00:07:02 +00:00
|
|
|
main()
|