| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Abhijeet Kasurde <[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_vmkernel_info |
| 15 |
short_description: Gathers VMKernel info about an ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to gather VMKernel information about an ESXi host from given ESXi hostname or cluster name. |
| 18 |
author: |
| 19 |
- Abhijeet Kasurde (@Akasurde) |
| 20 |
options: |
| 21 |
cluster_name: |
| 22 |
description: |
| 23 |
- Name of the cluster. |
| 24 |
- VMKernel information about each ESXi server will be returned for the given cluster. |
| 25 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 26 |
type: str |
| 27 |
esxi_hostname: |
| 28 |
description: |
| 29 |
- ESXi hostname. |
| 30 |
- VMKernel information about this ESXi server will be returned. |
| 31 |
- If O(cluster_name) is not given, this parameter is required. |
| 32 |
type: str |
| 33 |
extends_documentation_fragment: |
| 34 |
- vmware.vmware.base_options |
| 35 |
|
| 36 |
''' |
| 37 |
|
| 38 |
EXAMPLES = r''' |
| 39 |
- name: Gather VMKernel info about all ESXi Host in given Cluster |
| 40 |
community.vmware.vmware_vmkernel_info: |
| 41 |
hostname: '{{ vcenter_hostname }}' |
| 42 |
username: '{{ vcenter_username }}' |
| 43 |
password: '{{ vcenter_password }}' |
| 44 |
cluster_name: cluster_name |
| 45 |
delegate_to: localhost |
| 46 |
register: cluster_host_vmks |
| 47 |
|
| 48 |
- name: Gather VMKernel info about ESXi Host |
| 49 |
community.vmware.vmware_vmkernel_info: |
| 50 |
hostname: '{{ vcenter_hostname }}' |
| 51 |
username: '{{ vcenter_username }}' |
| 52 |
password: '{{ vcenter_password }}' |
| 53 |
esxi_hostname: '{{ esxi_hostname }}' |
| 54 |
delegate_to: localhost |
| 55 |
register: host_vmks |
| 56 |
''' |
| 57 |
|
| 58 |
RETURN = r''' |
| 59 |
host_vmk_info: |
| 60 |
description: metadata about VMKernel present on given host system |
| 61 |
returned: success |
| 62 |
type: dict |
| 63 |
sample: |
| 64 |
{ |
| 65 |
"10.76.33.208": [ |
| 66 |
{ |
| 67 |
"device": "vmk0", |
| 68 |
"dhcp": true, |
| 69 |
"enable_ft": false, |
| 70 |
"enable_management": true, |
| 71 |
"enable_vmotion": false, |
| 72 |
"enable_vsan": false, |
| 73 |
"ipv4_address": "10.76.33.28", |
| 74 |
"ipv4_subnet_mask": "255.255.255.0", |
| 75 |
"key": "key-vim.host.VirtualNic-vmk0", |
| 76 |
"mac": "52:54:00:12:50:ce", |
| 77 |
"mtu": 1500, |
| 78 |
"portgroup": "Management Network", |
| 79 |
"stack": "defaultTcpipStack" |
| 80 |
}, |
| 81 |
] |
| 82 |
} |
| 83 |
|
| 84 |
''' |
| 85 |
|
| 86 |
try: |
| 87 |
from pyVmomi import vim, vmodl |
| 88 |
except ImportError: |
| 89 |
pass |
| 90 |
|
| 91 |
from ansible.module_utils.basic import AnsibleModule |
| 92 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 93 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 94 |
from ansible.module_utils.common.text.converters import to_native |
| 95 |
|
| 96 |
|
| 97 |
class VmkernelInfoManager(PyVmomi): |
| 98 |
def __init__(self, module): |
| 99 |
super(VmkernelInfoManager, self).__init__(module) |
| 100 |
cluster_name = self.params.get('cluster_name', None) |
| 101 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 102 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 103 |
self.service_type_vmks = dict() |
| 104 |
self.get_all_vmks_by_service_type() |
| 105 |
|
| 106 |
def get_all_vmks_by_service_type(self): |
| 107 |
""" |
| 108 |
Function to return information about service types and VMKernel |
| 109 |
|
| 110 |
""" |
| 111 |
for host in self.hosts: |
| 112 |
self.service_type_vmks[host.name] = dict(vmotion=[], vsan=[], management=[], faultToleranceLogging=[]) |
| 113 |
for service_type in self.service_type_vmks[host.name].keys(): |
| 114 |
vmks_list = self.query_service_type_for_vmks(host, service_type) |
| 115 |
self.service_type_vmks[host.name][service_type] = vmks_list |
| 116 |
|
| 117 |
def query_service_type_for_vmks(self, host_system, service_type): |
| 118 |
""" |
| 119 |
Function to return list of VMKernels |
| 120 |
Args: |
| 121 |
host_system: Host system managed object |
| 122 |
service_type: Name of service type |
| 123 |
|
| 124 |
Returns: List of VMKernel which belongs to that service type |
| 125 |
|
| 126 |
""" |
| 127 |
vmks_list = [] |
| 128 |
query = None |
| 129 |
try: |
| 130 |
query = host_system.configManager.virtualNicManager.QueryNetConfig(service_type) |
| 131 |
except vim.fault.HostConfigFault as config_fault: |
| 132 |
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to" |
| 133 |
" host config fault : %s" % (service_type, to_native(config_fault.msg))) |
| 134 |
except vmodl.fault.InvalidArgument as invalid_argument: |
| 135 |
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to" |
| 136 |
" invalid arguments : %s" % (service_type, to_native(invalid_argument.msg))) |
| 137 |
except Exception as e: |
| 138 |
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to" |
| 139 |
"%s" % (service_type, to_native(e))) |
| 140 |
|
| 141 |
if not query or not query.selectedVnic: |
| 142 |
return vmks_list |
| 143 |
selected_vnics = list(query.selectedVnic) |
| 144 |
vnics_with_service_type = [vnic.device for vnic in query.candidateVnic if vnic.key in selected_vnics] |
| 145 |
return vnics_with_service_type |
| 146 |
|
| 147 |
def gather_host_vmk_info(self): |
| 148 |
hosts_info = {} |
| 149 |
|
| 150 |
for host in self.hosts: |
| 151 |
host_vmk_info = [] |
| 152 |
host_network_system = host.config.network |
| 153 |
if host_network_system: |
| 154 |
vmks_config = host.config.network.vnic |
| 155 |
for vmk in vmks_config: |
| 156 |
host_vmk_info.append(dict( |
| 157 |
device=vmk.device, |
| 158 |
key=vmk.key, |
| 159 |
portgroup=vmk.portgroup, |
| 160 |
ipv4_address=vmk.spec.ip.ipAddress, |
| 161 |
ipv4_subnet_mask=vmk.spec.ip.subnetMask, |
| 162 |
dhcp=vmk.spec.ip.dhcp, |
| 163 |
mac=vmk.spec.mac, |
| 164 |
mtu=vmk.spec.mtu, |
| 165 |
stack=vmk.spec.netStackInstanceKey, |
| 166 |
enable_vsan=vmk.device in self.service_type_vmks[host.name]['vsan'], |
| 167 |
enable_vmotion=vmk.device in self.service_type_vmks[host.name]['vmotion'], |
| 168 |
enable_management=vmk.device in self.service_type_vmks[host.name]['management'], |
| 169 |
enable_ft=vmk.device in self.service_type_vmks[host.name]['faultToleranceLogging'], |
| 170 |
) |
| 171 |
) |
| 172 |
hosts_info[host.name] = host_vmk_info |
| 173 |
return hosts_info |
| 174 |
|
| 175 |
|
| 176 |
def main(): |
| 177 |
argument_spec = base_argument_spec() |
| 178 |
argument_spec.update( |
| 179 |
cluster_name=dict(type='str', required=False), |
| 180 |
esxi_hostname=dict(type='str', required=False), |
| 181 |
) |
| 182 |
|
| 183 |
module = AnsibleModule( |
| 184 |
argument_spec=argument_spec, |
| 185 |
required_one_of=[ |
| 186 |
['cluster_name', 'esxi_hostname'], |
| 187 |
], |
| 188 |
supports_check_mode=True |
| 189 |
) |
| 190 |
|
| 191 |
vmware_vmk_config = VmkernelInfoManager(module) |
| 192 |
module.exit_json(changed=False, host_vmk_info=vmware_vmk_config.gather_host_vmk_info()) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
main() |