virt.py: autostart VM attribute (#22208)

* virt.py: autostart VM attribute
autostart is now an idempotent VM attribute instead of a non idempotent forced autostart attribute set to True

* Make shippable happy

* Missing version added

* Fix some points

* Autostart default is now None

* Ident fix
This commit is contained in:
Loïc Blot 2017-03-07 12:07:19 +01:00 committed by John R Barker
parent 863c1ff38b
commit 792efbe3b6

View file

@ -47,8 +47,14 @@ options:
- in addition to state management, various non-idempotent commands are available. See examples - in addition to state management, various non-idempotent commands are available. See examples
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",
"freemem", "list_vms", "info", "nodeinfo", "virttype", "define"] "freemem", "list_vms", "info", "nodeinfo", "virttype", "define"]
autostart:
description:
- start VM at host startup
choices: [True, False]
version_added: "2.3"
default: null
uri: uri:
description: description:
- libvirt connection uri - libvirt connection uri
@ -127,7 +133,7 @@ else:
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', 'define'] 'shutdown', 'undefine', 'destroy', 'get_xml', '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)
@ -339,9 +345,14 @@ class Virt(object):
def virttype(self): def virttype(self):
return self.__get_conn().get_type() return self.__get_conn().get_type()
def autostart(self, vmid): def autostart(self, vmid, as_flag):
self.conn = self.__get_conn() self.conn = self.__get_conn()
return self.conn.set_autostart(vmid, True) # Change autostart flag only if needed
if self.conn.get_autostart(vmid) != as_flag:
self.conn.set_autostart(vmid, as_flag)
return True
return False
def freemem(self): def freemem(self):
self.conn = self.__get_conn() self.conn = self.__get_conn()
@ -431,6 +442,7 @@ class Virt(object):
def core(module): def core(module):
state = module.params.get('state', None) state = module.params.get('state', None)
autostart = module.params.get('autostart', 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)
@ -449,7 +461,6 @@ def core(module):
if not guest: if not guest:
module.fail_json(msg = "state change requires a guest specified") module.fail_json(msg = "state change requires a guest specified")
res['changed'] = False
if state == 'running': if state == 'running':
if v.status(guest) is 'paused': if v.status(guest) is 'paused':
res['changed'] = True res['changed'] = True
@ -474,6 +485,9 @@ def core(module):
return VIRT_SUCCESS, res return VIRT_SUCCESS, res
if autostart is not None and v.autostart(guest, autostart):
res['changed'] = True
if command: if command:
if command in VM_COMMANDS: if command in VM_COMMANDS:
if not guest: if not guest:
@ -508,6 +522,7 @@ def main():
module = AnsibleModule(argument_spec=dict( module = AnsibleModule(argument_spec=dict(
name = dict(aliases=['guest']), name = dict(aliases=['guest']),
state = dict(choices=['running', 'shutdown', 'destroyed', 'paused']), state = dict(choices=['running', 'shutdown', 'destroyed', 'paused']),
autostart = dict(type='bool'),
command = dict(choices=ALL_COMMANDS), command = dict(choices=ALL_COMMANDS),
uri = dict(default='qemu:///system'), uri = dict(default='qemu:///system'),
xml = dict(), xml = dict(),