Skip to content

Commit

Permalink
Add guest shutdown/reboot (ansible#20564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdul-Anshad-A authored and jctanner committed Feb 7, 2017
1 parent b773b99 commit b309427
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/ansible/modules/cloud/vmware/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
- 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
required: True
choices: ['present', 'absent', 'poweredon', 'poweredoff', 'restarted', 'suspended']
choices: ['present', 'absent', 'poweredon', 'poweredoff', 'restarted', 'suspended', 'shutdownguest', 'rebootguest']
name:
description:
- Name of the VM to work with
Expand Down Expand Up @@ -507,13 +507,24 @@ def set_powerstate(self, vm, state, force):
else:
result = {'changed': False, 'failed': True,
'msg': "Cannot restart VM in the current state %s" % current_state}

elif expected_state == 'suspended':
if current_state in ('poweredon', 'poweringon'):
task = vm.Suspend()
else:
result = {'changed': False, 'failed': True,
'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:
e = get_exception()
result = {'changed': False, 'failed': True, 'msg': e}
Expand Down Expand Up @@ -1233,6 +1244,8 @@ def main():
'absent',
'restarted',
'suspended',
'shutdownguest',
'rebootguest'
],
default='present'),
validate_certs=dict(required=False, type='bool', default=True),
Expand Down Expand Up @@ -1289,7 +1302,7 @@ def main():
result = pyv.remove_vm(vm)
elif module.params['state'] == 'present':
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
tmp_result = pyv.set_powerstate(vm, module.params['state'], module.params['force'])
if tmp_result['changed']:
Expand Down

0 comments on commit b309427

Please sign in to comment.