| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Ansible Project |
| 5 |
# Copyright: (c) 2018, CrySyS Lab <www.crysys.hu> |
| 6 |
# Copyright: (c) 2018, Peter Gyorgy <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
|
| 15 |
DOCUMENTATION = r''' |
| 16 |
--- |
| 17 |
module: vmware_vspan_session |
| 18 |
short_description: Create or remove a Port Mirroring session. |
| 19 |
description: |
| 20 |
- This module can be used to create, delete or edit different kind of port mirroring sessions. |
| 21 |
author: |
| 22 |
- Peter Gyorgy (@gyorgypeter) |
| 23 |
options: |
| 24 |
switch: |
| 25 |
description: |
| 26 |
- The name of the distributed vSwitch on which to add or remove the mirroring session. |
| 27 |
required: true |
| 28 |
aliases: [ 'switch_name' ] |
| 29 |
type: str |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of the session. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
state: |
| 36 |
choices: |
| 37 |
- 'present' |
| 38 |
- 'absent' |
| 39 |
description: |
| 40 |
- Create or remove the session. |
| 41 |
required: true |
| 42 |
type: str |
| 43 |
session_type: |
| 44 |
default: 'dvPortMirror' |
| 45 |
choices: |
| 46 |
- 'encapsulatedRemoteMirrorSource' |
| 47 |
- 'remoteMirrorDest' |
| 48 |
- 'remoteMirrorSource' |
| 49 |
- 'dvPortMirror' |
| 50 |
description: |
| 51 |
- Select the mirroring type. |
| 52 |
- In V(encapsulatedRemoteMirrorSource) session, Distributed Ports can be used as source entities, |
| 53 |
and IP address can be used as destination entities. |
| 54 |
- In V(remoteMirrorDest) session, VLAN IDs can be used as source entities, and |
| 55 |
Distributed Ports can be used as destination entities. |
| 56 |
- In V(remoteMirrorSource) session, Distributed Ports can be used as source |
| 57 |
entities, and uplink ports name can be used as destination entities. |
| 58 |
- In V(dvPortMirror) session, Distributed Ports can be used as both source and |
| 59 |
destination entities. |
| 60 |
required: false |
| 61 |
type: str |
| 62 |
enabled: |
| 63 |
type: bool |
| 64 |
default: true |
| 65 |
description: |
| 66 |
- Whether the session is enabled. |
| 67 |
description: |
| 68 |
description: |
| 69 |
- The description for the session. |
| 70 |
required: false |
| 71 |
type: str |
| 72 |
source_port_transmitted: |
| 73 |
description: |
| 74 |
- Source port for which transmitted packets are mirrored. |
| 75 |
required: false |
| 76 |
type: str |
| 77 |
source_port_received: |
| 78 |
description: |
| 79 |
- Source port for which received packets are mirrored. |
| 80 |
required: false |
| 81 |
type: str |
| 82 |
destination_port: |
| 83 |
description: |
| 84 |
- Destination port that received the mirrored packets. |
| 85 |
- Also any port designated in the value of this |
| 86 |
property can not match the source port in any of the Distributed Port Mirroring session. |
| 87 |
required: false |
| 88 |
type: str |
| 89 |
encapsulation_vlan_id: |
| 90 |
description: |
| 91 |
- VLAN ID used to encapsulate the mirrored traffic. |
| 92 |
required: false |
| 93 |
type: int |
| 94 |
strip_original_vlan: |
| 95 |
description: |
| 96 |
- Whether to strip the original VLAN tag. |
| 97 |
- If false, the original VLAN tag will be preserved on the mirrored traffic. |
| 98 |
- If O(encapsulation_vlan_id) has been set and this property is V(false), the frames will be double tagged |
| 99 |
with the original VLAN ID as the inner tag. |
| 100 |
type: bool |
| 101 |
required: false |
| 102 |
mirrored_packet_length: |
| 103 |
description: |
| 104 |
- An integer that describes how much of each frame to mirror. |
| 105 |
- If unset, all of the frame would be mirrored. |
| 106 |
- Setting this property to a smaller value is useful when the consumer will look only at the headers. |
| 107 |
- The value cannot be less than 60. |
| 108 |
required: false |
| 109 |
type: int |
| 110 |
normal_traffic_allowed: |
| 111 |
description: |
| 112 |
- Whether or not destination ports can send and receive "normal" traffic. |
| 113 |
- Setting this to false will make mirror ports be used solely for mirroring and not double as normal access ports. |
| 114 |
type: bool |
| 115 |
required: false |
| 116 |
sampling_rate: |
| 117 |
description: |
| 118 |
- Sampling rate of the session. |
| 119 |
- If its value is n, one of every n packets is mirrored. |
| 120 |
- Valid values are between 1 to 65535. |
| 121 |
type: int |
| 122 |
required: false |
| 123 |
source_vm_transmitted: |
| 124 |
description: |
| 125 |
- With this parameter it is possible, to add a NIC of a VM to a port mirroring session. |
| 126 |
suboptions: |
| 127 |
name: |
| 128 |
description: |
| 129 |
- Name of the VM. |
| 130 |
type: str |
| 131 |
nic_label: |
| 132 |
description: |
| 133 |
- Label of the network interface card to use. |
| 134 |
type: str |
| 135 |
type: dict |
| 136 |
source_vm_received: |
| 137 |
description: |
| 138 |
- With this parameter it is possible, to add a NIC of a VM to a port mirroring session. |
| 139 |
suboptions: |
| 140 |
name: |
| 141 |
description: |
| 142 |
- Name of the VM. |
| 143 |
type: str |
| 144 |
nic_label: |
| 145 |
description: |
| 146 |
- Label of the network interface card to use. |
| 147 |
type: str |
| 148 |
type: dict |
| 149 |
destination_vm: |
| 150 |
description: |
| 151 |
- With this parameter it is possible, to add a NIC of a VM to a port mirroring session. |
| 152 |
suboptions: |
| 153 |
name: |
| 154 |
description: |
| 155 |
- Name of the VM. |
| 156 |
type: str |
| 157 |
nic_label: |
| 158 |
description: |
| 159 |
- Label of the network interface card to use. |
| 160 |
type: str |
| 161 |
required: false |
| 162 |
type: dict |
| 163 |
extends_documentation_fragment: |
| 164 |
- vmware.vmware.base_options |
| 165 |
|
| 166 |
''' |
| 167 |
|
| 168 |
EXAMPLES = r''' |
| 169 |
- name: Create distributed mirroring session. |
| 170 |
community.vmware.vmware_vspan_session: |
| 171 |
hostname: '{{ vcenter_hostname }}' |
| 172 |
username: '{{ vcenter_username }}' |
| 173 |
password: '{{ vcenter_password }}' |
| 174 |
switch_name: dvSwitch |
| 175 |
state: present |
| 176 |
name: Basic Session |
| 177 |
enabled: true |
| 178 |
description: "Example description" |
| 179 |
source_port_transmitted: 817 |
| 180 |
source_port_received: 817 |
| 181 |
destination_port: 815 |
| 182 |
delegate_to: localhost |
| 183 |
|
| 184 |
- name: Create remote destination mirroring session. |
| 185 |
community.vmware.vmware_vspan_session: |
| 186 |
hostname: '{{ vcenter_hostname }}' |
| 187 |
username: '{{ vcenter_username }}' |
| 188 |
password: '{{ vcenter_password }}' |
| 189 |
switch_name: dvSwitch |
| 190 |
state: present |
| 191 |
name: Remote Session |
| 192 |
enabled: true |
| 193 |
description: "Example description" |
| 194 |
source_port_received: 105 |
| 195 |
destination_port: 815 |
| 196 |
session_type: "remoteMirrorDest" |
| 197 |
delegate_to: localhost |
| 198 |
|
| 199 |
- name: Delete remote destination mirroring session. |
| 200 |
community.vmware.vmware_vspan_session: |
| 201 |
hostname: '{{ vcenter_hostname }}' |
| 202 |
username: '{{ vcenter_username }}' |
| 203 |
password: '{{ vcenter_password }}' |
| 204 |
switch_name: dvSwitch |
| 205 |
state: absent |
| 206 |
name: Remote Session |
| 207 |
delegate_to: localhost |
| 208 |
''' |
| 209 |
|
| 210 |
RETURN = r'''# |
| 211 |
''' |
| 212 |
|
| 213 |
try: |
| 214 |
from pyVmomi import vim |
| 215 |
except ImportError: |
| 216 |
pass |
| 217 |
|
| 218 |
from ansible.module_utils.basic import AnsibleModule |
| 219 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 220 |
PyVmomi, find_dvs_by_name, |
| 221 |
find_vm_by_name, wait_for_task) |
| 222 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 223 |
|
| 224 |
|
| 225 |
class VMwareVspanSession(PyVmomi): |
| 226 |
def __init__(self, module): |
| 227 |
super(VMwareVspanSession, self).__init__(module) |
| 228 |
self.switch = module.params['switch'] |
| 229 |
self.name = module.params['name'] |
| 230 |
self.session_type = module.params['session_type'] |
| 231 |
self.enabled = module.params['enabled'] |
| 232 |
self.state = module.params['state'] |
| 233 |
self.description = module.params['description'] |
| 234 |
self.source_port_transmitted = module.params['source_port_transmitted'] |
| 235 |
self.source_port_received = module.params['source_port_received'] |
| 236 |
self.destination_port = module.params['destination_port'] |
| 237 |
self.encapsulation_vlan_id = module.params['encapsulation_vlan_id'] |
| 238 |
self.strip_original_vlan = module.params['strip_original_vlan'] |
| 239 |
self.mirrored_packet_length = module.params['mirrored_packet_length'] |
| 240 |
self.normal_traffic_allowed = module.params['normal_traffic_allowed'] |
| 241 |
self.sampling_rate = module.params['sampling_rate'] |
| 242 |
self.dv_switch = find_dvs_by_name(self.content, self.switch) |
| 243 |
if self.dv_switch is None: |
| 244 |
self.module.fail_json(msg="There is no dvSwitch with the name: {0:s}.".format(self.switch)) |
| 245 |
self.operation = None |
| 246 |
self.modified_ports = dict() |
| 247 |
self.deleted_session = None |
| 248 |
if module.params['source_vm_transmitted'] is not None: |
| 249 |
if (module.params['source_vm_transmitted']['name'] is None |
| 250 |
or module.params['source_vm_transmitted']['nic_label'] is None): |
| 251 |
self.module.fail_json(msg="Please provide both VM name and NIC Label") |
| 252 |
self.source_vm_transmitted_name = module.params['source_vm_transmitted']['name'] |
| 253 |
self.source_vm_transmitted_nic_label = module.params['source_vm_transmitted']['nic_label'] |
| 254 |
if module.params['source_vm_received'] is not None: |
| 255 |
if (module.params['source_vm_received']['name'] is None |
| 256 |
or module.params['source_vm_received']['nic_label'] is None): |
| 257 |
self.module.fail_json(msg="Please provide both VM name and NIC Label") |
| 258 |
self.source_vm_received_name = module.params['source_vm_received']['name'] |
| 259 |
self.source_vm_received_nic_label = module.params['source_vm_received']['nic_label'] |
| 260 |
if module.params['destination_vm'] is not None: |
| 261 |
if (module.params['destination_vm']['name'] is None |
| 262 |
or module.params['destination_vm']['nic_label'] is None): |
| 263 |
self.module.fail_json(msg="Please provide both VM name and NIC Label") |
| 264 |
self.destination_vm_name = module.params['destination_vm']['name'] |
| 265 |
self.destination_vm_nic_label = module.params['destination_vm']['nic_label'] |
| 266 |
|
| 267 |
def set_operation(self): |
| 268 |
"""Sets the operation according to state""" |
| 269 |
if self.state == 'absent': |
| 270 |
self.operation = 'remove' |
| 271 |
elif self.state == 'present' and self.find_session_by_name() is None: |
| 272 |
self.operation = 'add' |
| 273 |
else: |
| 274 |
self.operation = 'edit' |
| 275 |
|
| 276 |
def find_session_by_name(self): |
| 277 |
"""Finds a session by name |
| 278 |
Returns |
| 279 |
------- |
| 280 |
vim.dvs.VmwareDistributedVirtualSwitch.VspanSession |
| 281 |
The session if there was a session by the given name, else returns None |
| 282 |
""" |
| 283 |
for vspan_session in self.dv_switch.config.vspanSession: |
| 284 |
if vspan_session.name == self.name: |
| 285 |
return vspan_session |
| 286 |
return None |
| 287 |
|
| 288 |
def get_vm_port(self, vm_name, nic_label): |
| 289 |
"""Finds the port of the VM |
| 290 |
Returns |
| 291 |
------- |
| 292 |
str |
| 293 |
the port number as a string, or None if the NIC couldnt be found |
| 294 |
""" |
| 295 |
vm = find_vm_by_name(self.content, vm_name) |
| 296 |
if vm is None: |
| 297 |
self.module.fail_json(msg="There is no VM with the name: {0:s}.".format(vm_name)) |
| 298 |
for hardware in vm.config.hardware.device: |
| 299 |
if isinstance(hardware, vim.vm.device.VirtualVmxnet3): |
| 300 |
if hardware.deviceInfo.label == nic_label: |
| 301 |
return hardware.backing.port.portKey |
| 302 |
return None |
| 303 |
|
| 304 |
def set_port_for_vm(self): |
| 305 |
"""Sets the ports, to the VM's specified port.""" |
| 306 |
if hasattr(self, 'source_vm_transmitted_name') and hasattr(self, 'source_vm_transmitted_nic_label'): |
| 307 |
port = self.get_vm_port(self.source_vm_transmitted_name, self.source_vm_transmitted_nic_label) |
| 308 |
if port is not None: |
| 309 |
self.source_port_transmitted = port |
| 310 |
else: |
| 311 |
self.module.fail_json( |
| 312 |
msg="No port could be found for VM: {0:s} NIC: {1:s}".format(self.source_vm_transmitted_name, |
| 313 |
self.source_vm_transmitted_nic_label)) |
| 314 |
if hasattr(self, 'source_vm_received_name') and hasattr(self, 'source_vm_received_nic_label'): |
| 315 |
port = self.get_vm_port(self.source_vm_received_name, self.source_vm_received_nic_label) |
| 316 |
if port is not None: |
| 317 |
self.source_port_received = port |
| 318 |
else: |
| 319 |
self.module.fail_json( |
| 320 |
msg="No port could be found for VM: {0:s} NIC: {1:s}".format(self.source_vm_received_name, |
| 321 |
self.source_vm_received_nic_label)) |
| 322 |
if hasattr(self, 'destination_vm_name') and hasattr(self, 'destination_vm_nic_label'): |
| 323 |
port = self.get_vm_port(self.destination_vm_name, self.destination_vm_nic_label) |
| 324 |
if port is not None: |
| 325 |
self.destination_port = port |
| 326 |
else: |
| 327 |
self.module.fail_json( |
| 328 |
msg="No port could be found for VM: {0:s} NIC: {1:s}".format(self.destination_vm_name, |
| 329 |
self.destination_vm_nic_label)) |
| 330 |
|
| 331 |
def process_operation(self): |
| 332 |
"""Calls the create or delete function based on the operation""" |
| 333 |
self.set_operation() |
| 334 |
if self.operation == 'remove': |
| 335 |
results = self.remove_vspan_session() |
| 336 |
self.module.exit_json(**results) |
| 337 |
if self.operation == 'add': |
| 338 |
self.set_port_for_vm() |
| 339 |
results = self.add_vspan_session() |
| 340 |
self.module.exit_json(**results) |
| 341 |
if self.operation == 'edit': |
| 342 |
self.remove_vspan_session() |
| 343 |
self.set_port_for_vm() |
| 344 |
results = self.add_vspan_session() |
| 345 |
self.module.exit_json(**results) |
| 346 |
|
| 347 |
def set_port_security_promiscuous(self, ports, state): |
| 348 |
"""Set the given port to the given promiscuous state. |
| 349 |
Parameters |
| 350 |
---------- |
| 351 |
port : str[] |
| 352 |
PortKey |
| 353 |
state: bool |
| 354 |
State of the promiscuous mode, if true its allowed, else not. |
| 355 |
""" |
| 356 |
# Creating the new port policy |
| 357 |
port_spec = [] |
| 358 |
port_policy = vim.dvs.VmwareDistributedVirtualSwitch.MacManagementPolicy(allowPromiscuous=state) |
| 359 |
port_settings = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy(macManagementPolicy=port_policy) |
| 360 |
for port in ports: |
| 361 |
temp_port_spec = vim.dvs.DistributedVirtualPort.ConfigSpec( |
| 362 |
operation="edit", |
| 363 |
key=port, |
| 364 |
setting=port_settings |
| 365 |
) |
| 366 |
port_spec.append(temp_port_spec) |
| 367 |
|
| 368 |
task = self.dv_switch.ReconfigureDVPort_Task(port_spec) |
| 369 |
try: |
| 370 |
wait_for_task(task) |
| 371 |
except Exception: |
| 372 |
self.restore_original_state() |
| 373 |
self.module.fail_json(msg=task.info.error.msg) |
| 374 |
|
| 375 |
def turn_off_promiscuous(self): |
| 376 |
"""Disable all promiscuous mode ports, and give them back in a list. |
| 377 |
Returns |
| 378 |
------- |
| 379 |
list |
| 380 |
Contains every port, where promiscuous mode has been turned off |
| 381 |
""" |
| 382 |
# Ports that are in mirror sessions |
| 383 |
ports = [] |
| 384 |
ports_of_selected_session = [] |
| 385 |
for vspan_session in self.dv_switch.config.vspanSession: |
| 386 |
if vspan_session.sourcePortReceived is not None: |
| 387 |
session_ports = vspan_session.sourcePortReceived.portKey |
| 388 |
for port in session_ports: |
| 389 |
if vspan_session.name == self.name: |
| 390 |
ports_of_selected_session.append(port) |
| 391 |
elif not (port in ports): |
| 392 |
ports.append(port) |
| 393 |
if vspan_session.sourcePortTransmitted is not None: |
| 394 |
session_ports = vspan_session.sourcePortTransmitted.portKey |
| 395 |
for port in session_ports: |
| 396 |
if vspan_session.name == self.name: |
| 397 |
ports_of_selected_session.append(port) |
| 398 |
elif not (port in ports): |
| 399 |
ports.append(port) |
| 400 |
if vspan_session.destinationPort is not None: |
| 401 |
session_ports = vspan_session.destinationPort.portKey |
| 402 |
for port in session_ports: |
| 403 |
if vspan_session.name == self.name: |
| 404 |
ports_of_selected_session.append(port) |
| 405 |
elif not (port in ports): |
| 406 |
ports.append(port) |
| 407 |
promiscuous_ports = [] |
| 408 |
if ports: |
| 409 |
dv_ports = self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=ports)) |
| 410 |
# If a port is promiscuous set disable it, and add it to the array to enable it after the changes are made. |
| 411 |
for dv_port in dv_ports: |
| 412 |
if dv_port.config.setting.macManagementPolicy.allowPromiscuous: |
| 413 |
self.set_port_security_promiscuous([dv_port.key], False) |
| 414 |
self.modified_ports.update({dv_port.key: True}) |
| 415 |
promiscuous_ports.append(dv_port.key) |
| 416 |
if ports_of_selected_session: |
| 417 |
current_dv_ports = self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=ports_of_selected_session)) |
| 418 |
for dv_port in current_dv_ports: |
| 419 |
if dv_port.config.setting.macManagementPolicy.allowPromiscuous: |
| 420 |
self.set_port_security_promiscuous([dv_port.key], False) |
| 421 |
self.modified_ports.update({dv_port.key: True}) |
| 422 |
# Return the promiscuous ports array, to set them back after the config is finished. |
| 423 |
return promiscuous_ports |
| 424 |
|
| 425 |
def delete_mirroring_session(self, key): |
| 426 |
"""Deletes the mirroring session. |
| 427 |
Parameters |
| 428 |
---------- |
| 429 |
key : str |
| 430 |
Key of the Session |
| 431 |
""" |
| 432 |
session = vim.dvs.VmwareDistributedVirtualSwitch.VspanSession( |
| 433 |
key=key |
| 434 |
) |
| 435 |
config_version = self.dv_switch.config.configVersion |
| 436 |
s_spec = vim.dvs.VmwareDistributedVirtualSwitch.VspanConfigSpec(vspanSession=session, operation="remove") |
| 437 |
c_spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(vspanConfigSpec=[s_spec], configVersion=config_version) |
| 438 |
task = self.dv_switch.ReconfigureDvs_Task(c_spec) |
| 439 |
try: |
| 440 |
wait_for_task(task) |
| 441 |
except Exception: |
| 442 |
self.restore_original_state() |
| 443 |
self.module.fail_json(msg=task.info.error.msg) |
| 444 |
|
| 445 |
def restore_original_state(self): |
| 446 |
"""In case of failure restore, the changes we made.""" |
| 447 |
for port, state in self.modified_ports.items(): |
| 448 |
self.set_port_security_promiscuous([port], state) |
| 449 |
if self.deleted_session is not None: |
| 450 |
session = self.deleted_session |
| 451 |
config_version = self.dv_switch.config.configVersion |
| 452 |
s_spec = vim.dvs.VmwareDistributedVirtualSwitch.VspanConfigSpec(vspanSession=session, operation="add") |
| 453 |
c_spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(vspanConfigSpec=[s_spec], configVersion=config_version) |
| 454 |
# Revert the delete |
| 455 |
task = self.dv_switch.ReconfigureDvs_Task(c_spec) |
| 456 |
try: |
| 457 |
wait_for_task(task) |
| 458 |
except Exception: |
| 459 |
self.restore_original_state() |
| 460 |
self.module.fail_json(msg=task.info.error.msg) |
| 461 |
|
| 462 |
def remove_vspan_session(self): |
| 463 |
"""Calls the necessary functions to delete a VSpanSession.""" |
| 464 |
results = dict(changed=False, result="") |
| 465 |
mirror_session = self.find_session_by_name() |
| 466 |
if mirror_session is None: |
| 467 |
results['result'] = "There is no VSpanSession with the name: {0:s}.".format(self.name) |
| 468 |
return results |
| 469 |
promiscuous_ports = self.turn_off_promiscuous() |
| 470 |
session_key = mirror_session.key |
| 471 |
# Delete Mirroring Session |
| 472 |
self.delete_mirroring_session(session_key) |
| 473 |
# Session |
| 474 |
self.deleted_session = mirror_session |
| 475 |
# Set back the promiscuous ports |
| 476 |
if promiscuous_ports: |
| 477 |
self.set_port_security_promiscuous(promiscuous_ports, True) |
| 478 |
results['changed'] = True |
| 479 |
results['result'] = 'VSpan Session has been deleted' |
| 480 |
return results |
| 481 |
|
| 482 |
def check_if_session_name_is_free(self): |
| 483 |
"""Checks whether the name is used or not |
| 484 |
Returns |
| 485 |
------- |
| 486 |
bool |
| 487 |
True if the name is free and False if it is used. |
| 488 |
""" |
| 489 |
for vspan_session in self.dv_switch.config.vspanSession: |
| 490 |
if vspan_session.name == self.name: |
| 491 |
return False |
| 492 |
return True |
| 493 |
|
| 494 |
def check_source_port_transmitted(self, session): |
| 495 |
if self.source_port_transmitted is not None: |
| 496 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=str(self.source_port_transmitted)) |
| 497 |
if not self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=port.portKey)): |
| 498 |
self.module.fail_json(msg="Couldn't find port: {0:s}".format(self.source_port_transmitted)) |
| 499 |
session.sourcePortTransmitted = port |
| 500 |
|
| 501 |
def check_source_port_received(self, session): |
| 502 |
if not self.source_port_received: |
| 503 |
return |
| 504 |
|
| 505 |
if self.session_type == 'remoteMirrorDest': |
| 506 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(vlans=[int(self.source_port_received)]) |
| 507 |
if int(self.source_port_received) not in self.dv_switch.QueryUsedVlanIdInDvs(): |
| 508 |
self.module.fail_json(msg="Couldn't find vlan: {0:s}".format(self.source_port_received)) |
| 509 |
session.sourcePortReceived = port |
| 510 |
else: |
| 511 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=str(self.source_port_received)) |
| 512 |
if not self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=port.portKey)): |
| 513 |
self.module.fail_json(msg="Couldn't find port: {0:s}".format(self.source_port_received)) |
| 514 |
session.sourcePortReceived = port |
| 515 |
|
| 516 |
def check_destination_port(self, session): |
| 517 |
if not self.destination_port: |
| 518 |
return |
| 519 |
|
| 520 |
if self.session_type == 'encapsulatedRemoteMirrorSource': |
| 521 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(ipAddress=str(self.destination_port)) |
| 522 |
session.destinationPort = port |
| 523 |
if self.session_type == 'remoteMirrorSource': |
| 524 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(uplinkPortName=str(self.destination_port)) |
| 525 |
session.destinationPort = port |
| 526 |
if self.session_type == 'remoteMirrorDest': |
| 527 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=str(self.destination_port)) |
| 528 |
if not self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=port.portKey)): |
| 529 |
self.module.fail_json(msg="Couldn't find port: {0:s}".format(self.destination_port)) |
| 530 |
session.destinationPort = port |
| 531 |
if self.session_type == 'dvPortMirror': |
| 532 |
port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=str(self.destination_port)) |
| 533 |
if not self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=port.portKey)): |
| 534 |
self.module.fail_json(msg="Couldn't find port: {0:s}".format(self.destination_port)) |
| 535 |
session.destinationPort = port |
| 536 |
|
| 537 |
def check_self_properties(self, session): |
| 538 |
if self.description is not None: |
| 539 |
session.description = self.description |
| 540 |
if self.encapsulation_vlan_id is not None: |
| 541 |
session.encapsulationVlanId = self.encapsulation_vlan_id |
| 542 |
if self.strip_original_vlan is not None: |
| 543 |
session.stripOriginalVlan = self.strip_original_vlan |
| 544 |
if self.mirrored_packet_length is not None: |
| 545 |
session.mirroredPacketLength = self.mirrored_packet_length |
| 546 |
if self.normal_traffic_allowed is not None: |
| 547 |
session.normalTrafficAllowed = self.normal_traffic_allowed |
| 548 |
if self.sampling_rate is not None: |
| 549 |
session.samplingRate = self.sampling_rate |
| 550 |
|
| 551 |
def create_vspan_session(self): |
| 552 |
"""Builds up the session, adds the parameters that we specified, then creates it on the vSwitch""" |
| 553 |
|
| 554 |
session = vim.dvs.VmwareDistributedVirtualSwitch.VspanSession( |
| 555 |
name=self.name, |
| 556 |
enabled=True |
| 557 |
) |
| 558 |
if self.session_type is not None: |
| 559 |
session.sessionType = self.session_type |
| 560 |
if self.session_type == 'encapsulatedRemoteMirrorSource': |
| 561 |
self.check_source_port_received(session) |
| 562 |
self.check_source_port_transmitted(session) |
| 563 |
self.check_destination_port(session) |
| 564 |
if self.session_type == 'remoteMirrorSource': |
| 565 |
self.check_source_port_received(session) |
| 566 |
self.check_source_port_transmitted(session) |
| 567 |
self.check_destination_port(session) |
| 568 |
if self.session_type == 'remoteMirrorDest': |
| 569 |
self.check_source_port_received(session) |
| 570 |
self.check_destination_port(session) |
| 571 |
if self.session_type == 'dvPortMirror': |
| 572 |
self.check_source_port_received(session) |
| 573 |
self.check_source_port_transmitted(session) |
| 574 |
self.check_destination_port(session) |
| 575 |
|
| 576 |
self.check_self_properties(session) |
| 577 |
|
| 578 |
config_version = self.dv_switch.config.configVersion |
| 579 |
s_spec = vim.dvs.VmwareDistributedVirtualSwitch.VspanConfigSpec(vspanSession=session, operation="add") |
| 580 |
c_spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(vspanConfigSpec=[s_spec], configVersion=config_version) |
| 581 |
task = self.dv_switch.ReconfigureDvs_Task(c_spec) |
| 582 |
try: |
| 583 |
wait_for_task(task) |
| 584 |
except Exception: |
| 585 |
self.restore_original_state() |
| 586 |
self.module.fail_json(msg=task.info.error.msg) |
| 587 |
|
| 588 |
def add_vspan_session(self): |
| 589 |
"""Calls the necessary functions to create a VSpanSession""" |
| 590 |
results = dict(changed=False, result="") |
| 591 |
promiscous_ports = self.turn_off_promiscuous() |
| 592 |
if not self.check_if_session_name_is_free(): |
| 593 |
self.module.fail_json(msg="There is another VSpan Session with the name: {0:s}.".format(self.name)) |
| 594 |
# Locate the ports, we want to use |
| 595 |
dv_ports = None |
| 596 |
ports = [str(self.source_port_received), str(self.source_port_transmitted), str(self.destination_port)] |
| 597 |
if ports: |
| 598 |
dv_ports = self.dv_switch.FetchDVPorts(vim.dvs.PortCriteria(portKey=ports)) |
| 599 |
for dv_port in dv_ports: |
| 600 |
if dv_port.config.setting.macManagementPolicy.allowPromiscuous: |
| 601 |
self.set_port_security_promiscuous([dv_port.key], False) |
| 602 |
self.modified_ports.update({dv_port.key: True}) |
| 603 |
# Now we can create the VspanSession |
| 604 |
self.create_vspan_session() |
| 605 |
# Finally we can set the destination port to promiscuous mode |
| 606 |
if self.session_type == 'dvPortMirror' or self.session_type == 'remoteMirrorDest': |
| 607 |
self.set_port_security_promiscuous([str(self.destination_port)], True) |
| 608 |
# Set Back the Promiscuous ports |
| 609 |
if promiscous_ports: |
| 610 |
self.set_port_security_promiscuous(promiscous_ports, True) |
| 611 |
results['changed'] = True |
| 612 |
results['result'] = 'Mirroring session has been created.' |
| 613 |
return results |
| 614 |
|
| 615 |
|
| 616 |
def main(): |
| 617 |
argument_spec = base_argument_spec() |
| 618 |
argument_spec.update(dict( |
| 619 |
switch=dict(type='str', required=True, aliases=['switch_name']), |
| 620 |
name=dict(type='str', required=True), |
| 621 |
state=dict(type='str', required=True, choices=['present', 'absent']), |
| 622 |
session_type=dict( |
| 623 |
type='str', |
| 624 |
default='dvPortMirror', |
| 625 |
choices=[ |
| 626 |
'dvPortMirror', |
| 627 |
'encapsulatedRemoteMirrorSource', |
| 628 |
'remoteMirrorDest', |
| 629 |
'remoteMirrorSource' |
| 630 |
] |
| 631 |
), |
| 632 |
enabled=dict(type='bool', default=True), |
| 633 |
description=dict(type='str'), |
| 634 |
source_port_transmitted=dict(type='str'), |
| 635 |
source_port_received=dict(type='str'), |
| 636 |
destination_port=dict(type='str'), |
| 637 |
encapsulation_vlan_id=dict(type='int'), |
| 638 |
strip_original_vlan=dict(type='bool'), |
| 639 |
mirrored_packet_length=dict(type='int'), |
| 640 |
normal_traffic_allowed=dict(type='bool'), |
| 641 |
sampling_rate=dict(type='int'), |
| 642 |
source_vm_transmitted=dict( |
| 643 |
type='dict', |
| 644 |
options=dict( |
| 645 |
name=dict(type='str'), |
| 646 |
nic_label=dict(type='str') |
| 647 |
) |
| 648 |
), |
| 649 |
source_vm_received=dict( |
| 650 |
type='dict', |
| 651 |
options=dict( |
| 652 |
name=dict(type='str'), |
| 653 |
nic_label=dict(type='str') |
| 654 |
) |
| 655 |
), |
| 656 |
destination_vm=dict( |
| 657 |
type='dict', |
| 658 |
options=dict( |
| 659 |
name=dict(type='str'), |
| 660 |
nic_label=dict(type='str') |
| 661 |
) |
| 662 |
), |
| 663 |
)) |
| 664 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) |
| 665 |
session = VMwareVspanSession(module) |
| 666 |
session.process_operation() |
| 667 |
|
| 668 |
|
| 669 |
if __name__ == '__main__': |
| 670 |
main() |