| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Sabari Jaganathan (@sajagana) <[email protected]> |
| 5 |
|
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: ndo_fabric_span_session_source |
| 17 |
version_added: "2.11.0" |
| 18 |
short_description: Manage Fabric SPAN Sessions Source on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Switched Port Analyzer (SPAN) Sessions Source on Cisco Nexus Dashboard Orchestrator (NDO). |
| 21 |
- This module is only supported on ND v3.1 (NDO v4.4) and later. |
| 22 |
author: |
| 23 |
- Sabari Jaganathan (@sajagana) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a Fabric Monitoring Access Policy template. |
| 29 |
- This parameter or O(template_id) is required. |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- The ID of the template. |
| 34 |
- The template must be a Fabric Monitoring Access Policy template. |
| 35 |
- This parameter or O(template) is required. |
| 36 |
type: str |
| 37 |
span_session_name: |
| 38 |
description: |
| 39 |
- The name of the SPAN Session. |
| 40 |
- This parameter or O(span_session_uuid) is required. |
| 41 |
type: str |
| 42 |
span_session_uuid: |
| 43 |
description: |
| 44 |
- The UUID of the SPAN Session. |
| 45 |
- This parameter or O(span_session_name) is required. |
| 46 |
type: str |
| 47 |
name: |
| 48 |
description: |
| 49 |
- The name of the SPAN Session source. |
| 50 |
type: str |
| 51 |
direction: |
| 52 |
description: |
| 53 |
- The direction of the SPAN Session source. |
| 54 |
- Defaults to O(direction=incoming) when unset during creation. |
| 55 |
type: str |
| 56 |
choices: [ incoming, outgoing, both ] |
| 57 |
span_drop_packets: |
| 58 |
description: |
| 59 |
- The SPAN Drop Packets of the SPAN Session source. |
| 60 |
- SPAN Drop Packets are packets that get dropped when the SPAN destination port cannot handle the volume of mirrored traffic from the source ports. |
| 61 |
- Defaults to O(span_drop_packets=false) when unset during creation. |
| 62 |
- The O(filter_epg) and O(filter_l3out) are not configurable when O(span_drop_packets=true). |
| 63 |
type: bool |
| 64 |
filter_epg: |
| 65 |
description: |
| 66 |
- The Filter EPG of the SPAN Session source. |
| 67 |
- When the Filter EPG is specified in the configuration, the Filter L3Out will be removed. |
| 68 |
- This parameter and O(filter_l3out) are mutually exclusive. |
| 69 |
- Providing an empty dictionary O(filter_epg={}) will remove the filter l3out from the SPAN Session source. |
| 70 |
type: dict |
| 71 |
aliases: [ epg ] |
| 72 |
suboptions: |
| 73 |
uuid: |
| 74 |
description: |
| 75 |
- The UUID of the EPG used to configure the Filter EPG. |
| 76 |
- This parameter or O(filter_epg.reference) is required. |
| 77 |
type: str |
| 78 |
aliases: [ epg_uuid ] |
| 79 |
reference: |
| 80 |
description: |
| 81 |
- The EPG object detail used to configure the Filter EPG. |
| 82 |
- This parameter or O(filter_epg.uuid) is required. |
| 83 |
type: dict |
| 84 |
aliases: [ ref ] |
| 85 |
suboptions: |
| 86 |
name: |
| 87 |
description: |
| 88 |
- The name of the EPG. |
| 89 |
type: str |
| 90 |
required: true |
| 91 |
template: |
| 92 |
description: |
| 93 |
- The name of the template that contains the EPG. |
| 94 |
- This parameter or O(filter_epg.reference.template_id) is required. |
| 95 |
type: str |
| 96 |
template_id: |
| 97 |
description: |
| 98 |
- The ID of the template that contains the EPG. |
| 99 |
- This parameter or O(filter_epg.reference.template) is required. |
| 100 |
type: str |
| 101 |
schema_id: |
| 102 |
description: |
| 103 |
- The ID of the schema that contains the EPG. |
| 104 |
- This parameter or O(filter_epg.reference.schema) is required. |
| 105 |
type: str |
| 106 |
schema: |
| 107 |
description: |
| 108 |
- The name of the schema that contains the EPG. |
| 109 |
- This parameter or O(filter_epg.reference.schema_id) is required. |
| 110 |
type: str |
| 111 |
anp: |
| 112 |
description: |
| 113 |
- The name of the ANP that contains the EPG. |
| 114 |
- This parameter or O(filter_epg.reference.anp_uuid) is required. |
| 115 |
type: str |
| 116 |
anp_uuid: |
| 117 |
description: |
| 118 |
- The UUID of the ANP that contains the EPG. |
| 119 |
- This parameter or O(filter_epg.reference.anp) is required. |
| 120 |
type: str |
| 121 |
filter_l3out: |
| 122 |
description: |
| 123 |
- The Filter L3Out of the SPAN Session source. |
| 124 |
- When the Filter L3Out is specified in the configuration, the Filter EPG will be removed. |
| 125 |
- This parameter and O(filter_epg) are mutually exclusive. |
| 126 |
- The L3Out must be defined in either the L3Out template or directly within the APIC tenant. |
| 127 |
- The Filter L3Out for a SPAN Session source does not support using an L3Out from an application tenant template. |
| 128 |
- Providing an empty dictionary O(filter_l3out={}) will remove the filter l3out from the SPAN Session source. |
| 129 |
type: dict |
| 130 |
aliases: [ l3out ] |
| 131 |
suboptions: |
| 132 |
reference: |
| 133 |
description: |
| 134 |
- The Filter L3Out object detail used to configure the Filter L3Out. |
| 135 |
- This parameter or O(filter_l3out.uuid) is required. |
| 136 |
type: dict |
| 137 |
aliases: [ ref ] |
| 138 |
suboptions: |
| 139 |
name: |
| 140 |
description: |
| 141 |
- The name of the L3Out. |
| 142 |
required: true |
| 143 |
type: str |
| 144 |
tenant: |
| 145 |
description: |
| 146 |
- The name of the tenant. This parameter is used to associate the L3Out from APIC. |
| 147 |
- This parameter or O(filter_l3out.reference.template), or O(filter_l3out.reference.template_id) is required. |
| 148 |
type: str |
| 149 |
template: |
| 150 |
description: |
| 151 |
- The name of the L3Out template. |
| 152 |
- This parameter or O(filter_l3out.reference.template_id), or O(filter_l3out.reference.tenant) is required. |
| 153 |
type: str |
| 154 |
template_id: |
| 155 |
description: |
| 156 |
- The ID of the L3Out template. |
| 157 |
- This parameter or O(filter_l3out.reference.template), or O(filter_l3out.reference.tenant) is required. |
| 158 |
type: str |
| 159 |
uuid: |
| 160 |
description: |
| 161 |
- The UUID of the L3Out. |
| 162 |
- This parameter or O(filter_l3out.reference) is required. |
| 163 |
type: str |
| 164 |
aliases: [ l3out_uuid ] |
| 165 |
vlan_id: |
| 166 |
description: |
| 167 |
- The ID of the VLAN, which is associated with L3Out interface. |
| 168 |
- This parameter is required to configure the Filter L3Out. |
| 169 |
type: int |
| 170 |
access_paths: |
| 171 |
description: |
| 172 |
- The Access Path of the SPAN Session source. |
| 173 |
- Providing a new list of O(access_paths) will completely replace an existing one from the SPAN Session source. |
| 174 |
- Providing an empty list will remove the O(access_paths=[]) from the SPAN Session source. |
| 175 |
type: list |
| 176 |
elements: dict |
| 177 |
suboptions: |
| 178 |
access_path_type: |
| 179 |
description: |
| 180 |
- The type of the Access Path. |
| 181 |
type: str |
| 182 |
choices: [ port, port_channel, virtual_port_channel, vpc_component_pc ] |
| 183 |
aliases: [ type ] |
| 184 |
uuid: |
| 185 |
description: |
| 186 |
- The UUID of the 'Access Port' or 'Port Channel', or 'Virtual Port Channel' which is used to create the Access Path. |
| 187 |
type: str |
| 188 |
node: |
| 189 |
description: |
| 190 |
- The ID of the Node. This parameter is required when O(access_paths.access_path_type=port) or O(access_paths.access_path_type=vpc_component_pc). |
| 191 |
type: int |
| 192 |
interface: |
| 193 |
description: |
| 194 |
- The interface of the Node. This parameter is required to configure the O(access_paths.access_path_type=port). |
| 195 |
type: str |
| 196 |
name: |
| 197 |
description: |
| 198 |
- The name of the 'Port Channel' or 'Virtual Port Channel' which is used to create the Access Path. |
| 199 |
type: str |
| 200 |
template: |
| 201 |
description: |
| 202 |
- The name of the Fabric Resource Policy template. |
| 203 |
- This parameter or O(access_paths.template_id) is required when O(access_paths.access_path_type=port_channel), |
| 204 |
O(access_paths.access_path_type=virtual_port_channel), or O(access_paths.access_path_type=vpc_component_pc). |
| 205 |
type: str |
| 206 |
template_id: |
| 207 |
description: |
| 208 |
- The ID of the Fabric Resource Policy template. |
| 209 |
- This parameter or O(access_paths.template) is required when O(access_paths.access_path_type=port_channel), |
| 210 |
O(access_paths.access_path_type=virtual_port_channel), or O(access_paths.access_path_type=vpc_component_pc). |
| 211 |
type: str |
| 212 |
state: |
| 213 |
description: |
| 214 |
- Use C(absent) for removing. |
| 215 |
- Use C(query) for listing an object or multiple objects. |
| 216 |
- Use C(present) for creating or updating. |
| 217 |
type: str |
| 218 |
choices: [ absent, query, present ] |
| 219 |
default: query |
| 220 |
notes: |
| 221 |
- The O(template) must exist before using this module in your playbook. |
| 222 |
Use M(cisco.mso.ndo_template) to create the Fabric Monitoring Access Policy template. |
| 223 |
- The O(span_session_name) must exist before using this module in your playbook. |
| 224 |
Use M(cisco.mso.ndo_fabric_span_session) to create the Fabric SPAN Session. |
| 225 |
- The O(filter_epg.reference) must exist before using it with this module in your playbook. |
| 226 |
Use M(cisco.mso.mso_schema_template_anp_epg) to create the EPG. |
| 227 |
- The O(filter_l3out.reference) must exist before using it with this module in your playbook. |
| 228 |
Use M(cisco.mso.ndo_l3out_template) to create the L3Out. |
| 229 |
- The O(access_paths.name) must exist before using it with this module in your playbook. |
| 230 |
Use M(cisco.mso.ndo_port_channel_interface) to create the Fabric resource port channel interface. |
| 231 |
- The O(access_paths.name) must exist before using it with this module in your playbook. |
| 232 |
Use M(cisco.mso.ndo_virtual_port_channel_interface) to create the Fabric resource virtual port channel interface. |
| 233 |
seealso: |
| 234 |
- module: cisco.mso.ndo_template |
| 235 |
- module: cisco.mso.ndo_fabric_span_session |
| 236 |
- module: cisco.mso.mso_schema_template_anp_epg |
| 237 |
- module: cisco.mso.ndo_port_channel_interface |
| 238 |
- module: cisco.mso.ndo_virtual_port_channel_interface |
| 239 |
extends_documentation_fragment: cisco.mso.modules |
| 240 |
""" |
| 241 |
|
| 242 |
EXAMPLES = r""" |
| 243 |
- name: Create the SPAN Session source with access paths |
| 244 |
cisco.mso.ndo_fabric_span_session_source: |
| 245 |
host: mso_host |
| 246 |
username: admin |
| 247 |
password: SomeSecretPassword |
| 248 |
template: ansible_test_mon_pol |
| 249 |
span_session_name: ansible_test_span_session |
| 250 |
name: ansible_test_source_2 |
| 251 |
direction: outgoing |
| 252 |
span_drop_packets: true |
| 253 |
access_paths: |
| 254 |
- access_path_type: port |
| 255 |
node: 101 |
| 256 |
interface: eth1/6 |
| 257 |
- access_path_type: port_channel |
| 258 |
name: ansible_test_pc1 |
| 259 |
template: ansible_test_fabric_resource |
| 260 |
- access_path_type: virtual_port_channel |
| 261 |
name: ansible_test_vpc1 |
| 262 |
template: ansible_test_fabric_resource |
| 263 |
- access_path_type: vpc_component_pc |
| 264 |
name: ansible_test_vpc1 |
| 265 |
template: ansible_test_fabric_resource |
| 266 |
node: 101 |
| 267 |
state: present |
| 268 |
register: add_ansible_test_source_2 |
| 269 |
|
| 270 |
- name: Create the SPAN Session source with access path and filter EPG |
| 271 |
cisco.mso.ndo_fabric_span_session_source: |
| 272 |
host: mso_host |
| 273 |
username: admin |
| 274 |
password: SomeSecretPassword |
| 275 |
template: ansible_test_mon_pol |
| 276 |
span_session_name: ansible_test_span_session |
| 277 |
name: ansible_test_source_3 |
| 278 |
direction: outgoing |
| 279 |
access_paths: |
| 280 |
- access_path_type: port |
| 281 |
node: 101 |
| 282 |
interface: eth1/6 |
| 283 |
filter_epg: |
| 284 |
reference: |
| 285 |
schema: ansible_test_schema |
| 286 |
template: template1 |
| 287 |
anp: ansible_test_anp |
| 288 |
name: ansible_test_epg1 |
| 289 |
state: present |
| 290 |
|
| 291 |
- name: Create the SPAN Session source with access path and filter L3Out |
| 292 |
cisco.mso.ndo_fabric_span_session_source: |
| 293 |
host: mso_host |
| 294 |
username: admin |
| 295 |
password: SomeSecretPassword |
| 296 |
template: ansible_test_mon_pol |
| 297 |
span_session_name: ansible_test_span_session |
| 298 |
name: ansible_test_source_4 |
| 299 |
direction: outgoing |
| 300 |
access_paths: |
| 301 |
- access_path_type: port |
| 302 |
node: 101 |
| 303 |
interface: eth1/1 |
| 304 |
filter_l3out: |
| 305 |
reference: |
| 306 |
name: ansible_test_l3out |
| 307 |
template: ansible_test_l3out_template |
| 308 |
vlan_id: 41 |
| 309 |
state: present |
| 310 |
|
| 311 |
- name: Create the SPAN Session source with access paths UUID |
| 312 |
cisco.mso.ndo_fabric_span_session_source: |
| 313 |
host: mso_host |
| 314 |
username: admin |
| 315 |
password: SomeSecretPassword |
| 316 |
template_id: "{{ add_span_session.current.templateId }}" |
| 317 |
span_session_uuid: "{{ add_span_session.current.uuid }}" |
| 318 |
name: ansible_test_source_1 |
| 319 |
access_paths: |
| 320 |
- access_path_type: port |
| 321 |
node: 101 |
| 322 |
interface: eth1/1 |
| 323 |
- access_path_type: port_channel |
| 324 |
uuid: "{{ add_fabric_pc_1.current.uuid }}" |
| 325 |
- access_path_type: virtual_port_channel |
| 326 |
uuid: "{{ add_fabric_vpc1.current.uuid }}" |
| 327 |
- access_path_type: vpc_component_pc |
| 328 |
uuid: "{{ add_fabric_vpc1.current.uuid }}" |
| 329 |
node: 101 |
| 330 |
state: present |
| 331 |
|
| 332 |
- name: Update the SPAN Session source Filter EPG using UUID |
| 333 |
cisco.mso.ndo_fabric_span_session_source: |
| 334 |
host: mso_host |
| 335 |
username: admin |
| 336 |
password: SomeSecretPassword |
| 337 |
template: ansible_test_mon_pol |
| 338 |
span_session_name: ansible_test_span_session |
| 339 |
name: ansible_test_source_1 |
| 340 |
direction: outgoing |
| 341 |
filter_epg: |
| 342 |
uuid: "{{ add_epg.current.epg }}" |
| 343 |
state: present |
| 344 |
|
| 345 |
- name: Update the SPAN Session source Filter L3Out using UUID |
| 346 |
cisco.mso.ndo_fabric_span_session_source: |
| 347 |
host: mso_host |
| 348 |
username: admin |
| 349 |
password: SomeSecretPassword |
| 350 |
template: ansible_test_mon_pol |
| 351 |
span_session_name: ansible_test_span_session |
| 352 |
name: ansible_test_source_1 |
| 353 |
direction: outgoing |
| 354 |
access_paths: |
| 355 |
- access_path_type: port_channel |
| 356 |
name: ansible_test_pc1 |
| 357 |
template: ansible_test_fabric_resource |
| 358 |
filter_l3out: |
| 359 |
uuid: "{{ add_l3out.current.uuid }}" |
| 360 |
vlan_id: 42 |
| 361 |
state: present |
| 362 |
|
| 363 |
- name: Query a specific SPAN Session source |
| 364 |
cisco.mso.ndo_fabric_span_session_source: |
| 365 |
host: mso_host |
| 366 |
username: admin |
| 367 |
password: SomeSecretPassword |
| 368 |
template: ansible_test_mon_pol |
| 369 |
span_session_name: ansible_test_span_session |
| 370 |
name: ansible_test_source_1 |
| 371 |
state: query |
| 372 |
register: query_one |
| 373 |
|
| 374 |
- name: Query all SPAN Session sources |
| 375 |
cisco.mso.ndo_fabric_span_session_source: |
| 376 |
host: mso_host |
| 377 |
username: admin |
| 378 |
password: SomeSecretPassword |
| 379 |
template: ansible_test_mon_pol |
| 380 |
span_session_name: ansible_test_span_session |
| 381 |
state: query |
| 382 |
register: query_all |
| 383 |
|
| 384 |
- name: Delete the SPAN Session source |
| 385 |
cisco.mso.ndo_fabric_span_session_source: |
| 386 |
host: mso_host |
| 387 |
username: admin |
| 388 |
password: SomeSecretPassword |
| 389 |
template: ansible_test_mon_pol |
| 390 |
span_session_name: ansible_test_span_session |
| 391 |
name: ansible_test_source_1 |
| 392 |
state: absent |
| 393 |
""" |
| 394 |
|
| 395 |
RETURN = r""" |
| 396 |
""" |
| 397 |
|
| 398 |
from ansible.module_utils.basic import AnsibleModule |
| 399 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, epg_object_reference_spec |
| 400 |
from ansible_collections.cisco.mso.plugins.module_utils.schemas import MSOSchemas |
| 401 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 402 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 403 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, check_if_all_elements_are_none |
| 404 |
import copy |
| 405 |
|
| 406 |
|
| 407 |
def main(): |
| 408 |
argument_spec = mso_argument_spec() |
| 409 |
argument_spec.update( |
| 410 |
template=dict(type="str"), |
| 411 |
template_id=dict(type="str"), |
| 412 |
span_session_name=dict(type="str"), |
| 413 |
span_session_uuid=dict(type="str"), |
| 414 |
name=dict(type="str"), |
| 415 |
direction=dict(type="str", choices=["incoming", "outgoing", "both"]), |
| 416 |
span_drop_packets=dict(type="bool"), |
| 417 |
filter_epg=dict( |
| 418 |
type="dict", |
| 419 |
aliases=["epg"], |
| 420 |
options=dict( |
| 421 |
uuid=dict(type="str", aliases=["epg_uuid"]), |
| 422 |
reference=epg_object_reference_spec(aliases=["ref"]), |
| 423 |
), |
| 424 |
mutually_exclusive=[("reference", "uuid")], |
| 425 |
), |
| 426 |
filter_l3out=dict( |
| 427 |
type="dict", |
| 428 |
aliases=["l3out"], |
| 429 |
options=dict( |
| 430 |
reference=dict( |
| 431 |
type="dict", |
| 432 |
options=dict( |
| 433 |
name=dict(type="str", required=True), |
| 434 |
tenant=dict(type="str"), |
| 435 |
template=dict(type="str"), |
| 436 |
template_id=dict(type="str"), |
| 437 |
), |
| 438 |
mutually_exclusive=[("tenant", "template", "template_id")], |
| 439 |
required_one_of=[["tenant", "template", "template_id"]], |
| 440 |
aliases=["ref"], |
| 441 |
), |
| 442 |
uuid=dict(type="str", aliases=["l3out_uuid"]), |
| 443 |
vlan_id=dict(type="int"), |
| 444 |
), |
| 445 |
mutually_exclusive=[("reference", "uuid")], |
| 446 |
required_by={ |
| 447 |
"reference": "vlan_id", |
| 448 |
"uuid": "vlan_id", |
| 449 |
}, |
| 450 |
), |
| 451 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 452 |
access_paths=dict( |
| 453 |
type="list", |
| 454 |
elements="dict", |
| 455 |
options=dict( |
| 456 |
uuid=dict(type="str"), |
| 457 |
node=dict(type="int"), |
| 458 |
interface=dict(type="str"), |
| 459 |
name=dict(type="str"), |
| 460 |
template=dict(type="str"), |
| 461 |
template_id=dict(type="str"), |
| 462 |
access_path_type=dict(type="str", choices=["port", "port_channel", "virtual_port_channel", "vpc_component_pc"], aliases=["type"]), |
| 463 |
), |
| 464 |
mutually_exclusive=[("uuid", "interface", "name"), ("template", "template_id")], |
| 465 |
), |
| 466 |
) |
| 467 |
|
| 468 |
module = AnsibleModule( |
| 469 |
argument_spec=argument_spec, |
| 470 |
supports_check_mode=True, |
| 471 |
mutually_exclusive=[ |
| 472 |
("template", "template_id"), |
| 473 |
("span_session_name", "span_session_uuid"), |
| 474 |
("filter_epg", "filter_l3out"), |
| 475 |
], |
| 476 |
required_if=[ |
| 477 |
["state", "absent", ["name"]], |
| 478 |
["state", "present", ["name"]], |
| 479 |
], |
| 480 |
required_one_of=[ |
| 481 |
["template", "template_id"], |
| 482 |
["span_session_name", "span_session_uuid"], |
| 483 |
], |
| 484 |
) |
| 485 |
|
| 486 |
mso = MSOModule(module) |
| 487 |
mso_schemas = MSOSchemas(mso) |
| 488 |
mso_templates = MSOTemplates(mso) |
| 489 |
|
| 490 |
template_name = module.params.get("template") |
| 491 |
template_id = module.params.get("template_id") |
| 492 |
span_session_name = module.params.get("span_session_name") |
| 493 |
span_session_uuid = module.params.get("span_session_uuid") |
| 494 |
name = module.params.get("name") |
| 495 |
direction = module.params.get("direction") |
| 496 |
span_drop_packets = module.params.get("span_drop_packets") |
| 497 |
filter_epg = module.params.get("filter_epg") |
| 498 |
filter_l3out = module.params.get("filter_l3out") |
| 499 |
access_paths = module.params.get("access_paths") |
| 500 |
state = module.params.get("state") |
| 501 |
|
| 502 |
if filter_epg is not None and check_if_all_elements_are_none(filter_epg.values()): |
| 503 |
filter_epg = {} |
| 504 |
|
| 505 |
if filter_l3out is not None and check_if_all_elements_are_none(filter_l3out.values()): |
| 506 |
filter_l3out = {} |
| 507 |
|
| 508 |
errors = validate_access_paths(access_paths) |
| 509 |
if errors: |
| 510 |
mso.fail_json(msg=", ".join(errors)) |
| 511 |
|
| 512 |
mso_template = MSOTemplate(mso, "monitoring_tenant", template_name, template_id) |
| 513 |
mso_template.validate_template("monitoring") |
| 514 |
site_id = mso_template.template.get("monitoringTemplate").get("sites")[0].get("siteId") |
| 515 |
|
| 516 |
fabric_span_session = mso_template.get_fabric_span_session(span_session_uuid, span_session_name, fail_module=True) |
| 517 |
match = mso_template.get_fabric_span_session_source(name, fabric_span_session.details.get("sourceGroup", {}).get("sources", [])) |
| 518 |
|
| 519 |
if match and name: |
| 520 |
mso.existing = mso.previous = copy.deepcopy( |
| 521 |
set_fabric_span_session_source_object_details(mso_template, site_id, match.details) |
| 522 |
) # Query a specific object |
| 523 |
elif match: |
| 524 |
mso.existing = [set_fabric_span_session_source_object_details(mso_template, site_id, obj) for obj in match] # Query all objects |
| 525 |
|
| 526 |
if state != "query": |
| 527 |
source_path = "/monitoringTemplate/template/spanSessions/{0}/sourceGroup/sources/{1}".format(fabric_span_session.index, match.index if match else "-") |
| 528 |
|
| 529 |
ops = [] |
| 530 |
|
| 531 |
if state == "present": |
| 532 |
mso_values = dict( |
| 533 |
name=name, |
| 534 |
direction=direction, |
| 535 |
spanDropPackets=span_drop_packets, |
| 536 |
) |
| 537 |
|
| 538 |
if filter_epg and (filter_epg.get("reference") or filter_epg.get("uuid")): |
| 539 |
mso_values["epg"] = mso_schemas.get_epg_uuid(filter_epg.get("reference"), filter_epg.get("uuid")) |
| 540 |
|
| 541 |
if filter_l3out and (filter_l3out.get("reference") or filter_l3out.get("uuid")): |
| 542 |
mso_values["l3out"] = dict( |
| 543 |
encapType="vlan", |
| 544 |
encapValue=filter_l3out.get("vlan_id"), |
| 545 |
) |
| 546 |
if filter_l3out.get("uuid"): |
| 547 |
mso_values["l3out"]["ref"] = filter_l3out.get("uuid") |
| 548 |
elif filter_l3out.get("reference") and filter_l3out.get("reference").get("tenant"): |
| 549 |
mso_values["l3out"]["dn"] = "uni/tn-{0}/out-{1}".format(filter_l3out.get("reference").get("tenant"), filter_l3out.get("reference").get("name")) |
| 550 |
elif filter_l3out.get("reference") and (filter_l3out.get("reference").get("template") or filter_l3out.get("reference").get("template_id")): |
| 551 |
l3out_template = mso_templates.get_template( |
| 552 |
"l3out", filter_l3out.get("reference").get("template"), filter_l3out.get("reference").get("template_id") |
| 553 |
) |
| 554 |
l3out_match = l3out_template.get_l3out_object(uuid=filter_l3out.get("uuid"), name=filter_l3out.get("reference").get("name"), fail_module=True) |
| 555 |
if l3out_match: |
| 556 |
mso_values["l3out"]["ref"] = l3out_match.details.get("uuid") |
| 557 |
|
| 558 |
if access_paths: |
| 559 |
mso_values["accessPaths"] = update_access_paths(mso, site_id, access_paths, mso_templates) |
| 560 |
|
| 561 |
if match: |
| 562 |
mso_remove_values = [] |
| 563 |
proposed_payload = copy.deepcopy(match.details) |
| 564 |
proposed_payload.update({"name": mso_values["name"], "direction": mso_values["direction"], "spanDropPackets": mso_values["spanDropPackets"]}) |
| 565 |
|
| 566 |
if filter_epg == {} and match.details.get("epg"): |
| 567 |
mso_remove_values.append("epg") |
| 568 |
proposed_payload["epg"] = "" |
| 569 |
match.details.pop("epgName") |
| 570 |
match.details.pop("epgTemplateName") |
| 571 |
match.details.pop("epgTemplateId") |
| 572 |
match.details.pop("epgSchemaName") |
| 573 |
match.details.pop("epgSchemaId") |
| 574 |
elif mso_values.get("epg"): |
| 575 |
# When the filter EPG is specified in the configuration, the filter L3Out will be removed. |
| 576 |
# L3Out and EPG cannot be configured simultaneously. |
| 577 |
if match.details.get("l3out"): |
| 578 |
mso_remove_values.append("l3out") |
| 579 |
proposed_payload["epg"] = mso_values["epg"] |
| 580 |
|
| 581 |
if filter_l3out == {} and match.details.get("l3out"): |
| 582 |
mso_remove_values.append("l3out") |
| 583 |
proposed_payload["l3out"] = {} |
| 584 |
match.details["l3out"] = {} |
| 585 |
elif mso_values.get("l3out"): |
| 586 |
# When the filter L3Out is specified in the configuration, the filter EPG will be removed. |
| 587 |
# L3Out and EPG cannot be configured simultaneously. |
| 588 |
if match.details.get("epg"): |
| 589 |
mso_remove_values.append("epg") |
| 590 |
proposed_payload["l3out"] = mso_values["l3out"] |
| 591 |
|
| 592 |
if access_paths == [] and match.details.get("accessPaths"): |
| 593 |
mso_remove_values.append("accessPaths") |
| 594 |
proposed_payload["accessPaths"] = [] |
| 595 |
match.details["accessPaths"] = [] |
| 596 |
elif mso_values.get("accessPaths"): |
| 597 |
proposed_payload["accessPaths"] = mso_values["accessPaths"] |
| 598 |
|
| 599 |
mso.sanitize(proposed_payload, collate=True) |
| 600 |
append_update_ops_data(ops, match.details, source_path, mso_values, mso_remove_values) |
| 601 |
else: |
| 602 |
mso.sanitize(mso_values) |
| 603 |
ops.append(dict(op="add", path=source_path, value=mso.sent)) |
| 604 |
|
| 605 |
elif state == "absent" and match: |
| 606 |
ops.append(dict(op="remove", path=source_path)) |
| 607 |
|
| 608 |
if not module.check_mode and ops: |
| 609 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 610 |
fabric_span_session = mso_template.get_fabric_span_session(span_session_uuid, span_session_name, fail_module=True) |
| 611 |
match = mso_template.get_fabric_span_session_source(name, fabric_span_session.details.get("sourceGroup", {}).get("sources", [])) |
| 612 |
if match: |
| 613 |
mso.existing = set_fabric_span_session_source_object_details(mso_template, site_id, match.details) # When the state is present |
| 614 |
else: |
| 615 |
mso.existing = {} # When the state is absent |
| 616 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 617 |
mso.existing = set_fabric_span_session_source_object_details(mso_template, site_id, mso.proposed) if state == "present" else {} |
| 618 |
|
| 619 |
mso.exit_json() |
| 620 |
|
| 621 |
|
| 622 |
def validate_access_paths(access_paths): |
| 623 |
if access_paths: |
| 624 |
errors = [] |
| 625 |
for index, path in enumerate(access_paths): |
| 626 |
access_path_type = path.get("access_path_type") |
| 627 |
uuid = path.get("uuid") |
| 628 |
node = path.get("node") |
| 629 |
interface = path.get("interface") |
| 630 |
name = path.get("name") |
| 631 |
template = path.get("template") |
| 632 |
template_id = path.get("template_id") |
| 633 |
|
| 634 |
# Validate based on access_path_type |
| 635 |
if access_path_type == "port": |
| 636 |
if not uuid and not (node and interface): |
| 637 |
errors.append( |
| 638 |
"Access path {0}: when the access_path_type='port', either 'uuid' or both 'node' and 'interface' must be provided.".format(index + 1) |
| 639 |
) |
| 640 |
|
| 641 |
elif access_path_type == "port_channel": |
| 642 |
if not uuid and not (name and template) and not (name and template_id): |
| 643 |
errors.append( |
| 644 |
( |
| 645 |
"Access path {0}: when the access_path_type='port_channel', either 'uuid'," |
| 646 |
+ " or both 'name' and 'template', or both 'name' and 'template_id' must be provided." |
| 647 |
).format(index + 1) |
| 648 |
) |
| 649 |
|
| 650 |
elif access_path_type == "virtual_port_channel": |
| 651 |
if not uuid and not (name and template) and not (name and template_id): |
| 652 |
errors.append( |
| 653 |
( |
| 654 |
"Access path {0}: when the access_path_type 'virtual_port_channel', either 'uuid'," |
| 655 |
+ " or both 'name' and 'template', or both 'name' and 'template_id' must be provided." |
| 656 |
).format(index + 1) |
| 657 |
) |
| 658 |
|
| 659 |
elif access_path_type == "vpc_component_pc": |
| 660 |
if not (uuid and node) and not (name and template and node) and not (name and template_id and node): |
| 661 |
errors.append( |
| 662 |
( |
| 663 |
"Access path {0}: when the access_path_type 'vpc_component_pc', either both 'uuid' and 'node'," |
| 664 |
+ " or all of 'name', 'template', and 'node'," |
| 665 |
+ " or all of 'name', 'template_id', and 'node' must be provided." |
| 666 |
).format(index + 1) |
| 667 |
) |
| 668 |
return errors |
| 669 |
|
| 670 |
|
| 671 |
def get_access_path_payload(mso_templates, access_path_config, access_path_type, resource_type): |
| 672 |
uuid = access_path_config.get("uuid") |
| 673 |
if not uuid: |
| 674 |
fabric_template = mso_templates.get_template("fabric_resource", access_path_config.get("template"), access_path_config.get("template_id")) |
| 675 |
uuid = fabric_template.get_template_policy_uuid("fabric_resource", access_path_config.get("name"), resource_type) |
| 676 |
|
| 677 |
return ( |
| 678 |
dict(vpcComponentPc=[dict(vpc=uuid, node=str(access_path_config.get("node")))]) if access_path_type == "vpc_component_pc" else {resource_type: [uuid]} |
| 679 |
) |
| 680 |
|
| 681 |
|
| 682 |
def update_access_paths(mso, site_id, access_paths, mso_templates): |
| 683 |
updated_paths = [] |
| 684 |
for access_path in access_paths: |
| 685 |
if access_path.get("access_path_type") == "port": |
| 686 |
port_uuid = access_path.get("uuid") |
| 687 |
if not port_uuid: |
| 688 |
port_uuid = mso.get_site_interface_details( |
| 689 |
site_id=site_id, |
| 690 |
uuid=None, |
| 691 |
node=access_path.get("node"), |
| 692 |
port=access_path.get("interface"), |
| 693 |
).get("uuid") |
| 694 |
updated_paths.append(dict(accessInterfaces=[port_uuid])) |
| 695 |
|
| 696 |
elif access_path.get("access_path_type") == "port_channel": |
| 697 |
updated_paths.append(get_access_path_payload(mso_templates, access_path, "port_channel", "portChannels")) |
| 698 |
|
| 699 |
elif access_path.get("access_path_type") == "virtual_port_channel": |
| 700 |
updated_paths.append(get_access_path_payload(mso_templates, access_path, "virtual_port_channel", "virtualPortChannels")) |
| 701 |
|
| 702 |
elif access_path.get("access_path_type") == "vpc_component_pc": |
| 703 |
updated_paths.append(get_access_path_payload(mso_templates, access_path, "vpc_component_pc", "virtualPortChannels")) |
| 704 |
|
| 705 |
if len(set([str(path) for path in updated_paths])) == len(updated_paths): |
| 706 |
return updated_paths |
| 707 |
else: |
| 708 |
mso.fail_json(msg="Remove duplicate entries from the access_paths: {0}".format(access_paths)) |
| 709 |
|
| 710 |
|
| 711 |
def set_fabric_span_session_source_object_details(mso_template, site_id, source): |
| 712 |
if source: |
| 713 |
for access_path in source.get("accessPaths", []): # Adding the object reference name to use the update_config_with_template_and_references function |
| 714 |
if access_path.get("accessInterfaces") and isinstance(access_path.get("accessInterfaces")[0], str): |
| 715 |
access_path.get("accessInterfaces")[0] = mso_template.mso.get_site_interface_details(site_id, access_path.get("accessInterfaces")[0]) |
| 716 |
elif access_path.get("portChannels") and isinstance(access_path.get("portChannels")[0], str): |
| 717 |
access_path.get("portChannels")[0] = dict(portChannel=access_path.get("portChannels")[0]) |
| 718 |
elif access_path.get("virtualPortChannels") and isinstance(access_path.get("virtualPortChannels")[0], str): |
| 719 |
access_path.get("virtualPortChannels")[0] = dict(virtualPortChannel=access_path.get("virtualPortChannels")[0]) |
| 720 |
|
| 721 |
reference_details = { |
| 722 |
"filterEPG": { |
| 723 |
"name": "epgName", |
| 724 |
"reference": "epg", |
| 725 |
"type": "epg", |
| 726 |
"template": "epgTemplateName", |
| 727 |
"templateId": "epgTemplateId", |
| 728 |
"schema": "epgSchemaName", |
| 729 |
"schemaId": "epgSchemaId", |
| 730 |
}, |
| 731 |
"portChannels": { |
| 732 |
"name": "portChannelName", |
| 733 |
"reference": "portChannel", |
| 734 |
"type": "portChannel", |
| 735 |
"template": "portChannelTemplateName", |
| 736 |
"templateId": "portChannelTemplateId", |
| 737 |
}, |
| 738 |
"virtualPortChannels": { |
| 739 |
"name": "virtualPortChannelName", |
| 740 |
"reference": "virtualPortChannel", |
| 741 |
"type": "virtualPortChannel", |
| 742 |
"template": "virtualPortChannelTemplateName", |
| 743 |
"templateId": "virtualPortChannelTemplateId", |
| 744 |
}, |
| 745 |
"vpcComponentPc": { |
| 746 |
"name": "vpcComponentPcName", |
| 747 |
"reference": "vpc", |
| 748 |
"type": "virtualPortChannel", |
| 749 |
"template": "vpcComponentPcTemplateName", |
| 750 |
"templateId": "vpcComponentPcTemplateId", |
| 751 |
}, |
| 752 |
"l3out": { |
| 753 |
"name": "l3outName", |
| 754 |
"reference": "ref", |
| 755 |
"type": "l3out", |
| 756 |
"template": "l3outTemplateName", |
| 757 |
"templateId": "l3outTemplateId", |
| 758 |
}, |
| 759 |
} |
| 760 |
|
| 761 |
mso_template.update_config_with_template_and_references(source, reference_details, True) |
| 762 |
return source |
| 763 |
|
| 764 |
|
| 765 |
if __name__ == "__main__": |
| 766 |
main() |