| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Ansible Project |
| 5 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vmware_host_powerstate |
| 16 |
short_description: Manages power states of host systems in vCenter |
| 17 |
description: |
| 18 |
- This module can be used to manage power states of host systems in given vCenter infrastructure. |
| 19 |
- User can set power state to 'power-down-to-standby', 'power-up-from-standby', 'shutdown-host' and 'reboot-host'. |
| 20 |
- State 'reboot-host', 'shutdown-host' and 'power-down-to-standby' are not supported by all the host systems. |
| 21 |
author: |
| 22 |
- Abhijeet Kasurde (@Akasurde) |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- Set the state of the host system. |
| 27 |
choices: [ 'power-down-to-standby', 'power-up-from-standby', 'shutdown-host', 'reboot-host' ] |
| 28 |
default: 'shutdown-host' |
| 29 |
type: str |
| 30 |
esxi_hostname: |
| 31 |
description: |
| 32 |
- Name of the host system to work with. |
| 33 |
- This is required parameter if O(cluster_name) is not specified. |
| 34 |
type: str |
| 35 |
cluster_name: |
| 36 |
description: |
| 37 |
- Name of the cluster from which all host systems will be used. |
| 38 |
- This is required parameter if O(esxi_hostname) is not specified. |
| 39 |
type: str |
| 40 |
force: |
| 41 |
description: |
| 42 |
- 'This parameter specify if the host should be proceeding with user defined powerstate |
| 43 |
regardless of whether it is in maintenance mode.' |
| 44 |
- 'If O(state=reboot-host) and O(force=true), then host system is rebooted regardless of whether it is in maintenance mode.' |
| 45 |
- 'If O(state=shutdown-host) and O(force=true), then host system is shutdown regardless of whether it is in maintenance mode.' |
| 46 |
- 'If O(state=power-down-to-standby) and O(force=true), then all powered off VMs will evacuated.' |
| 47 |
- 'Not applicable if O(state=power-up-from-standby).' |
| 48 |
type: bool |
| 49 |
default: false |
| 50 |
timeout: |
| 51 |
description: |
| 52 |
- 'This parameter defines timeout for O(state=power-down-to-standby) or O(state=power-up-from-standby).' |
| 53 |
- 'Ignored if O(state=reboot-host) or O(state=shutdown-host).' |
| 54 |
- 'This parameter is defined in seconds.' |
| 55 |
default: 600 |
| 56 |
type: int |
| 57 |
extends_documentation_fragment: |
| 58 |
- vmware.vmware.base_options |
| 59 |
|
| 60 |
''' |
| 61 |
|
| 62 |
EXAMPLES = r''' |
| 63 |
- name: Set the state of a host system to reboot |
| 64 |
community.vmware.vmware_host_powerstate: |
| 65 |
hostname: '{{ vcenter_hostname }}' |
| 66 |
username: '{{ vcenter_username }}' |
| 67 |
password: '{{ vcenter_password }}' |
| 68 |
esxi_hostname: '{{ esxi_hostname }}' |
| 69 |
state: reboot-host |
| 70 |
delegate_to: localhost |
| 71 |
register: reboot_host |
| 72 |
|
| 73 |
- name: Set the state of a host system to power down to standby |
| 74 |
community.vmware.vmware_host_powerstate: |
| 75 |
hostname: '{{ vcenter_hostname }}' |
| 76 |
username: '{{ vcenter_username }}' |
| 77 |
password: '{{ vcenter_password }}' |
| 78 |
esxi_hostname: '{{ esxi_hostname }}' |
| 79 |
state: power-down-to-standby |
| 80 |
delegate_to: localhost |
| 81 |
register: power_down |
| 82 |
|
| 83 |
- name: Set the state of all host systems from cluster to reboot |
| 84 |
community.vmware.vmware_host_powerstate: |
| 85 |
hostname: '{{ vcenter_hostname }}' |
| 86 |
username: '{{ vcenter_username }}' |
| 87 |
password: '{{ vcenter_password }}' |
| 88 |
cluster_name: '{{ cluster_name }}' |
| 89 |
state: reboot-host |
| 90 |
delegate_to: localhost |
| 91 |
register: reboot_host |
| 92 |
''' |
| 93 |
|
| 94 |
RETURN = r''' |
| 95 |
result: |
| 96 |
description: metadata about host system's state |
| 97 |
returned: always |
| 98 |
type: dict |
| 99 |
sample: { |
| 100 |
"esxi01": { |
| 101 |
"msg": "power down 'esxi01' to standby", |
| 102 |
"error": "", |
| 103 |
}, |
| 104 |
} |
| 105 |
''' |
| 106 |
|
| 107 |
from ansible.module_utils.basic import AnsibleModule |
| 108 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, wait_for_task, TaskError |
| 109 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 110 |
from ansible.module_utils.common.text.converters import to_native |
| 111 |
|
| 112 |
|
| 113 |
class VmwareHostPowerManager(PyVmomi): |
| 114 |
def __init__(self, module): |
| 115 |
super(VmwareHostPowerManager, self).__init__(module) |
| 116 |
cluster_name = self.params.get('cluster_name') |
| 117 |
esxi_host_name = self.params.get('esxi_hostname') |
| 118 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 119 |
if not self.hosts: |
| 120 |
self.module.fail_json(msg="Failed to find host system with given configuration.") |
| 121 |
|
| 122 |
def ensure(self): |
| 123 |
""" |
| 124 |
Function to manage internal state of host system |
| 125 |
|
| 126 |
""" |
| 127 |
results = dict(changed=False, result=dict()) |
| 128 |
state = self.params.get('state') |
| 129 |
force = self.params.get('force') |
| 130 |
timeout = self.params.get('timeout') |
| 131 |
host_change_list = [] |
| 132 |
for host in self.hosts: |
| 133 |
changed = False |
| 134 |
task = None |
| 135 |
if not host.runtime.inMaintenanceMode and not force: |
| 136 |
self.module.fail_json(msg="Current host system '%s' is not in maintenance mode," |
| 137 |
" please specify 'force' as True to proceed." % host.name) |
| 138 |
if host.runtime.connectionState == 'notResponding': |
| 139 |
self.module.fail_json(msg="Current host system '%s' can not be set in '%s'" |
| 140 |
" mode as the host system is not responding." % (host.name, state)) |
| 141 |
|
| 142 |
results['result'][host.name] = dict(msg='', error='') |
| 143 |
if state == 'reboot-host' and not host.capability.rebootSupported: |
| 144 |
self.module.fail_json(msg="Current host '%s' can not be rebooted as the host system" |
| 145 |
" does not have capability to reboot." % host.name) |
| 146 |
elif state == 'shutdown-host' and not host.capability.shutdownSupported: |
| 147 |
self.module.fail_json(msg="Current host '%s' can not be shut down as the host system" |
| 148 |
" does not have capability to shut down." % host.name) |
| 149 |
elif state in ['power-down-to-standby', 'power-up-from-standby'] and not host.capability.standbySupported: |
| 150 |
self.module.fail_json(msg="Current host '%s' can not be '%s' as the host system" |
| 151 |
" does not have capability to standby supported." % (host.name, state)) |
| 152 |
|
| 153 |
if state == 'reboot-host': |
| 154 |
if not self.module.check_mode: |
| 155 |
task = host.RebootHost_Task(force) |
| 156 |
verb = "reboot '%s'" % host.name |
| 157 |
elif state == 'shutdown-host': |
| 158 |
if not self.module.check_mode: |
| 159 |
task = host.ShutdownHost_Task(force) |
| 160 |
verb = "shutdown '%s'" % host.name |
| 161 |
elif state == 'power-down-to-standby': |
| 162 |
if not self.module.check_mode: |
| 163 |
task = host.PowerDownHostToStandBy_Task(timeout, force) |
| 164 |
verb = "power down '%s' to standby" % host.name |
| 165 |
elif state == 'power-up-from-standby': |
| 166 |
if not self.module.check_mode: |
| 167 |
task = host.PowerUpHostFromStandBy_Task(timeout) |
| 168 |
verb = "power up '%s' from standby" % host.name |
| 169 |
|
| 170 |
if not self.module.check_mode: |
| 171 |
try: |
| 172 |
success, result = wait_for_task(task) |
| 173 |
if success: |
| 174 |
changed = True |
| 175 |
results['result'][host.name]['msg'] = verb |
| 176 |
else: |
| 177 |
results['result'][host.name]['error'] = result |
| 178 |
except TaskError as task_error: |
| 179 |
self.module.fail_json(msg="Failed to %s as host system due to : %s" % (verb, |
| 180 |
str(task_error))) |
| 181 |
except Exception as generic_exc: |
| 182 |
self.module.fail_json(msg="Failed to %s due to generic exception : %s" % (host.name, |
| 183 |
to_native(generic_exc))) |
| 184 |
else: |
| 185 |
# Check mode |
| 186 |
changed = True |
| 187 |
results['result'][host.name]['msg'] = verb |
| 188 |
|
| 189 |
host_change_list.append(changed) |
| 190 |
|
| 191 |
if any(host_change_list): |
| 192 |
results['changed'] = True |
| 193 |
self.module.exit_json(**results) |
| 194 |
|
| 195 |
|
| 196 |
def main(): |
| 197 |
argument_spec = base_argument_spec() |
| 198 |
argument_spec.update( |
| 199 |
state=dict(type='str', default='shutdown-host', |
| 200 |
choices=['power-down-to-standby', 'power-up-from-standby', 'shutdown-host', 'reboot-host']), |
| 201 |
esxi_hostname=dict(type='str', required=False), |
| 202 |
cluster_name=dict(type='str', required=False), |
| 203 |
force=dict(type='bool', default=False), |
| 204 |
timeout=dict(type='int', default=600), |
| 205 |
|
| 206 |
) |
| 207 |
|
| 208 |
module = AnsibleModule(argument_spec=argument_spec, |
| 209 |
supports_check_mode=True, |
| 210 |
required_one_of=[ |
| 211 |
['cluster_name', 'esxi_hostname'], |
| 212 |
] |
| 213 |
) |
| 214 |
|
| 215 |
host_power_manager = VmwareHostPowerManager(module) |
| 216 |
host_power_manager.ensure() |
| 217 |
|
| 218 |
|
| 219 |
if __name__ == '__main__': |
| 220 |
main() |