| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, 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 |
--- |
| 14 |
module: vmware_dvswitch_info |
| 15 |
short_description: Gathers info dvswitch configurations |
| 16 |
description: |
| 17 |
- This module can be used to gather information about dvswitch configurations. |
| 18 |
author: |
| 19 |
- sky-joker (@sky-joker) |
| 20 |
options: |
| 21 |
folder: |
| 22 |
description: |
| 23 |
- Specify a folder location of dvswitch to gather information from. |
| 24 |
- 'Examples:' |
| 25 |
- ' folder: /datacenter1/network' |
| 26 |
- ' folder: datacenter1/network' |
| 27 |
- ' folder: /datacenter1/network/folder1' |
| 28 |
- ' folder: datacenter1/network/folder1' |
| 29 |
- ' folder: /folder1/datacenter1/network' |
| 30 |
- ' folder: folder1/datacenter1/network' |
| 31 |
- ' folder: /folder1/datacenter1/network/folder2' |
| 32 |
required: false |
| 33 |
type: str |
| 34 |
switch_name: |
| 35 |
description: |
| 36 |
- Name of a dvswitch to look for. |
| 37 |
- If O(switch_name) not specified gather all dvswitch information. |
| 38 |
aliases: ['switch', 'dvswitch'] |
| 39 |
required: false |
| 40 |
type: str |
| 41 |
schema: |
| 42 |
description: |
| 43 |
- Specify the output schema desired. |
| 44 |
- The 'summary' output schema is the legacy output from the module |
| 45 |
- The 'vsphere' output schema is the vSphere API class definition |
| 46 |
choices: ['summary', 'vsphere'] |
| 47 |
default: 'summary' |
| 48 |
type: str |
| 49 |
properties: |
| 50 |
description: |
| 51 |
- Specify the properties to retrieve. |
| 52 |
- If not specified, all properties are retrieved (deeply). |
| 53 |
- Results are returned in a structure identical to the vsphere API. |
| 54 |
- 'Example:' |
| 55 |
- ' properties: [' |
| 56 |
- ' "summary.name",' |
| 57 |
- ' "summary.numPorts",' |
| 58 |
- ' "config.maxMtu",' |
| 59 |
- ' "overallStatus"' |
| 60 |
- ' ]' |
| 61 |
- Only valid when O(schema=vsphere). |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
required: false |
| 65 |
extends_documentation_fragment: |
| 66 |
- vmware.vmware.base_options |
| 67 |
''' |
| 68 |
|
| 69 |
EXAMPLES = r''' |
| 70 |
- name: Gather all registered dvswitch |
| 71 |
community.vmware.vmware_dvswitch_info: |
| 72 |
hostname: "{{ vcenter_hostname }}" |
| 73 |
username: "{{ vcenter_username }}" |
| 74 |
password: "{{ vcenter_password }}" |
| 75 |
delegate_to: localhost |
| 76 |
register: dvswitch_info |
| 77 |
|
| 78 |
- name: Gather info about specific dvswitch |
| 79 |
community.vmware.vmware_dvswitch_info: |
| 80 |
hostname: "{{ vcenter_hostname }}" |
| 81 |
username: "{{ vcenter_username }}" |
| 82 |
password: "{{ vcenter_password }}" |
| 83 |
switch_name: DVSwitch01 |
| 84 |
delegate_to: localhost |
| 85 |
register: dvswitch_info |
| 86 |
|
| 87 |
- name: Gather info from folder about specific dvswitch |
| 88 |
community.vmware.vmware_dvswitch_info: |
| 89 |
hostname: "{{ vcenter_hostname }}" |
| 90 |
username: "{{ vcenter_username }}" |
| 91 |
password: "{{ vcenter_password }}" |
| 92 |
folder: /datacenter1/network/F01 |
| 93 |
switch_name: DVSwitch02 |
| 94 |
delegate_to: localhost |
| 95 |
register: dvswitch_info |
| 96 |
|
| 97 |
- name: Gather some info from a dvswitch using the vSphere API output schema |
| 98 |
community.vmware.vmware_dvswitch_info: |
| 99 |
hostname: "{{ vcenter_hostname }}" |
| 100 |
username: "{{ vcenter_username }}" |
| 101 |
password: "{{ vcenter_password }}" |
| 102 |
schema: vsphere |
| 103 |
properties: |
| 104 |
- summary.name |
| 105 |
- summary.numPorts |
| 106 |
- config.maxMtu |
| 107 |
- overallStatus |
| 108 |
switch_name: DVSwitch01 |
| 109 |
register: dvswitch_info |
| 110 |
''' |
| 111 |
|
| 112 |
RETURN = r''' |
| 113 |
distributed_virtual_switches: |
| 114 |
description: list of dictionary of dvswitch and their information |
| 115 |
returned: always |
| 116 |
type: list |
| 117 |
sample: |
| 118 |
[ |
| 119 |
{ |
| 120 |
"configure": { |
| 121 |
"folder": "network", |
| 122 |
"hosts": [ |
| 123 |
"esxi-test-02.local", |
| 124 |
"esxi-test-01.local" |
| 125 |
], |
| 126 |
"settings": { |
| 127 |
"healthCheck": { |
| 128 |
"TeamingHealthCheckConfig": false, |
| 129 |
"VlanMtuHealthCheckConfig": false |
| 130 |
}, |
| 131 |
"netflow": { |
| 132 |
"activeFlowTimeout": 60, |
| 133 |
"collectorIpAddress": "", |
| 134 |
"collectorPort": 0, |
| 135 |
"idleFlowTimeout": 15, |
| 136 |
"internalFlowsOnly": false, |
| 137 |
"observationDomainId": 0, |
| 138 |
"samplingRate": 0, |
| 139 |
"switchIpAddress": null |
| 140 |
}, |
| 141 |
"properties": { |
| 142 |
"administratorContact": { |
| 143 |
"contact": null, |
| 144 |
"name": null |
| 145 |
}, |
| 146 |
"advanced": { |
| 147 |
"maxMtu": 1500, |
| 148 |
"multicastFilteringMode": "legacyFiltering" |
| 149 |
}, |
| 150 |
"discoveryProtocol": { |
| 151 |
"operation": "listen", |
| 152 |
"protocol": "cdp" |
| 153 |
}, |
| 154 |
"general": { |
| 155 |
"ioControl": true, |
| 156 |
"name": "DVSwitch01", |
| 157 |
"numPorts": 10, |
| 158 |
"numUplinks": 1, |
| 159 |
"vendor": "VMware, Inc.", |
| 160 |
"version": "6.6.0" |
| 161 |
} |
| 162 |
}, |
| 163 |
"privateVlan": [] |
| 164 |
} |
| 165 |
}, |
| 166 |
"uuid": "50 30 99 9c a7 60 8a 4f-05 9f e7 b5 da df 8f 17" |
| 167 |
} |
| 168 |
] |
| 169 |
''' |
| 170 |
|
| 171 |
try: |
| 172 |
from pyVmomi import vim |
| 173 |
except ImportError: |
| 174 |
pass |
| 175 |
|
| 176 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_obj, find_object_by_name |
| 177 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 178 |
from ansible.module_utils.basic import AnsibleModule |
| 179 |
|
| 180 |
|
| 181 |
class VMwareDvSwitchInfoManager(PyVmomi): |
| 182 |
def __init__(self, module): |
| 183 |
super(VMwareDvSwitchInfoManager, self).__init__(module) |
| 184 |
self.folder = self.params['folder'] |
| 185 |
self.switch_name = self.params['switch_name'] |
| 186 |
|
| 187 |
folder_obj = None |
| 188 |
if self.folder: |
| 189 |
folder_obj = self.content.searchIndex.FindByInventoryPath(self.folder) |
| 190 |
if not folder_obj: |
| 191 |
self.module.fail_json(msg="Failed to find folder specified by %s" % self.folder) |
| 192 |
|
| 193 |
if self.switch_name: |
| 194 |
self.switch_objs = [find_object_by_name(self.content, self.switch_name, vim.DistributedVirtualSwitch, folder_obj)] |
| 195 |
if None in self.switch_objs: |
| 196 |
self.switch_objs = None |
| 197 |
else: |
| 198 |
self.switch_objs = find_obj(self.content, [vim.DistributedVirtualSwitch], '', first=False) |
| 199 |
|
| 200 |
def all_info(self): |
| 201 |
distributed_virtual_switches = [] |
| 202 |
if not self.switch_objs: |
| 203 |
self.module.exit_json(changed=False, distributed_virtual_switches=distributed_virtual_switches) |
| 204 |
|
| 205 |
for switch_obj in self.switch_objs: |
| 206 |
pvlans = [] |
| 207 |
if switch_obj.config.pvlanConfig: |
| 208 |
for vlan in switch_obj.config.pvlanConfig: |
| 209 |
pvlans.append({ |
| 210 |
'primaryVlanId': vlan.primaryVlanId, |
| 211 |
'secondaryVlanId': vlan.secondaryVlanId, |
| 212 |
'pvlanType': vlan.pvlanType |
| 213 |
}) |
| 214 |
|
| 215 |
host_members = [] |
| 216 |
if switch_obj.summary.hostMember: |
| 217 |
for host in switch_obj.summary.hostMember: |
| 218 |
host_members.append(host.name) |
| 219 |
|
| 220 |
health_check = {} |
| 221 |
for health_config in switch_obj.config.healthCheckConfig: |
| 222 |
if isinstance(health_config, vim.dvs.VmwareDistributedVirtualSwitch.VlanMtuHealthCheckConfig): |
| 223 |
health_check['VlanMtuHealthCheckConfig'] = health_config.enable |
| 224 |
elif isinstance(health_config, vim.dvs.VmwareDistributedVirtualSwitch.TeamingHealthCheckConfig): |
| 225 |
health_check['TeamingHealthCheckConfig'] = health_config.enable |
| 226 |
|
| 227 |
distributed_virtual_switches.append({ |
| 228 |
'configure': { |
| 229 |
'settings': { |
| 230 |
'properties': { |
| 231 |
'general': { |
| 232 |
'name': switch_obj.name, |
| 233 |
'vendor': switch_obj.config.productInfo.vendor, |
| 234 |
'version': switch_obj.config.productInfo.version, |
| 235 |
'numUplinks': len(switch_obj.config.uplinkPortPolicy.uplinkPortName), |
| 236 |
'numPorts': switch_obj.summary.numPorts, |
| 237 |
'ioControl': switch_obj.config.networkResourceManagementEnabled, |
| 238 |
}, |
| 239 |
'advanced': { |
| 240 |
'maxMtu': switch_obj.config.maxMtu, |
| 241 |
'multicastFilteringMode': switch_obj.config.multicastFilteringMode, |
| 242 |
}, |
| 243 |
'discoveryProtocol': { |
| 244 |
'protocol': switch_obj.config.linkDiscoveryProtocolConfig.protocol, |
| 245 |
'operation': switch_obj.config.linkDiscoveryProtocolConfig.operation, |
| 246 |
}, |
| 247 |
'administratorContact': { |
| 248 |
'name': switch_obj.config.contact.name, |
| 249 |
'contact': switch_obj.config.contact.contact |
| 250 |
} |
| 251 |
}, |
| 252 |
'privateVlan': pvlans, |
| 253 |
'netflow': { |
| 254 |
'switchIpAddress': switch_obj.config.switchIpAddress, |
| 255 |
'collectorIpAddress': switch_obj.config.ipfixConfig.collectorIpAddress, |
| 256 |
'collectorPort': switch_obj.config.ipfixConfig.collectorPort, |
| 257 |
'observationDomainId': switch_obj.config.ipfixConfig.observationDomainId, |
| 258 |
'activeFlowTimeout': switch_obj.config.ipfixConfig.activeFlowTimeout, |
| 259 |
'idleFlowTimeout': switch_obj.config.ipfixConfig.idleFlowTimeout, |
| 260 |
'samplingRate': switch_obj.config.ipfixConfig.samplingRate, |
| 261 |
'internalFlowsOnly': switch_obj.config.ipfixConfig.internalFlowsOnly |
| 262 |
}, |
| 263 |
'healthCheck': health_check |
| 264 |
}, |
| 265 |
'hosts': host_members, |
| 266 |
'folder': switch_obj.parent.name, |
| 267 |
'name': switch_obj.name, |
| 268 |
}, |
| 269 |
'uuid': switch_obj.uuid, |
| 270 |
}) |
| 271 |
|
| 272 |
self.module.exit_json(changed=False, distributed_virtual_switches=distributed_virtual_switches) |
| 273 |
|
| 274 |
def properties_facts(self): |
| 275 |
distributed_virtual_switches = [] |
| 276 |
for switch_obj in self.switch_objs: |
| 277 |
distributed_virtual_switches.append(self.to_json(switch_obj, self.params.get('properties'))) |
| 278 |
|
| 279 |
self.module.exit_json(changed=False, distributed_virtual_switches=distributed_virtual_switches) |
| 280 |
|
| 281 |
|
| 282 |
def main(): |
| 283 |
argument_spec = base_argument_spec() |
| 284 |
argument_spec.update( |
| 285 |
folder=dict(type='str', required=False), |
| 286 |
switch_name=dict(type='str', required=False, aliases=['switch', 'dvswitch']), |
| 287 |
schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'), |
| 288 |
properties=dict(type='list', required=False, elements='str') |
| 289 |
) |
| 290 |
|
| 291 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 292 |
|
| 293 |
vmware_dvswitch_info_mgr = VMwareDvSwitchInfoManager(module) |
| 294 |
|
| 295 |
if module.params['schema'] == 'summary': |
| 296 |
vmware_dvswitch_info_mgr.all_info() |
| 297 |
else: |
| 298 |
vmware_dvswitch_info_mgr.properties_facts() |
| 299 |
|
| 300 |
|
| 301 |
if __name__ == "__main__": |
| 302 |
main() |