| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Christian Kotte <[email protected]> |
| 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 |
--- |
| 14 |
module: vmware_host_powermgmt_policy |
| 15 |
short_description: Manages the Power Management Policy of an ESXI host system |
| 16 |
description: |
| 17 |
- This module can be used to manage the Power Management Policy of ESXi host systems in given vCenter infrastructure. |
| 18 |
author: |
| 19 |
- Christian Kotte (@ckotte) <[email protected]> |
| 20 |
options: |
| 21 |
policy: |
| 22 |
description: |
| 23 |
- Set the Power Management Policy of the host system. |
| 24 |
choices: [ 'high-performance', 'balanced', 'low-power', 'custom' ] |
| 25 |
default: 'balanced' |
| 26 |
type: str |
| 27 |
esxi_hostname: |
| 28 |
description: |
| 29 |
- Name of the host system to work with. |
| 30 |
- This is required parameter if O(cluster_name) is not specified. |
| 31 |
type: str |
| 32 |
cluster_name: |
| 33 |
description: |
| 34 |
- Name of the cluster from which all host systems will be used. |
| 35 |
- This is required parameter if O(esxi_hostname) is not specified. |
| 36 |
type: str |
| 37 |
extends_documentation_fragment: |
| 38 |
- vmware.vmware.base_options |
| 39 |
|
| 40 |
''' |
| 41 |
|
| 42 |
EXAMPLES = r''' |
| 43 |
- name: Set the Power Management Policy of a host system to high-performance |
| 44 |
community.vmware.vmware_host_powermgmt_policy: |
| 45 |
hostname: '{{ vcenter_hostname }}' |
| 46 |
username: '{{ vcenter_username }}' |
| 47 |
password: '{{ vcenter_password }}' |
| 48 |
esxi_hostname: '{{ esxi_host }}' |
| 49 |
policy: high-performance |
| 50 |
delegate_to: localhost |
| 51 |
|
| 52 |
- name: Set the Power Management Policy of all host systems from cluster to high-performance |
| 53 |
community.vmware.vmware_host_powermgmt_policy: |
| 54 |
hostname: '{{ vcenter_hostname }}' |
| 55 |
username: '{{ vcenter_username }}' |
| 56 |
password: '{{ vcenter_password }}' |
| 57 |
cluster_name: '{{ cluster_name }}' |
| 58 |
policy: high-performance |
| 59 |
delegate_to: localhost |
| 60 |
''' |
| 61 |
|
| 62 |
RETURN = r''' |
| 63 |
result: |
| 64 |
description: metadata about host system's Power Management Policy |
| 65 |
returned: always |
| 66 |
type: dict |
| 67 |
sample: { |
| 68 |
"changed": true, |
| 69 |
"result": { |
| 70 |
"esxi01": { |
| 71 |
"changed": true, |
| 72 |
"current_state": "high-performance", |
| 73 |
"desired_state": "high-performance", |
| 74 |
"msg": "Power policy changed", |
| 75 |
"previous_state": "balanced" |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
''' |
| 80 |
|
| 81 |
try: |
| 82 |
from pyVmomi import vim, vmodl |
| 83 |
except ImportError: |
| 84 |
pass |
| 85 |
|
| 86 |
from ansible.module_utils.basic import AnsibleModule |
| 87 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 88 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 89 |
from ansible.module_utils.common.text.converters import to_native |
| 90 |
|
| 91 |
|
| 92 |
class VmwareHostPowerManagement(PyVmomi): |
| 93 |
""" |
| 94 |
Class to manage power management policy of an ESXi host system |
| 95 |
""" |
| 96 |
|
| 97 |
def __init__(self, module): |
| 98 |
super(VmwareHostPowerManagement, self).__init__(module) |
| 99 |
cluster_name = self.params.get('cluster_name') |
| 100 |
esxi_host_name = self.params.get('esxi_hostname') |
| 101 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 102 |
if not self.hosts: |
| 103 |
self.module.fail_json(msg="Failed to find host system with given configuration.") |
| 104 |
|
| 105 |
def ensure(self): |
| 106 |
""" |
| 107 |
Manage power management policy of an ESXi host system |
| 108 |
""" |
| 109 |
results = dict(changed=False, result=dict()) |
| 110 |
policy = self.params.get('policy') |
| 111 |
host_change_list = [] |
| 112 |
power_policies = { |
| 113 |
'high-performance': { |
| 114 |
'key': 1, |
| 115 |
'short_name': 'static' |
| 116 |
}, |
| 117 |
'balanced': { |
| 118 |
'key': 2, |
| 119 |
'short_name': 'dynamic' |
| 120 |
}, |
| 121 |
'low-power': { |
| 122 |
'key': 3, |
| 123 |
'short_name': 'low' |
| 124 |
}, |
| 125 |
'custom': { |
| 126 |
'key': 4, |
| 127 |
'short_name': 'custom' |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
for host in self.hosts: |
| 132 |
changed = False |
| 133 |
results['result'][host.name] = dict(msg='') |
| 134 |
|
| 135 |
power_system = host.configManager.powerSystem |
| 136 |
|
| 137 |
# get current power policy |
| 138 |
power_system_info = power_system.info |
| 139 |
current_host_power_policy = power_system_info.currentPolicy |
| 140 |
|
| 141 |
# the "name" and "description" parameters are pretty useless |
| 142 |
# they store only strings containing "PowerPolicy.<shortName>.name" and "PowerPolicy.<shortName>.description" |
| 143 |
if current_host_power_policy.shortName == "static": |
| 144 |
current_policy = 'high-performance' |
| 145 |
elif current_host_power_policy.shortName == "dynamic": |
| 146 |
current_policy = 'balanced' |
| 147 |
elif current_host_power_policy.shortName == "low": |
| 148 |
current_policy = 'low-power' |
| 149 |
elif current_host_power_policy.shortName == "custom": |
| 150 |
current_policy = 'custom' |
| 151 |
|
| 152 |
results['result'][host.name]['desired_state'] = policy |
| 153 |
|
| 154 |
# Don't do anything if the power policy is already configured |
| 155 |
if current_host_power_policy.key == power_policies[policy]['key']: |
| 156 |
results['result'][host.name]['changed'] = changed |
| 157 |
results['result'][host.name]['previous_state'] = current_policy |
| 158 |
results['result'][host.name]['current_state'] = policy |
| 159 |
results['result'][host.name]['msg'] = "Power policy is already configured" |
| 160 |
else: |
| 161 |
# get available power policies and check if policy is included |
| 162 |
supported_policy = False |
| 163 |
power_system_capability = power_system.capability |
| 164 |
available_host_power_policies = power_system_capability.availablePolicy |
| 165 |
for available_policy in available_host_power_policies: |
| 166 |
if available_policy.shortName == power_policies[policy]['short_name']: |
| 167 |
supported_policy = True |
| 168 |
if supported_policy: |
| 169 |
if not self.module.check_mode: |
| 170 |
try: |
| 171 |
power_system.ConfigurePowerPolicy(key=power_policies[policy]['key']) |
| 172 |
changed = True |
| 173 |
results['result'][host.name]['changed'] = True |
| 174 |
results['result'][host.name]['msg'] = "Power policy changed" |
| 175 |
except vmodl.fault.InvalidArgument: |
| 176 |
self.module.fail_json(msg="Invalid power policy key provided for host '%s'" % host.name) |
| 177 |
except vim.fault.HostConfigFault as host_config_fault: |
| 178 |
self.module.fail_json(msg="Failed to configure power policy for host '%s': %s" % |
| 179 |
(host.name, to_native(host_config_fault.msg))) |
| 180 |
else: |
| 181 |
changed = True |
| 182 |
results['result'][host.name]['changed'] = True |
| 183 |
results['result'][host.name]['msg'] = "Power policy will be changed" |
| 184 |
results['result'][host.name]['previous_state'] = current_policy |
| 185 |
results['result'][host.name]['current_state'] = policy |
| 186 |
else: |
| 187 |
changed = False |
| 188 |
results['result'][host.name]['changed'] = changed |
| 189 |
results['result'][host.name]['previous_state'] = current_policy |
| 190 |
results['result'][host.name]['current_state'] = current_policy |
| 191 |
self.module.fail_json(msg="Power policy '%s' isn't supported for host '%s'" % |
| 192 |
(policy, host.name)) |
| 193 |
|
| 194 |
host_change_list.append(changed) |
| 195 |
|
| 196 |
if any(host_change_list): |
| 197 |
results['changed'] = True |
| 198 |
self.module.exit_json(**results) |
| 199 |
|
| 200 |
|
| 201 |
def main(): |
| 202 |
""" |
| 203 |
Main |
| 204 |
""" |
| 205 |
argument_spec = base_argument_spec() |
| 206 |
argument_spec.update( |
| 207 |
policy=dict(type='str', default='balanced', |
| 208 |
choices=['high-performance', 'balanced', 'low-power', 'custom']), |
| 209 |
esxi_hostname=dict(type='str', required=False), |
| 210 |
cluster_name=dict(type='str', required=False), |
| 211 |
) |
| 212 |
|
| 213 |
module = AnsibleModule(argument_spec=argument_spec, |
| 214 |
required_one_of=[ |
| 215 |
['cluster_name', 'esxi_hostname'], |
| 216 |
], |
| 217 |
supports_check_mode=True |
| 218 |
) |
| 219 |
|
| 220 |
host_power_management = VmwareHostPowerManagement(module) |
| 221 |
host_power_management.ensure() |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == '__main__': |
| 225 |
main() |