| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, sky-joker |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
module: vmware_host_auto_start |
| 14 |
short_description: Manage the auto power ON or OFF for vm on ESXi host |
| 15 |
author: |
| 16 |
- sky-joker (@sky-joker) |
| 17 |
description: |
| 18 |
- In this module, can set up automatic startup and shutdown of virtual machines according to host startup or shutdown. |
| 19 |
options: |
| 20 |
esxi_hostname: |
| 21 |
description: |
| 22 |
- ESXi hostname where the VM to set auto power on or off exists. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
name: |
| 26 |
description: |
| 27 |
- VM name to set auto power on or off. |
| 28 |
- This is not necessary if change only system default VM settings for autoStart config. |
| 29 |
type: str |
| 30 |
uuid: |
| 31 |
description: |
| 32 |
- VM uuid to set auto power on or off, this is VMware's unique identifier. |
| 33 |
- This is required if O(name) is not supplied. |
| 34 |
- This is not necessary if change only system default VM settings for autoStart config. |
| 35 |
type: str |
| 36 |
use_instance_uuid: |
| 37 |
description: |
| 38 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 39 |
type: bool |
| 40 |
default: false |
| 41 |
moid: |
| 42 |
description: |
| 43 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 44 |
- This is required if O(name) or O(uuid) is not supplied. |
| 45 |
type: str |
| 46 |
system_defaults: |
| 47 |
description: |
| 48 |
- System defaults for auto-start or auto-stop config for virtual machine. |
| 49 |
type: dict |
| 50 |
suboptions: |
| 51 |
enabled: |
| 52 |
description: |
| 53 |
- Enable automatically start or stop of virtual machines. |
| 54 |
type: bool |
| 55 |
default: false |
| 56 |
start_delay: |
| 57 |
description: |
| 58 |
- Default auto start delay in seconds. |
| 59 |
type: int |
| 60 |
default: 120 |
| 61 |
stop_action: |
| 62 |
description: |
| 63 |
- Default stop action executed on the virtual machine when the system stops. |
| 64 |
type: str |
| 65 |
choices: ['none', 'guestShutdown', 'powerOff', 'suspend'] |
| 66 |
default: powerOff |
| 67 |
stop_delay: |
| 68 |
description: |
| 69 |
- Default auto stop delay in seconds. |
| 70 |
type: int |
| 71 |
default: 120 |
| 72 |
wait_for_heartbeat: |
| 73 |
description: |
| 74 |
- Continue power on processing when VMware Tools started. |
| 75 |
- If this parameter is enabled to powers on the next virtual machine without waiting for the delay to pass. |
| 76 |
- However, the virtual machine must have VMware Tools installed. |
| 77 |
type: bool |
| 78 |
default: false |
| 79 |
power_info: |
| 80 |
description: |
| 81 |
- Startup or shutdown settings of virtual machine. |
| 82 |
- This setting will override the system defaults. |
| 83 |
type: dict |
| 84 |
default: |
| 85 |
start_action: none |
| 86 |
start_delay: -1 |
| 87 |
start_order: -1 |
| 88 |
stop_action: systemDefault |
| 89 |
stop_delay: -1 |
| 90 |
wait_for_heartbeat: systemDefault |
| 91 |
suboptions: |
| 92 |
start_action: |
| 93 |
description: |
| 94 |
- Whether to start the virtual machine when the host startup. |
| 95 |
type: str |
| 96 |
choices: ['none', 'powerOn'] |
| 97 |
default: none |
| 98 |
start_delay: |
| 99 |
description: |
| 100 |
- Auto start delay in seconds of virtual machine. |
| 101 |
type: int |
| 102 |
default: -1 |
| 103 |
start_order: |
| 104 |
description: |
| 105 |
- The autostart priority of virtual machine. |
| 106 |
- Virtual machines with a lower number are powered on first. |
| 107 |
- On host shutdown, the virtual machines are shut down in reverse order, meaning those with a higher number are powered off first. |
| 108 |
type: int |
| 109 |
default: -1 |
| 110 |
stop_action: |
| 111 |
description: |
| 112 |
- Stop action executed on the virtual machine when the system stops of virtual machine. |
| 113 |
choices: ['none', 'systemDefault', 'powerOff', 'suspend'] |
| 114 |
type: str |
| 115 |
default: systemDefault |
| 116 |
stop_delay: |
| 117 |
description: |
| 118 |
- Auto stop delay in seconds of virtual machine. |
| 119 |
type: int |
| 120 |
default: -1 |
| 121 |
wait_for_heartbeat: |
| 122 |
description: |
| 123 |
- Continue power on processing when VMware Tools started. |
| 124 |
type: str |
| 125 |
choices: ['no', 'yes', 'systemDefault'] |
| 126 |
default: systemDefault |
| 127 |
extends_documentation_fragment: |
| 128 |
- vmware.vmware.base_options |
| 129 |
|
| 130 |
''' |
| 131 |
|
| 132 |
EXAMPLES = r''' |
| 133 |
--- |
| 134 |
- name: Update for system defaults config. |
| 135 |
community.vmware.vmware_host_auto_start: |
| 136 |
hostname: "{{ hostname }}" |
| 137 |
username: "{{ username }}" |
| 138 |
password: "{{ password }}" |
| 139 |
esxi_hostname: "{{ esxi_hostname }}" |
| 140 |
system_defaults: |
| 141 |
enabled: true |
| 142 |
start_delay: 100 |
| 143 |
stop_action: guestShutdown |
| 144 |
|
| 145 |
- name: Update for powerInfo config of virtual machine. |
| 146 |
community.vmware.vmware_host_auto_start: |
| 147 |
hostname: "{{ hostname }}" |
| 148 |
username: "{{ username }}" |
| 149 |
password: "{{ password }}" |
| 150 |
esxi_hostname: "{{ esxi_hostname }}" |
| 151 |
name: "{{ vm_name }}" |
| 152 |
power_info: |
| 153 |
start_action: powerOn |
| 154 |
start_delay: 10 |
| 155 |
start_order: 1 |
| 156 |
stop_action: powerOff |
| 157 |
wait_for_heartbeat: true |
| 158 |
''' |
| 159 |
|
| 160 |
RETURN = r''' |
| 161 |
system_defaults_config: |
| 162 |
description: Parameter return when system defaults config is changed. |
| 163 |
returned: changed |
| 164 |
type: dict |
| 165 |
sample: >- |
| 166 |
{ |
| 167 |
"enabled": true, |
| 168 |
"start_delay": 120, |
| 169 |
"stop_action": "powerOff", |
| 170 |
"stop_delay": 120, |
| 171 |
"wait_for_heartbeat": false |
| 172 |
} |
| 173 |
power_info_config: |
| 174 |
description: Parameter return when virtual machine power info config is changed. |
| 175 |
returned: changed |
| 176 |
type: dict |
| 177 |
sample: >- |
| 178 |
{ |
| 179 |
"start_action": "powerOn", |
| 180 |
"start_delay": -1, |
| 181 |
"start_order": -1, |
| 182 |
"stop_action": "systemDefault", |
| 183 |
"stop_delay": -1, |
| 184 |
"wait_for_heartbeat": "systemDefault" |
| 185 |
} |
| 186 |
''' |
| 187 |
|
| 188 |
try: |
| 189 |
from pyVmomi import vim |
| 190 |
except ImportError: |
| 191 |
pass |
| 192 |
|
| 193 |
try: |
| 194 |
from collections import OrderedDict |
| 195 |
except ImportError: |
| 196 |
try: |
| 197 |
from ordereddict import OrderedDict |
| 198 |
except ImportError: |
| 199 |
pass |
| 200 |
|
| 201 |
from ansible.module_utils.common.text.converters import to_native |
| 202 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 203 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 204 |
from ansible.module_utils.basic import AnsibleModule |
| 205 |
|
| 206 |
|
| 207 |
class VMwareHostAutoStartManager(PyVmomi): |
| 208 |
def __init__(self, module): |
| 209 |
super(VMwareHostAutoStartManager, self).__init__(module) |
| 210 |
self.esxi_hostname = self.params['esxi_hostname'] |
| 211 |
self.name = self.params['name'] |
| 212 |
self.uuid = self.params['uuid'] |
| 213 |
self.moid = self.params['moid'] |
| 214 |
self.system_defaults = self.params['system_defaults'] |
| 215 |
self.power_info = self.params['power_info'] |
| 216 |
|
| 217 |
def generate_system_defaults_config(self): |
| 218 |
system_defaults_config = vim.host.AutoStartManager.SystemDefaults() |
| 219 |
system_defaults_config.enabled = self.system_defaults['enabled'] |
| 220 |
system_defaults_config.startDelay = self.system_defaults['start_delay'] |
| 221 |
system_defaults_config.stopAction = self.system_defaults['stop_action'] |
| 222 |
system_defaults_config.stopDelay = self.system_defaults['stop_delay'] |
| 223 |
system_defaults_config.waitForHeartbeat = self.system_defaults['wait_for_heartbeat'] |
| 224 |
|
| 225 |
return system_defaults_config |
| 226 |
|
| 227 |
def generate_power_info_config(self): |
| 228 |
power_info_config = vim.host.AutoStartManager.AutoPowerInfo() |
| 229 |
power_info_config.key = self.vm_obj |
| 230 |
power_info_config.startAction = self.power_info['start_action'] |
| 231 |
power_info_config.startDelay = self.power_info['start_delay'] |
| 232 |
power_info_config.startOrder = self.power_info['start_order'] |
| 233 |
power_info_config.stopAction = self.power_info['stop_action'] |
| 234 |
power_info_config.stopDelay = self.power_info['stop_delay'] |
| 235 |
power_info_config.waitForHeartbeat = self.power_info['wait_for_heartbeat'] |
| 236 |
|
| 237 |
return power_info_config |
| 238 |
|
| 239 |
def execute(self): |
| 240 |
result = dict(changed=False, diff={'before': {}, 'after': {}}) |
| 241 |
|
| 242 |
host_obj = self.find_hostsystem_by_name(self.esxi_hostname) |
| 243 |
if not host_obj: |
| 244 |
self.module.fail_json(msg="Cannot find the specified ESXi host: %s" % self.esxi_hostname) |
| 245 |
|
| 246 |
self.vm_obj = None |
| 247 |
if self.name or self.uuid or self.moid: |
| 248 |
self.vm_obj = self.get_vm() |
| 249 |
if not self.vm_obj: |
| 250 |
self.module.fail_json(msg="Cannot find the specified VM: %s" % (self.name or self.uuid or self.moid)) |
| 251 |
elif self.esxi_hostname != self.vm_obj.runtime.host.name: |
| 252 |
self.module.fail_json(msg="%s exists on another host: %s" % (self.name or self.uuid or self.moid, self.vm_obj.runtime.host.name)) |
| 253 |
|
| 254 |
# Check the existing autoStart setting difference. |
| 255 |
system_defaults_config_difference = False |
| 256 |
existing_system_defaults = self.to_json(host_obj.config.autoStart.defaults) |
| 257 |
system_defaults_for_compare = dict( |
| 258 |
enabled=existing_system_defaults['enabled'], |
| 259 |
start_delay=existing_system_defaults['startDelay'], |
| 260 |
stop_action=existing_system_defaults['stopAction'], |
| 261 |
stop_delay=existing_system_defaults['stopDelay'], |
| 262 |
wait_for_heartbeat=existing_system_defaults['waitForHeartbeat'] |
| 263 |
) |
| 264 |
if self.system_defaults: |
| 265 |
if 'guestshutdown' == system_defaults_for_compare['stop_action']: |
| 266 |
system_defaults_for_compare['stop_action'] = 'guestShutdown' |
| 267 |
|
| 268 |
if 'poweroff' == system_defaults_for_compare['stop_action']: |
| 269 |
system_defaults_for_compare['stop_action'] = 'powerOff' |
| 270 |
|
| 271 |
if system_defaults_for_compare != self.system_defaults: |
| 272 |
result['diff']['before']['system_defaults'] = OrderedDict(sorted(system_defaults_for_compare.items())) |
| 273 |
result['diff']['after']['system_defaults'] = OrderedDict(sorted(self.system_defaults.items())) |
| 274 |
system_defaults_config_difference = True |
| 275 |
|
| 276 |
# Check the existing autoStart powerInfo setting difference for VM. |
| 277 |
vm_power_info_config_difference = False |
| 278 |
existing_vm_power_info = {} |
| 279 |
if system_defaults_for_compare['enabled'] and self.vm_obj: |
| 280 |
for vm_power_info in host_obj.config.autoStart.powerInfo: |
| 281 |
if vm_power_info.key == self.vm_obj: |
| 282 |
existing_vm_power_info = self.to_json(vm_power_info) |
| 283 |
break |
| 284 |
|
| 285 |
if existing_vm_power_info: |
| 286 |
vm_power_info_for_compare = dict( |
| 287 |
start_action=existing_vm_power_info['startAction'], |
| 288 |
start_delay=existing_vm_power_info['startDelay'], |
| 289 |
start_order=existing_vm_power_info['startOrder'], |
| 290 |
stop_action=existing_vm_power_info['stopAction'], |
| 291 |
stop_delay=existing_vm_power_info['stopDelay'], |
| 292 |
wait_for_heartbeat=existing_vm_power_info['waitForHeartbeat'] |
| 293 |
) |
| 294 |
else: |
| 295 |
vm_power_info_for_compare = dict( |
| 296 |
start_action='none', |
| 297 |
start_delay=-1, |
| 298 |
start_order=-1, |
| 299 |
stop_action='systemDefault', |
| 300 |
stop_delay=-1, |
| 301 |
wait_for_heartbeat='systemDefault' |
| 302 |
) |
| 303 |
|
| 304 |
if vm_power_info_for_compare != self.power_info: |
| 305 |
result['diff']['before']['power_info'] = OrderedDict(sorted(vm_power_info_for_compare.items())) |
| 306 |
result['diff']['after']['power_info'] = OrderedDict(sorted(self.power_info.items())) |
| 307 |
vm_power_info_config_difference = True |
| 308 |
|
| 309 |
auto_start_manager_config = vim.host.AutoStartManager.Config() |
| 310 |
auto_start_manager_config.powerInfo = [] |
| 311 |
if system_defaults_config_difference or vm_power_info_config_difference: |
| 312 |
if system_defaults_config_difference: |
| 313 |
auto_start_manager_config.defaults = self.generate_system_defaults_config() |
| 314 |
result['system_defaults_config'] = self.system_defaults |
| 315 |
|
| 316 |
if vm_power_info_config_difference: |
| 317 |
auto_start_manager_config.powerInfo = [self.generate_power_info_config()] |
| 318 |
result['power_info_config'] = self.power_info |
| 319 |
|
| 320 |
if self.module.check_mode: |
| 321 |
result['changed'] = True |
| 322 |
self.module.exit_json(**result) |
| 323 |
|
| 324 |
try: |
| 325 |
host_obj.configManager.autoStartManager.ReconfigureAutostart(spec=auto_start_manager_config) |
| 326 |
result['changed'] = True |
| 327 |
self.module.exit_json(**result) |
| 328 |
except Exception as e: |
| 329 |
self.module.fail_json(msg=to_native(e)) |
| 330 |
|
| 331 |
self.module.exit_json(**result) |
| 332 |
else: |
| 333 |
self.module.exit_json(**result) |
| 334 |
|
| 335 |
|
| 336 |
def main(): |
| 337 |
argument_spec = base_argument_spec() |
| 338 |
argument_spec.update(esxi_hostname=dict(type='str', required=True), |
| 339 |
name=dict(type='str'), |
| 340 |
uuid=dict(type='str'), |
| 341 |
use_instance_uuid=dict(type='bool', default=False), |
| 342 |
moid=dict(type='str'), |
| 343 |
system_defaults=dict(type='dict', |
| 344 |
options=dict( |
| 345 |
enabled=dict(type='bool', default=False), |
| 346 |
start_delay=dict(type='int', default=120), |
| 347 |
stop_action=dict(type='str', choices=['none', 'guestShutdown', |
| 348 |
'powerOff', 'suspend'], |
| 349 |
default='powerOff'), |
| 350 |
stop_delay=dict(type='int', default=120), |
| 351 |
wait_for_heartbeat=dict(type='bool', default=False)), |
| 352 |
), |
| 353 |
power_info=dict(type='dict', |
| 354 |
options=dict( |
| 355 |
start_action=dict(type='str', choices=['none', 'powerOn'], default='none'), |
| 356 |
start_delay=dict(type='int', default=-1), |
| 357 |
start_order=dict(type='int', default=-1), |
| 358 |
stop_action=dict(type='str', choices=['none', 'systemDefault', 'powerOff', |
| 359 |
'suspend'], default='systemDefault'), |
| 360 |
stop_delay=dict(type='int', default=-1), |
| 361 |
wait_for_heartbeat=dict(type='str', choices=['no', 'yes', 'systemDefault'], |
| 362 |
default='systemDefault')), |
| 363 |
default=dict( |
| 364 |
start_action='none', |
| 365 |
start_delay=-1, |
| 366 |
start_order=-1, |
| 367 |
stop_action='systemDefault', |
| 368 |
stop_delay=-1, |
| 369 |
wait_for_heartbeat='systemDefault' |
| 370 |
)) |
| 371 |
) |
| 372 |
|
| 373 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 374 |
|
| 375 |
vmware_host_auto_start = VMwareHostAutoStartManager(module) |
| 376 |
vmware_host_auto_start.execute() |
| 377 |
|
| 378 |
|
| 379 |
if __name__ == "__main__": |
| 380 |
main() |