Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_fabric_span_session.py

raw
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
17 version_added: "2.11.0"
18 short_description: Manage Fabric SPAN Sessions on Cisco Nexus Dashboard Orchestrator (NDO).
19 description:
20 - Manage Switched Port Analyzer (SPAN) Sessions 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 name:
38 description:
39 - The name of the SPAN Session.
40 type: str
41 aliases: [ span_session ]
42 uuid:
43 description:
44 - The UUID of the SPAN Session.
45 - This parameter is required when the O(name) needs to be updated.
46 type: str
47 aliases: [ span_session_uuid ]
48 description:
49 description:
50 - The description of the SPAN Session.
51 type: str
52 admin_state:
53 description:
54 - The administrative state of the SPAN Session.
55 - Defaults to C(enabled) when unset during creation.
56 choices: [ enabled, disabled ]
57 type: str
58 mtu:
59 description:
60 - The MTU truncation size for the SPAN packets.
61 - The value must be in the range 64 - 9216.
62 - Defaults to 1518 when unset during creation.
63 type: int
64 destination_epg:
65 description:
66 - The destination EPG configuration group.
67 - This parameter or O(destination_port) or O(destination_port_channel) is required when creating the SPAN Session.
68 type: dict
69 suboptions:
70 epg_uuid:
71 description:
72 - The UUID of the destination EPG to use for the SPAN Session.
73 - This parameter or O(destination_epg.epg) is required.
74 type: str
75 epg:
76 description:
77 - The destination EPG to use for the SPAN Session.
78 - This parameter or O(destination_epg.epg_uuid) is required.
79 type: dict
80 suboptions:
81 name:
82 description:
83 - The name of the destination EPG.
84 type: str
85 required: true
86 template:
87 description:
88 - The name of the template that contains the destination EPG.
89 - This parameter or O(destination_epg.epg.template_id) is required.
90 type: str
91 template_id:
92 description:
93 - The ID of the template that contains the destination EPG.
94 - This parameter or O(destination_epg.epg.template) is required.
95 type: str
96 schema_id:
97 description:
98 - The ID of the schema that contains the destination EPG.
99 - This parameter or O(destination_epg.epg.schema) is required.
100 type: str
101 schema:
102 description:
103 - The name of the schema that contains the destination EPG.
104 - This parameter or O(destination_epg.epg.schema_id) is required.
105 type: str
106 anp:
107 description:
108 - The name of the ANP that contains the destination EPG.
109 - This parameter or O(destination_epg.epg.anp_uuid) is required.
110 type: str
111 anp_uuid:
112 description:
113 - The UUID of the ANP that contains the destination EPG.
114 - This parameter or O(destination_epg.epg.anp) is required.
115 type: str
116 destination_ip:
117 description:
118 - The destination IP address to route SPAN Session packets.
119 type: str
120 source_ip_prefix:
121 description:
122 - The prefix used to assign source IP addresses to ERSPAN packets which can be used to identify which Leaf or Spine is sending the traffic.
123 - This can be any IP. If the prefix is used, the node ID of the source node is used for the undefined bits of the prefix.
124 type: str
125 span_version:
126 description:
127 - The version of the SPAN Session.
128 - Defaults to C(v2) when unset during creation.
129 choices: [ v1, v2 ]
130 type: str
131 enforce_span_version:
132 description:
133 - Enforce the SPAN Session version defined in O(destination_epg.span_version).
134 - Defaults to true when unset during creation.
135 type: bool
136 flow_id:
137 description:
138 - The flow ID of the SPAN Session packets.
139 - The value must be in the range 1 - 1023.
140 - Defaults to 1 when unset during creation.
141 type: int
142 ttl:
143 description:
144 - The time to live (TTL) of the SPAN Session packets.
145 - The value must be in the range 1 - 1023.
146 - Defaults to 1 when unset during creation.
147 type: int
148 dscp:
149 description:
150 - The DSCP value for sending the monitored SPAN Session packets.
151 - Defaults to C(unspecified) when unset during creation.
152 choices:
153 - af11
154 - af12
155 - af13
156 - af21
157 - af22
158 - af23
159 - af31
160 - af32
161 - af33
162 - af41
163 - af42
164 - af43
165 - cs0
166 - cs1
167 - cs2
168 - cs3
169 - cs4
170 - cs5
171 - cs6
172 - cs7
173 - expedited_forwarding
174 - voice_admit
175 - unspecified
176 type: str
177 destination_port:
178 description:
179 - The destination port configuration group.
180 - This parameter or O(destination_epg) or O(destination_port_channel) is required when creating the SPAN Session.
181 type: dict
182 suboptions:
183 port_uuid:
184 description:
185 - The UUID of the destination port to use for the SPAN Session.
186 - This parameter or O(destination_port.port) is required.
187 type: str
188 port:
189 description:
190 - The destination port to use for the SPAN Session.
191 - This parameter or O(destination_port.port_uuid) is required.
192 type: dict
193 suboptions:
194 node:
195 description:
196 - The Node ID of the Node to use for the SPAN Session.
197 type: int
198 required: true
199 interface:
200 description:
201 - The Ethernet interface of the Node to use for the SPAN Session
202 type: str
203 required: true
204 destination_port_channel:
205 description:
206 - The destination port channel configuration group.
207 - This parameter or O(destination_epg) or O(destination_port) is required when creating the SPAN Session.
208 type: dict
209 suboptions:
210 port_channel_uuid:
211 description:
212 - The UUID of the destination port channel to use for the SPAN Session.
213 - This parameter or O(destination_port_channel.port_channel) is required.
214 type: str
215 port_channel:
216 description:
217 - The destination port channel to use for the SPAN Session.
218 - This parameter or O(destination_port_channel.port_channel_uuid) is required.
219 type: dict
220 suboptions:
221 name:
222 description:
223 - The name of the destination port channel.
224 type: str
225 required: true
226 template:
227 description:
228 - The name of the template that contains the destination port channel.
229 - This parameter or O(destination_port_channel.port_channel.template_id) is required.
230 type: str
231 template_id:
232 description:
233 - The ID of the template that contains the destination port channel.
234 - This parameter or O(destination_port_channel.port_channel.template) is required.
235 type: str
236 state:
237 description:
238 - Use C(absent) for removing.
239 - Use C(query) for listing an object or multiple objects.
240 - Use C(present) for creating or updating.
241 type: str
242 choices: [ absent, query, present ]
243 default: query
244 notes:
245 - The O(template) must exist before using this module in your playbook.
246 Use M(cisco.mso.ndo_template) to create the Fabric Monitoring Access Policy template.
247 - The O(destination_epg.epg) must exist before using it with this module in your playbook.
248 Use M(cisco.mso.mso_schema_template_anp_epg) to create the EPG.
249 - The O(destination_port_channel.port_channel) must exist before using it with this module in your playbook.
250 Use M(cisco.mso.ndo_port_channel_interface) to create the Fabric resource port channel interface.
251 seealso:
252 - module: cisco.mso.ndo_template
253 - module: cisco.mso.mso_schema_template_anp_epg
254 - module: cisco.mso.ndo_port_channel_interface
255 extends_documentation_fragment: cisco.mso.modules
256 """
257
258 EXAMPLES = r"""
259 - name: Create Fabric SPAN Session with destination EPG
260 cisco.mso.ndo_fabric_span_session:
261 host: mso_host
262 username: admin
263 password: SomeSecretPassword
264 template: ansible_test
265 name: ansible_test_epg
266 destination_epg:
267 epg:
268 schema: ansible_test
269 template: Template1
270 anp: Anp1
271 name: EPG1
272 destination_ip: "1.1.1.1"
273 source_ip_prefix: "2.2.2.2"
274 state: present
275 register: create_epg_span_session
276
277 - name: Create Fabric SPAN Session with destination Port
278 cisco.mso.ndo_fabric_span_session:
279 host: mso_host
280 username: admin
281 password: SomeSecretPassword
282 template: ansible_test
283 name: ansible_test_port
284 destination_port:
285 port:
286 node: 101
287 interface: "eth1/1"
288 state: present
289
290 - name: Create Fabric SPAN Session with destination Port Channel
291 cisco.mso.ndo_fabric_span_session:
292 host: mso_host
293 username: admin
294 password: SomeSecretPassword
295 template: ansible_test
296 name: ansible_test_pc
297 destination_port_channel:
298 port_channel:
299 template: ansible_test
300 name: ansible_test_resource_pc_1
301 state: present
302
303 - name: Update Fabric SPAN Session from destination EPG to destination Port
304 cisco.mso.ndo_fabric_span_session:
305 host: mso_host
306 username: admin
307 password: SomeSecretPassword
308 template: ansible_test
309 name: ansible_test_epg
310 destination_port:
311 port:
312 node: 101
313 interface: "eth1/1"
314 state: present
315
316 - name: Update Fabric SPAN Session from destination Port to destination Port Channel
317 cisco.mso.ndo_fabric_span_session:
318 host: mso_host
319 username: admin
320 password: SomeSecretPassword
321 template: ansible_test
322 name: ansible_test_port
323 destination_port_channel:
324 port_channel:
325 template: ansible_test
326 name: ansible_test_resource_pc_1
327 state: present
328
329 - name: Update Fabric SPAN Session from destination Port Channel to destination EPG
330 cisco.mso.ndo_fabric_span_session:
331 host: mso_host
332 username: admin
333 password: SomeSecretPassword
334 template: ansible_test
335 name: ansible_test_pc
336 destination_epg:
337 epg:
338 schema: ansible_test
339 template: Template1
340 anp: Anp1
341 name: EPG1
342 destination_ip: "1.1.1.1"
343 source_ip_prefix: "2.2.2.2"
344 state: present
345
346 - name: Update the Fabric SPAN Session name using UUID
347 cisco.mso.ndo_fabric_span_session:
348 host: mso_host
349 username: admin
350 password: SomeSecretPassword
351 template: ansible_test
352 uuid: "{{ create_epg_span_session.current.uuid }}"
353 name: ansible_test_pc_updated
354 state: present
355
356 - name: Query a specific Fabric SPAN Session using name
357 cisco.mso.ndo_fabric_span_session:
358 host: mso_host
359 username: admin
360 password: SomeSecretPassword
361 template: ansible_test
362 name: ansible_test_pc_updated
363 state: query
364 register: query_with_name
365
366 - name: Query a specific Fabric SPAN Session using UUID
367 cisco.mso.ndo_fabric_span_session:
368 host: mso_host
369 username: admin
370 password: SomeSecretPassword
371 template: ansible_test
372 uuid: "{{ create_epg_span_session.current.uuid }}"
373 state: query
374 register: query_with_uuid
375
376 - name: Query all Fabric SPAN Sessions
377 cisco.mso.ndo_fabric_span_session:
378 host: mso_host
379 username: admin
380 password: SomeSecretPassword
381 template: ansible_test
382 state: query
383 register: query_all_objects
384
385 - name: Delete a specific Fabric SPAN Session using Name
386 cisco.mso.ndo_fabric_span_session:
387 host: mso_host
388 username: admin
389 password: SomeSecretPassword
390 template: ansible_test
391 name: ansible_test_pc_updated
392 state: absent
393
394 - name: Delete a Fabric SPAN Session using UUID
395 cisco.mso.ndo_fabric_span_session:
396 host: mso_host
397 username: admin
398 password: SomeSecretPassword
399 template_id: ansible_test
400 uuid: "{{ create_epg_span_session.current.uuid }}"
401 state: absent
402 """
403
404 RETURN = r"""
405 """
406
407 from ansible.module_utils.basic import AnsibleModule
408 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, epg_object_reference_spec
409 from ansible_collections.cisco.mso.plugins.module_utils.schemas import MSOSchemas
410 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
411 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
412 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP, ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP
413 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
414 import copy
415
416
417 def main():
418 argument_spec = mso_argument_spec()
419 argument_spec.update(
420 template=dict(type="str"),
421 template_id=dict(type="str"),
422 name=dict(type="str", aliases=["span_session"]),
423 uuid=dict(type="str", aliases=["span_session_uuid"]),
424 description=dict(type="str"),
425 admin_state=dict(type="str", choices=["enabled", "disabled"]),
426 mtu=dict(type="int"),
427 destination_epg=dict(
428 type="dict",
429 mutually_exclusive=[
430 ("epg", "epg_uuid"),
431 ],
432 required_one_of=[
433 ["epg", "epg_uuid"],
434 ],
435 options=dict(
436 epg_uuid=dict(type="str"),
437 epg=epg_object_reference_spec(),
438 destination_ip=dict(type="str"),
439 source_ip_prefix=dict(type="str"),
440 span_version=dict(type="str", choices=["v1", "v2"]),
441 enforce_span_version=dict(type="bool"),
442 flow_id=dict(type="int"),
443 ttl=dict(type="int"),
444 dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)),
445 ),
446 ),
447 destination_port=dict(
448 type="dict",
449 mutually_exclusive=[
450 ("port", "port_uuid"),
451 ],
452 required_one_of=[
453 ["port", "port_uuid"],
454 ],
455 options=dict(
456 port_uuid=dict(type="str"),
457 port=dict(
458 type="dict",
459 options=dict(
460 node=dict(type="int", required=True),
461 interface=dict(type="str", required=True),
462 ),
463 ),
464 ),
465 ),
466 destination_port_channel=dict(
467 type="dict",
468 mutually_exclusive=[
469 ("port_channel", "port_channel_uuid"),
470 ],
471 required_one_of=[
472 ["port_channel", "port_channel_uuid"],
473 ],
474 options=dict(
475 port_channel_uuid=dict(type="str"),
476 port_channel=dict(
477 type="dict",
478 options=dict(
479 name=dict(type="str", required=True),
480 template=dict(type="str"),
481 template_id=dict(type="str"),
482 ),
483 required_one_of=[
484 ["template", "template_id"],
485 ],
486 mutually_exclusive=[
487 ("template", "template_id"),
488 ],
489 ),
490 ),
491 ),
492 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
493 )
494
495 module = AnsibleModule(
496 argument_spec=argument_spec,
497 supports_check_mode=True,
498 mutually_exclusive=[("template", "template_id"), ("destination_epg", "destination_port_channel", "destination_port")],
499 required_if=[
500 ["state", "absent", ["name", "uuid"], True],
501 ["state", "present", ["name", "uuid"], True],
502 ],
503 required_one_of=[
504 ["template", "template_id"],
505 ],
506 )
507
508 mso = MSOModule(module)
509 mso_schemas = MSOSchemas(mso)
510 mso_templates = MSOTemplates(mso)
511
512 template_name = module.params.get("template")
513 template_id = module.params.get("template_id")
514 name = module.params.get("name")
515 uuid = module.params.get("uuid")
516 description = module.params.get("description")
517 admin_state = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(module.params.get("admin_state")) if module.params.get("admin_state") else None
518 mtu = module.params.get("mtu")
519 destination_epg = module.params.get("destination_epg")
520 destination_port = module.params.get("destination_port")
521 destination_port_channel = module.params.get("destination_port_channel")
522 state = module.params.get("state")
523
524 mso_template = MSOTemplate(mso, "monitoring_tenant", template_name, template_id)
525 mso_template.validate_template("monitoring")
526 object_description = "SPAN Session"
527 site_id = mso_template.template.get("monitoringTemplate").get("sites")[0].get("siteId")
528
529 match = None
530 if uuid or name:
531 match = mso_template.get_object_by_key_value_pairs(
532 object_description,
533 mso_template.template.get("monitoringTemplate", {}).get("template", {}).get("spanSessions", []),
534 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
535 )
536 else:
537 match = mso_template.template.get("monitoringTemplate", {}).get("template", {}).get("spanSessions", [])
538
539 if (uuid or name) and match:
540 set_fabric_span_session_object_details(mso_template, site_id, match.details)
541 mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object
542 elif match:
543 mso.existing = [set_fabric_span_session_object_details(mso_template, site_id, obj) for obj in match] # Query all objects
544
545 if state != "query":
546 span_session_path = "/monitoringTemplate/template/spanSessions/{0}".format(match.index if match else "-")
547
548 ops = []
549
550 if state == "present":
551 if uuid and not mso.existing:
552 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
553
554 mso_values = dict()
555 if destination_epg:
556 mso_values["destination"] = dict(
557 remote=dict(
558 epgRef=mso_schemas.get_epg_uuid(destination_epg.get("epg"), destination_epg.get("epg_uuid")),
559 spanVersion=destination_epg.get("span_version"),
560 enforceSpanVersion=destination_epg.get("enforce_span_version"),
561 destIPAddress=destination_epg.get("destination_ip"),
562 srcIPPrefix=destination_epg.get("source_ip_prefix"),
563 flowID=destination_epg.get("flow_id"),
564 ttl=destination_epg.get("ttl"),
565 dscp=TARGET_DSCP_MAP.get(destination_epg.get("dscp")),
566 ),
567 mtu=mtu,
568 )
569
570 if destination_port:
571 # Destination Port supports UUIDs, but we have no module to get them
572 destination_port_uuid = destination_port.get("port_uuid")
573
574 if destination_port_uuid is None:
575 node = destination_port.get("port").get("node")
576 interface_port = destination_port.get("port").get("interface")
577 destination_port_uuid = mso.get_site_interface_details(site_id=site_id, uuid=None, node=node, port=interface_port).get("uuid")
578 mso_values["destination"] = dict(local=dict(accessInterface=destination_port_uuid), mtu=mtu)
579
580 if destination_port_channel:
581 destination_port_channel_uuid = destination_port_channel.get("port_channel_uuid")
582 if destination_port_channel_uuid is None:
583 fabric_resource_template = mso_templates.get_template(
584 "fabric_resource",
585 destination_port_channel.get("port_channel").get("template"),
586 destination_port_channel.get("port_channel").get("template_id"),
587 )
588
589 destination_port_channel_uuid = fabric_resource_template.get_template_policy_uuid(
590 "fabric_resource", destination_port_channel.get("port_channel").get("name"), "portChannels"
591 )
592
593 mso_values["destination"] = dict(local=dict(portChannel=destination_port_channel_uuid), mtu=mtu)
594
595 if match:
596 mso_update_values = {"name": name, "description": description}
597 mso_update_values[("destination", "mtu")] = mtu
598
599 if admin_state is not None:
600 mso_update_values[("sourceGroup", "enableAdminState")] = admin_state
601
602 if destination_epg:
603 if match.details.get("destination", {}).get("local", {}).get("accessInterface") or match.details.get("destination", {}).get("local", {}).get(
604 "portChannel"
605 ):
606 mso_update_values[("destination", "local")] = dict()
607
608 remote_group = mso_values.get("destination", {}).get("remote")
609 if remote_group:
610 mso_update_values[("destination", "remote", "epgRef")] = remote_group.get("epgRef")
611 mso_update_values[("destination", "remote", "spanVersion")] = remote_group.get("spanVersion")
612 mso_update_values[("destination", "remote", "enforceSpanVersion")] = remote_group.get("enforceSpanVersion")
613 mso_update_values[("destination", "remote", "destIPAddress")] = remote_group.get("destIPAddress")
614 mso_update_values[("destination", "remote", "srcIPPrefix")] = remote_group.get("srcIPPrefix")
615 mso_update_values[("destination", "remote", "flowID")] = remote_group.get("flowID")
616 mso_update_values[("destination", "remote", "ttl")] = remote_group.get("ttl")
617 mso_update_values[("destination", "remote", "dscp")] = remote_group.get("dscp")
618
619 if destination_port:
620 if match.details.get("destination", {}).get("remote", {}).get("epgRef"):
621 mso_update_values[("destination", "remote")] = dict()
622
623 if match.details.get("destination", {}).get("local", {}).get("portChannel"):
624 mso_update_values[("destination", "local")] = dict()
625
626 mso_update_values[("destination", "local")] = dict(accessInterface=destination_port_uuid)
627
628 if destination_port_channel:
629 if match.details.get("destination", {}).get("remote", {}).get("epgRef"):
630 mso_update_values[("destination", "remote")] = dict()
631
632 if match.details.get("destination", {}).get("local", {}).get("accessInterface"):
633 mso_update_values[("destination", "local")] = dict()
634
635 mso_update_values[("destination", "local")] = dict(portChannel=destination_port_channel_uuid)
636
637 proposed_payload = copy.deepcopy(match.details)
638 append_update_ops_data(ops, proposed_payload, span_session_path, mso_update_values)
639 mso.sanitize(proposed_payload, collate=True)
640 else:
641 mso_values["name"] = name
642 mso_values["description"] = description
643
644 if admin_state is not None:
645 mso_values["sourceGroup"] = dict(enableAdminState=admin_state)
646
647 mso.sanitize(mso_values)
648 ops.append(dict(op="add", path=span_session_path, value=mso.sent))
649
650 elif state == "absent":
651 if match:
652 ops.append(dict(op="remove", path=span_session_path))
653
654 if not module.check_mode and ops:
655 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
656 match = mso_template.get_object_by_key_value_pairs(
657 object_description,
658 response.get("monitoringTemplate", {}).get("template", {}).get("spanSessions", []),
659 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
660 )
661 if match:
662 set_fabric_span_session_object_details(mso_template, site_id, match.details)
663 mso.existing = match.details # When the state is present
664 else:
665 mso.existing = {} # When the state is absent
666 elif module.check_mode and state != "query": # When the state is present/absent with check mode
667 set_fabric_span_session_object_details(mso_template, site_id, mso.proposed)
668 mso.existing = mso.proposed if state == "present" else {}
669 mso.exit_json()
670
671
672 def set_fabric_span_session_object_details(mso_template, site_id, span_session):
673 if span_session:
674 span_session.update({"templateId": mso_template.template_id, "templateName": mso_template.template_name})
675 if span_session.get("destination", {}).get("local", {}).get("accessInterface"):
676 interface = mso_template.mso.get_site_interface_details(site_id, span_session.get("destination").get("local").get("accessInterface"))
677 interface.pop("uuid", None)
678 span_session.get("destination").get("local").update(interface)
679 else:
680 reference_details = {
681 "remote": {
682 "name": "epgName",
683 "reference": "epgRef",
684 "type": "epg",
685 "template": "epgTemplateName",
686 "templateId": "epgTemplateId",
687 "schema": "epgSchemaName",
688 "schemaId": "epgSchemaId",
689 },
690 "local": {
691 "name": "portChannelName",
692 "reference": "portChannel",
693 "type": "portChannel",
694 "template": "portChannelTemplateName",
695 "templateId": "portChannelTemplateId",
696 },
697 }
698 mso_template.update_config_with_template_and_references(
699 span_session.get("destination"),
700 reference_details,
701 False,
702 )
703 return span_session
704
705
706 if __name__ == "__main__":
707 main()