Add guest shutdown/reboot (#20564)

This commit is contained in:
Abdul Anshad A 2017-02-07 21:03:13 +05:30 committed by jctanner
parent b773b99136
commit b30942787b

View file

@ -45,7 +45,7 @@ options:
- What state should the virtual machine be in? - What state should the virtual machine be in?
- If C(state) is set to C(present) and VM exists, ensure the VM configuration conforms to task arguments - If C(state) is set to C(present) and VM exists, ensure the VM configuration conforms to task arguments
required: True required: True
choices: ['present', 'absent', 'poweredon', 'poweredoff', 'restarted', 'suspended'] choices: ['present', 'absent', 'poweredon', 'poweredoff', 'restarted', 'suspended', 'shutdownguest', 'rebootguest']
name: name:
description: description:
- Name of the VM to work with - Name of the VM to work with
@ -507,6 +507,7 @@ class PyVmomiHelper(object):
else: else:
result = {'changed': False, 'failed': True, result = {'changed': False, 'failed': True,
'msg': "Cannot restart VM in the current state %s" % current_state} 'msg': "Cannot restart VM in the current state %s" % current_state}
elif expected_state == 'suspended': elif expected_state == 'suspended':
if current_state in ('poweredon', 'poweringon'): if current_state in ('poweredon', 'poweringon'):
task = vm.Suspend() task = vm.Suspend()
@ -514,6 +515,16 @@ class PyVmomiHelper(object):
result = {'changed': False, 'failed': True, result = {'changed': False, 'failed': True,
'msg': 'Cannot suspend VM in the current state %s' % current_state} 'msg': 'Cannot suspend VM in the current state %s' % current_state}
elif expected_state in ['shutdownguest', 'rebootguest']:
if current_state == 'poweredon' and vm.guest.toolsRunningStatus == 'guestToolsRunning':
if expected_state == 'shutdownguest':
task = vm.ShutdownGuest()
else:
task = vm.RebootGuest()
else:
result = {'changed': False, 'failed': True,
'msg': "VM %s must be in poweredon state & tools should be installed for guest shutdown/reboot" % vm.name}
except Exception: except Exception:
e = get_exception() e = get_exception()
result = {'changed': False, 'failed': True, 'msg': e} result = {'changed': False, 'failed': True, 'msg': e}
@ -1233,6 +1244,8 @@ def main():
'absent', 'absent',
'restarted', 'restarted',
'suspended', 'suspended',
'shutdownguest',
'rebootguest'
], ],
default='present'), default='present'),
validate_certs=dict(required=False, type='bool', default=True), validate_certs=dict(required=False, type='bool', default=True),
@ -1289,7 +1302,7 @@ def main():
result = pyv.remove_vm(vm) result = pyv.remove_vm(vm)
elif module.params['state'] == 'present': elif module.params['state'] == 'present':
result = pyv.reconfigure_vm() result = pyv.reconfigure_vm()
elif module.params['state'] in ['poweredon', 'poweredoff', 'restarted', 'suspended']: elif module.params['state'] in ['poweredon', 'poweredoff', 'restarted', 'suspended', 'shutdownguest', 'rebootguest']:
# set powerstate # set powerstate
tmp_result = pyv.set_powerstate(vm, module.params['state'], module.params['force']) tmp_result = pyv.set_powerstate(vm, module.params['state'], module.params['force'])
if tmp_result['changed']: if tmp_result['changed']: