| 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 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vmware_dvs_portgroup_info |
| 16 |
short_description: Gathers info DVS portgroup configurations |
| 17 |
description: |
| 18 |
- This module can be used to gather information about DVS portgroup configurations. |
| 19 |
author: |
| 20 |
- Abhijeet Kasurde (@Akasurde) |
| 21 |
options: |
| 22 |
datacenter: |
| 23 |
description: |
| 24 |
- Name of the datacenter. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
dvswitch: |
| 28 |
description: |
| 29 |
- Name of a dvswitch to look for. |
| 30 |
required: false |
| 31 |
type: str |
| 32 |
show_mac_learning: |
| 33 |
description: |
| 34 |
- Show or hide MAC learning information of the DVS portgroup. |
| 35 |
type: bool |
| 36 |
default: true |
| 37 |
show_network_policy: |
| 38 |
description: |
| 39 |
- Show or hide network policies of DVS portgroup. |
| 40 |
type: bool |
| 41 |
default: true |
| 42 |
show_port_policy: |
| 43 |
description: |
| 44 |
- Show or hide port policies of DVS portgroup. |
| 45 |
type: bool |
| 46 |
default: true |
| 47 |
show_teaming_policy: |
| 48 |
description: |
| 49 |
- Show or hide teaming policies of DVS portgroup. |
| 50 |
type: bool |
| 51 |
default: true |
| 52 |
show_uplinks: |
| 53 |
description: |
| 54 |
- Show or hide uplinks of DVS portgroup. |
| 55 |
type: bool |
| 56 |
default: true |
| 57 |
show_vlan_info: |
| 58 |
description: |
| 59 |
- Show or hide vlan information of the DVS portgroup. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
extends_documentation_fragment: |
| 63 |
- vmware.vmware.base_options |
| 64 |
|
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = r''' |
| 68 |
- name: Get info about DVPG |
| 69 |
community.vmware.vmware_dvs_portgroup_info: |
| 70 |
hostname: "{{ vcenter_hostname }}" |
| 71 |
username: "{{ vcenter_username }}" |
| 72 |
password: "{{ vcenter_password }}" |
| 73 |
datacenter: "{{ datacenter_name }}" |
| 74 |
register: dvpg_info |
| 75 |
|
| 76 |
- name: Get number of ports for portgroup 'dvpg_001' in 'dvs_001' |
| 77 |
debug: |
| 78 |
msg: "{{ item.num_ports }}" |
| 79 |
with_items: |
| 80 |
- "{{ dvpg_info.dvs_portgroup_info['dvs_001'] | json_query(query) }}" |
| 81 |
vars: |
| 82 |
query: "[?portgroup_name=='dvpg_001']" |
| 83 |
''' |
| 84 |
|
| 85 |
RETURN = r''' |
| 86 |
dvs_portgroup_info: |
| 87 |
description: metadata about DVS portgroup configuration |
| 88 |
returned: on success |
| 89 |
type: dict |
| 90 |
sample: { |
| 91 |
"dvs_0":[ |
| 92 |
{ |
| 93 |
"active_uplinks": [ |
| 94 |
"uplink 1" |
| 95 |
], |
| 96 |
"description": null, |
| 97 |
"dvswitch_name": "dvs_001", |
| 98 |
"key": "dvportgroup-1014", |
| 99 |
"mac_learning": { |
| 100 |
"allow_unicast_flooding": null, |
| 101 |
"enabled": false, |
| 102 |
"limit": null, |
| 103 |
"limit_policy": null |
| 104 |
}, |
| 105 |
"moid": "dvportgroup-1014", |
| 106 |
"network_policy": { |
| 107 |
"forged_transmits": false, |
| 108 |
"mac_changes": false, |
| 109 |
"promiscuous": false |
| 110 |
}, |
| 111 |
"num_ports": 8, |
| 112 |
"port_allocation": "elastic", |
| 113 |
"port_binding": "static", |
| 114 |
"standby_uplinks": [], |
| 115 |
"port_policy": { |
| 116 |
"block_override": true, |
| 117 |
"ipfix_override": false, |
| 118 |
"live_port_move": false, |
| 119 |
"network_rp_override": false, |
| 120 |
"port_config_reset_at_disconnect": true, |
| 121 |
"shaping_override": false, |
| 122 |
"traffic_filter_override": false, |
| 123 |
"uplink_teaming_override": false, |
| 124 |
"vendor_config_override": false, |
| 125 |
"vlan_override": false |
| 126 |
}, |
| 127 |
"portgroup_name": "dvpg_001", |
| 128 |
"teaming_policy": { |
| 129 |
"inbound_policy": true, |
| 130 |
"notify_switches": true, |
| 131 |
"policy": "loadbalance_srcid", |
| 132 |
"rolling_order": false |
| 133 |
}, |
| 134 |
"vlan_info": { |
| 135 |
"trunk": false, |
| 136 |
"pvlan": false, |
| 137 |
"vlan_id": 0 |
| 138 |
}, |
| 139 |
"type": "earlyBinding" |
| 140 |
}, |
| 141 |
] |
| 142 |
} |
| 143 |
''' |
| 144 |
|
| 145 |
try: |
| 146 |
from pyVmomi import vim |
| 147 |
except ImportError: |
| 148 |
pass |
| 149 |
|
| 150 |
from ansible.module_utils.basic import AnsibleModule |
| 151 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 152 |
PyVmomi, |
| 153 |
get_all_objs, |
| 154 |
find_dvs_by_name) |
| 155 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 156 |
from urllib.parse import unquote |
| 157 |
|
| 158 |
|
| 159 |
class DVSPortgroupInfoManager(PyVmomi): |
| 160 |
def __init__(self, module): |
| 161 |
super(DVSPortgroupInfoManager, self).__init__(module) |
| 162 |
self.dc_name = self.params['datacenter'] |
| 163 |
self.dvs_name = self.params['dvswitch'] |
| 164 |
|
| 165 |
datacenter = self.find_datacenter_by_name(self.dc_name) |
| 166 |
if datacenter is None: |
| 167 |
self.module.fail_json(msg="Failed to find the datacenter %s" % self.dc_name) |
| 168 |
if self.dvs_name: |
| 169 |
# User specified specific dvswitch name to gather information |
| 170 |
dvsn = find_dvs_by_name(self.content, self.dvs_name) |
| 171 |
if dvsn is None: |
| 172 |
self.module.fail_json(msg="Failed to find the dvswitch %s" % self.dvs_name) |
| 173 |
|
| 174 |
self.dvsls = [dvsn] |
| 175 |
else: |
| 176 |
# default behaviour, gather information about all dvswitches |
| 177 |
self.dvsls = get_all_objs(self.content, [vim.DistributedVirtualSwitch], folder=datacenter.networkFolder) |
| 178 |
|
| 179 |
def get_vlan_info(self, vlan_obj=None): |
| 180 |
""" |
| 181 |
Return vlan information from given object |
| 182 |
Args: |
| 183 |
vlan_obj: vlan managed object |
| 184 |
Returns: Dict of vlan details of the specific object |
| 185 |
""" |
| 186 |
|
| 187 |
vdret = dict() |
| 188 |
if not vlan_obj: |
| 189 |
return vdret |
| 190 |
|
| 191 |
if isinstance(vlan_obj, vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec): |
| 192 |
vlan_id_list = [] |
| 193 |
for vli in vlan_obj.vlanId: |
| 194 |
if vli.start == vli.end: |
| 195 |
vlan_id_list.append(str(vli.start)) |
| 196 |
else: |
| 197 |
vlan_id_list.append(str(vli.start) + "-" + str(vli.end)) |
| 198 |
vdret = dict(trunk=True, pvlan=False, vlan_id=vlan_id_list) |
| 199 |
elif isinstance(vlan_obj, vim.dvs.VmwareDistributedVirtualSwitch.PvlanSpec): |
| 200 |
vdret = dict(trunk=False, pvlan=True, vlan_id=str(vlan_obj.pvlanId)) |
| 201 |
else: |
| 202 |
vdret = dict(trunk=False, pvlan=False, vlan_id=str(vlan_obj.vlanId)) |
| 203 |
|
| 204 |
return vdret |
| 205 |
|
| 206 |
def gather_dvs_portgroup_info(self): |
| 207 |
dvs_lists = self.dvsls |
| 208 |
result = dict() |
| 209 |
for dvs in dvs_lists: |
| 210 |
result[dvs.name] = list() |
| 211 |
for dvs_pg in dvs.portgroup: |
| 212 |
mac_learning = dict() |
| 213 |
network_policy = dict() |
| 214 |
teaming_policy = dict() |
| 215 |
port_policy = dict() |
| 216 |
vlan_info = dict() |
| 217 |
active_uplinks = list() |
| 218 |
standby_uplinks = list() |
| 219 |
|
| 220 |
if dvs_pg.config.type == 'ephemeral': |
| 221 |
port_binding = 'ephemeral' |
| 222 |
else: |
| 223 |
port_binding = 'static' |
| 224 |
|
| 225 |
if dvs_pg.config.autoExpand is True: |
| 226 |
port_allocation = 'elastic' |
| 227 |
else: |
| 228 |
port_allocation = 'fixed' |
| 229 |
|
| 230 |
if self.module.params['show_network_policy']: |
| 231 |
network_policy = dict( |
| 232 |
forged_transmits=dvs_pg.config.defaultPortConfig.macManagementPolicy.forgedTransmits, |
| 233 |
promiscuous=dvs_pg.config.defaultPortConfig.macManagementPolicy.allowPromiscuous, |
| 234 |
mac_changes=dvs_pg.config.defaultPortConfig.macManagementPolicy.macChanges |
| 235 |
) |
| 236 |
|
| 237 |
if self.module.params['show_mac_learning']: |
| 238 |
macLearningPolicy = dvs_pg.config.defaultPortConfig.macManagementPolicy.macLearningPolicy |
| 239 |
mac_learning = dict( |
| 240 |
allow_unicast_flooding=macLearningPolicy.allowUnicastFlooding, |
| 241 |
enabled=macLearningPolicy.enabled, |
| 242 |
limit=macLearningPolicy.limit, |
| 243 |
limit_policy=macLearningPolicy.limitPolicy |
| 244 |
) |
| 245 |
|
| 246 |
if self.module.params['show_teaming_policy']: |
| 247 |
teaming_policy = dict( |
| 248 |
policy=dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.policy.value, |
| 249 |
inbound_policy=dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.reversePolicy.value, |
| 250 |
notify_switches=dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.notifySwitches.value, |
| 251 |
rolling_order=dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.rollingOrder.value, |
| 252 |
) |
| 253 |
|
| 254 |
if self.module.params['show_uplinks'] and \ |
| 255 |
dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy and \ |
| 256 |
dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder: |
| 257 |
active_uplinks = dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.activeUplinkPort |
| 258 |
standby_uplinks = dvs_pg.config.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.standbyUplinkPort |
| 259 |
|
| 260 |
if self.params['show_port_policy']: |
| 261 |
port_policy = dict( |
| 262 |
block_override=dvs_pg.config.policy.blockOverrideAllowed, |
| 263 |
ipfix_override=dvs_pg.config.policy.ipfixOverrideAllowed, |
| 264 |
live_port_move=dvs_pg.config.policy.livePortMovingAllowed, |
| 265 |
network_rp_override=dvs_pg.config.policy.networkResourcePoolOverrideAllowed, |
| 266 |
port_config_reset_at_disconnect=dvs_pg.config.policy.portConfigResetAtDisconnect, |
| 267 |
shaping_override=dvs_pg.config.policy.shapingOverrideAllowed, |
| 268 |
traffic_filter_override=dvs_pg.config.policy.trafficFilterOverrideAllowed, |
| 269 |
uplink_teaming_override=dvs_pg.config.policy.uplinkTeamingOverrideAllowed, |
| 270 |
vendor_config_override=dvs_pg.config.policy.vendorConfigOverrideAllowed, |
| 271 |
vlan_override=dvs_pg.config.policy.vlanOverrideAllowed |
| 272 |
) |
| 273 |
|
| 274 |
if self.params['show_vlan_info']: |
| 275 |
vlan_info = self.get_vlan_info(dvs_pg.config.defaultPortConfig.vlan) |
| 276 |
|
| 277 |
dvpg_details = dict( |
| 278 |
portgroup_name=unquote(dvs_pg.name), |
| 279 |
moid=dvs_pg._moId, |
| 280 |
num_ports=dvs_pg.config.numPorts, |
| 281 |
dvswitch_name=dvs_pg.config.distributedVirtualSwitch.name, |
| 282 |
description=dvs_pg.config.description, |
| 283 |
type=dvs_pg.config.type, |
| 284 |
port_binding=port_binding, |
| 285 |
port_allocation=port_allocation, |
| 286 |
teaming_policy=teaming_policy, |
| 287 |
port_policy=port_policy, |
| 288 |
mac_learning=mac_learning, |
| 289 |
network_policy=network_policy, |
| 290 |
vlan_info=vlan_info, |
| 291 |
key=dvs_pg.key, |
| 292 |
active_uplinks=active_uplinks, |
| 293 |
standby_uplinks=standby_uplinks, |
| 294 |
) |
| 295 |
result[dvs.name].append(dvpg_details) |
| 296 |
|
| 297 |
return result |
| 298 |
|
| 299 |
|
| 300 |
def main(): |
| 301 |
argument_spec = base_argument_spec() |
| 302 |
argument_spec.update( |
| 303 |
datacenter=dict(type='str', required=True), |
| 304 |
show_mac_learning=dict(type='bool', default=True), |
| 305 |
show_network_policy=dict(type='bool', default=True), |
| 306 |
show_teaming_policy=dict(type='bool', default=True), |
| 307 |
show_uplinks=dict(type='bool', default=True), |
| 308 |
show_port_policy=dict(type='bool', default=True), |
| 309 |
dvswitch=dict(), |
| 310 |
show_vlan_info=dict(type='bool', default=False), |
| 311 |
) |
| 312 |
module = AnsibleModule( |
| 313 |
argument_spec=argument_spec, |
| 314 |
supports_check_mode=True, |
| 315 |
) |
| 316 |
|
| 317 |
dvs_pg_mgr = DVSPortgroupInfoManager(module) |
| 318 |
module.exit_json(changed=False, |
| 319 |
dvs_portgroup_info=dvs_pg_mgr.gather_dvs_portgroup_info()) |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__": |
| 323 |
main() |