| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2019, Aaron Longchamps, <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: vmware_host_kernel_manager |
| 14 |
short_description: Manage kernel module options on ESXi hosts |
| 15 |
description: |
| 16 |
- This module can be used to manage kernel module options on ESXi hosts. |
| 17 |
- All connected ESXi hosts in scope will be configured when specified. |
| 18 |
- If a host is not connected at time of configuration, it will be marked as such in the output. |
| 19 |
- Kernel module options may require a reboot to take effect which is not covered here. |
| 20 |
- You can use M(ansible.builtin.reboot) or M(community.vmware.vmware_host_powerstate) module to reboot all ESXi host systems. |
| 21 |
author: |
| 22 |
- Aaron Longchamps (@alongchamps) |
| 23 |
options: |
| 24 |
esxi_hostname: |
| 25 |
description: |
| 26 |
- Name of the ESXi host to work on. |
| 27 |
- This parameter is required if O(cluster_name) is not specified. |
| 28 |
type: str |
| 29 |
cluster_name: |
| 30 |
description: |
| 31 |
- Name of the VMware cluster to work on. |
| 32 |
- All ESXi hosts in this cluster will be configured. |
| 33 |
- This parameter is required if O(esxi_hostname) is not specified. |
| 34 |
type: str |
| 35 |
kernel_module_name: |
| 36 |
description: |
| 37 |
- Name of the kernel module to be configured. |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
kernel_module_option: |
| 41 |
description: |
| 42 |
- Specified configurations will be applied to the given module. |
| 43 |
- These values are specified in key=value pairs and separated by a space when there are multiple options. |
| 44 |
required: true |
| 45 |
type: str |
| 46 |
extends_documentation_fragment: |
| 47 |
- vmware.vmware.base_options |
| 48 |
|
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = r''' |
| 52 |
- name: Configure IPv6 to be off via tcpip4 kernel module |
| 53 |
community.vmware.vmware_host_kernel_manager: |
| 54 |
hostname: '{{ vcenter_hostname }}' |
| 55 |
username: '{{ vcenter_username }}' |
| 56 |
password: '{{ vcenter_password }}' |
| 57 |
esxi_hostname: '{{ esxi_hostname }}' |
| 58 |
kernel_module_name: "tcpip4" |
| 59 |
kernel_module_option: "ipv6=0" |
| 60 |
|
| 61 |
- name: Using cluster_name, configure vmw_psp_rr options |
| 62 |
community.vmware.vmware_host_kernel_manager: |
| 63 |
hostname: '{{ vcenter_hostname }}' |
| 64 |
username: '{{ vcenter_username }}' |
| 65 |
password: '{{ vcenter_password }}' |
| 66 |
cluster_name: '{{ virtual_cluster_name }}' |
| 67 |
kernel_module_name: "vmw_psp_rr" |
| 68 |
kernel_module_option: "maxPathsPerDevice=2" |
| 69 |
''' |
| 70 |
|
| 71 |
RETURN = r''' |
| 72 |
host_kernel_status: |
| 73 |
description: |
| 74 |
- dict with information on what was changed, by ESXi host in scope. |
| 75 |
returned: success |
| 76 |
type: dict |
| 77 |
sample: { |
| 78 |
"results": { |
| 79 |
"myhost01.example.com": { |
| 80 |
"changed": true, |
| 81 |
"configured_options": "ipv6=0", |
| 82 |
"msg": "Options have been changed on the kernel module", |
| 83 |
"original_options": "ipv6=1" |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
''' |
| 88 |
|
| 89 |
try: |
| 90 |
from pyVmomi import vim |
| 91 |
except ImportError: |
| 92 |
pass |
| 93 |
|
| 94 |
from ansible.module_utils.basic import AnsibleModule |
| 95 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 96 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 97 |
from ansible.module_utils.common.text.converters import to_native |
| 98 |
|
| 99 |
|
| 100 |
class VmwareKernelManager(PyVmomi): |
| 101 |
def __init__(self, module): |
| 102 |
self.module = module |
| 103 |
super(VmwareKernelManager, self).__init__(module) |
| 104 |
cluster_name = self.params.get('cluster_name', None) |
| 105 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 106 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 107 |
self.kernel_module_name = self.params.get('kernel_module_name') |
| 108 |
self.kernel_module_option = self.params.get('kernel_module_option') |
| 109 |
self.results = {} |
| 110 |
|
| 111 |
if not self.hosts: |
| 112 |
self.module.fail_json(msg="Failed to find a host system that matches the specified criteria") |
| 113 |
|
| 114 |
# find kernel module options for a given kmod_name. If the name is not right, this will throw an exception |
| 115 |
def get_kernel_module_option(self, host, kmod_name): |
| 116 |
host_kernel_manager = host.configManager.kernelModuleSystem |
| 117 |
|
| 118 |
try: |
| 119 |
return host_kernel_manager.QueryConfiguredModuleOptionString(self.kernel_module_name) |
| 120 |
except vim.fault.NotFound as kernel_fault: |
| 121 |
self.module.fail_json(msg="Failed to find kernel module on host '%s'. More information: %s" % (host.name, to_native(kernel_fault.msg))) |
| 122 |
|
| 123 |
# configure the provided kernel module with the specified options |
| 124 |
def apply_kernel_module_option(self, host, kmod_name, kmod_option): |
| 125 |
host_kernel_manager = host.configManager.kernelModuleSystem |
| 126 |
|
| 127 |
if host_kernel_manager: |
| 128 |
try: |
| 129 |
if not self.module.check_mode: |
| 130 |
host_kernel_manager.UpdateModuleOptionString(kmod_name, kmod_option) |
| 131 |
except vim.fault.NotFound as kernel_fault: |
| 132 |
self.module.fail_json(msg="Failed to find kernel module on host '%s'. More information: %s" % (host.name, to_native(kernel_fault))) |
| 133 |
except Exception as kernel_fault: |
| 134 |
self.module.fail_json(msg="Failed to configure kernel module for host '%s' due to: %s" % (host.name, to_native(kernel_fault))) |
| 135 |
|
| 136 |
# evaluate our current configuration against desired options and save results |
| 137 |
def check_host_configuration_state(self): |
| 138 |
change_list = [] |
| 139 |
|
| 140 |
for host in self.hosts: |
| 141 |
changed = False |
| 142 |
msg = "" |
| 143 |
self.results[host.name] = dict() |
| 144 |
|
| 145 |
if host.runtime.connectionState == "connected": |
| 146 |
host_kernel_manager = host.configManager.kernelModuleSystem |
| 147 |
|
| 148 |
if host_kernel_manager: |
| 149 |
# keep track of original options on the kernel module |
| 150 |
original_options = self.get_kernel_module_option(host, self.kernel_module_name) |
| 151 |
desired_options = self.kernel_module_option |
| 152 |
|
| 153 |
# apply as needed, also depending on check mode |
| 154 |
if original_options != desired_options: |
| 155 |
changed = True |
| 156 |
if self.module.check_mode: |
| 157 |
msg = "Options would be changed on the kernel module" |
| 158 |
else: |
| 159 |
self.apply_kernel_module_option(host, self.kernel_module_name, desired_options) |
| 160 |
msg = "Options have been changed on the kernel module" |
| 161 |
self.results[host.name]['configured_options'] = desired_options |
| 162 |
else: |
| 163 |
msg = "Options are already the same" |
| 164 |
|
| 165 |
change_list.append(changed) |
| 166 |
self.results[host.name]['changed'] = changed |
| 167 |
self.results[host.name]['msg'] = msg |
| 168 |
self.results[host.name]['original_options'] = original_options |
| 169 |
|
| 170 |
else: |
| 171 |
msg = "No kernel module manager found on host %s - impossible to configure." % host.name |
| 172 |
self.results[host.name]['changed'] = changed |
| 173 |
self.results[host.name]['msg'] = msg |
| 174 |
else: |
| 175 |
msg = "Host %s is disconnected and cannot be changed." % host.name |
| 176 |
self.results[host.name]['changed'] = changed |
| 177 |
self.results[host.name]['msg'] = msg |
| 178 |
|
| 179 |
self.module.exit_json(changed=any(change_list), host_kernel_status=self.results) |
| 180 |
|
| 181 |
|
| 182 |
def main(): |
| 183 |
argument_spec = base_argument_spec() |
| 184 |
# add the arguments we're going to use for this module |
| 185 |
argument_spec.update( |
| 186 |
cluster_name=dict(type='str', required=False), |
| 187 |
esxi_hostname=dict(type='str', required=False), |
| 188 |
kernel_module_name=dict(type='str', required=True), |
| 189 |
kernel_module_option=dict(type='str', required=True), |
| 190 |
) |
| 191 |
|
| 192 |
# make sure we have a valid target cluster_name or esxi_hostname (not both) |
| 193 |
# and also enable check mode |
| 194 |
module = AnsibleModule( |
| 195 |
argument_spec=argument_spec, |
| 196 |
supports_check_mode=True, |
| 197 |
required_one_of=[ |
| 198 |
['cluster_name', 'esxi_hostname'], |
| 199 |
], |
| 200 |
mutually_exclusive=[ |
| 201 |
['cluster_name', 'esxi_hostname'], |
| 202 |
], |
| 203 |
) |
| 204 |
|
| 205 |
vmware_host_config = VmwareKernelManager(module) |
| 206 |
vmware_host_config.check_host_configuration_state() |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == '__main__': |
| 210 |
main() |