Merge pull request #2702 from DazWorrall/devel
Add define command to virt module
This commit is contained in:
commit
c3cf446a8a
1 changed files with 30 additions and 3 deletions
33
virt
33
virt
|
@ -44,12 +44,17 @@ options:
|
||||||
required: false
|
required: false
|
||||||
choices: ["create","status", "start", "stop", "pause", "unpause",
|
choices: ["create","status", "start", "stop", "pause", "unpause",
|
||||||
"shutdown", "undefine", "destroy", "get_xml", "autostart",
|
"shutdown", "undefine", "destroy", "get_xml", "autostart",
|
||||||
"freemem", "list_vms", "info", "nodeinfo", "virttype"]
|
"freemem", "list_vms", "info", "nodeinfo", "virttype", "define"]
|
||||||
uri:
|
uri:
|
||||||
description:
|
description:
|
||||||
- libvirt connection uri
|
- libvirt connection uri
|
||||||
required: false
|
required: false
|
||||||
defaults: qemu:///system
|
defaults: qemu:///system
|
||||||
|
xml:
|
||||||
|
description:
|
||||||
|
- XML document used with the define command
|
||||||
|
required: false
|
||||||
|
default: null
|
||||||
examples:
|
examples:
|
||||||
- code: "virt: name=alpha state=running"
|
- code: "virt: name=alpha state=running"
|
||||||
description: "Example from Ansible Playbooks"
|
description: "Example from Ansible Playbooks"
|
||||||
|
@ -76,7 +81,7 @@ except ImportError:
|
||||||
|
|
||||||
ALL_COMMANDS = []
|
ALL_COMMANDS = []
|
||||||
VM_COMMANDS = ['create','status', 'start', 'stop', 'pause', 'unpause',
|
VM_COMMANDS = ['create','status', 'start', 'stop', 'pause', 'unpause',
|
||||||
'shutdown', 'undefine', 'destroy', 'get_xml', 'autostart']
|
'shutdown', 'undefine', 'destroy', 'get_xml', 'autostart', 'define']
|
||||||
HOST_COMMANDS = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
|
HOST_COMMANDS = ['freemem', 'list_vms', 'info', 'nodeinfo', 'virttype']
|
||||||
ALL_COMMANDS.extend(VM_COMMANDS)
|
ALL_COMMANDS.extend(VM_COMMANDS)
|
||||||
ALL_COMMANDS.extend(HOST_COMMANDS)
|
ALL_COMMANDS.extend(HOST_COMMANDS)
|
||||||
|
@ -91,6 +96,8 @@ VIRT_STATE_NAME_MAP = {
|
||||||
6 : "crashed"
|
6 : "crashed"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class VMNotFound(Exception): pass
|
||||||
|
|
||||||
class LibvirtConnection(object):
|
class LibvirtConnection(object):
|
||||||
|
|
||||||
def __init__(self, uri):
|
def __init__(self, uri):
|
||||||
|
@ -136,7 +143,7 @@ class LibvirtConnection(object):
|
||||||
if vm.name() == vmid:
|
if vm.name() == vmid:
|
||||||
return vm
|
return vm
|
||||||
|
|
||||||
raise Exception("virtual machine %s not found" % vmid)
|
raise VMNotFound("virtual machine %s not found" % vmid)
|
||||||
|
|
||||||
def shutdown(self, vmid):
|
def shutdown(self, vmid):
|
||||||
return self.find_vm(vmid).shutdown()
|
return self.find_vm(vmid).shutdown()
|
||||||
|
@ -195,6 +202,8 @@ class LibvirtConnection(object):
|
||||||
vm = self.conn.lookupByName(vmid)
|
vm = self.conn.lookupByName(vmid)
|
||||||
return vm.setAutostart(val)
|
return vm.setAutostart(val)
|
||||||
|
|
||||||
|
def define_from_xml(self, xml):
|
||||||
|
return self.conn.defineXML(xml)
|
||||||
|
|
||||||
|
|
||||||
class Virt(object):
|
class Virt(object):
|
||||||
|
@ -357,12 +366,20 @@ class Virt(object):
|
||||||
self.__get_conn()
|
self.__get_conn()
|
||||||
return self.conn.get_MaxMemory(vmid)
|
return self.conn.get_MaxMemory(vmid)
|
||||||
|
|
||||||
|
def define(self, xml):
|
||||||
|
"""
|
||||||
|
Define a guest with the given xml
|
||||||
|
"""
|
||||||
|
self.__get_conn()
|
||||||
|
return self.conn.define_from_xml(xml)
|
||||||
|
|
||||||
def core(module):
|
def core(module):
|
||||||
|
|
||||||
state = module.params.get('state', None)
|
state = module.params.get('state', None)
|
||||||
guest = module.params.get('name', None)
|
guest = module.params.get('name', None)
|
||||||
command = module.params.get('command', None)
|
command = module.params.get('command', None)
|
||||||
uri = module.params.get('uri', None)
|
uri = module.params.get('uri', None)
|
||||||
|
xml = module.params.get('xml', None)
|
||||||
|
|
||||||
v = Virt(uri)
|
v = Virt(uri)
|
||||||
res = {}
|
res = {}
|
||||||
|
@ -390,6 +407,15 @@ def core(module):
|
||||||
if command in VM_COMMANDS:
|
if command in VM_COMMANDS:
|
||||||
if not guest:
|
if not guest:
|
||||||
module.fail_json(msg = "%s requires 1 argument: guest" % command)
|
module.fail_json(msg = "%s requires 1 argument: guest" % command)
|
||||||
|
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
|
||||||
res = getattr(v, command)(guest)
|
res = getattr(v, command)(guest)
|
||||||
if type(res) != dict:
|
if type(res) != dict:
|
||||||
res = { command: res }
|
res = { command: res }
|
||||||
|
@ -413,6 +439,7 @@ def main():
|
||||||
state = dict(choices=['running', 'shutdown']),
|
state = dict(choices=['running', 'shutdown']),
|
||||||
command = dict(choices=ALL_COMMANDS),
|
command = dict(choices=ALL_COMMANDS),
|
||||||
uri = dict(default='qemu:///system'),
|
uri = dict(default='qemu:///system'),
|
||||||
|
xml = dict(),
|
||||||
))
|
))
|
||||||
|
|
||||||
rc = VIRT_SUCCESS
|
rc = VIRT_SUCCESS
|
||||||
|
|
Loading…
Reference in a new issue