Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Samita Bhattacharjee (@samiib) <[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 = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_tenant_span_session
21 version_added: "2.11.0"
22 short_description: Manage Tenant SPAN Sessions on Cisco Nexus Dashboard Orchestrator (NDO).
23 description:
24 - Manage Switched Port Analyzer (SPAN) Sessions on Cisco Nexus Dashboard Orchestrator (NDO).
25 - This module is only supported on ND v3.1 (NDO v4.3) and later.
26 author:
27 - Samita Bhattacharjee (@samiib)
28 options:
29 template:
30 description:
31 - The name of the template.
32 - The template must be a Tenant Monitoring Policy template.
33 - This parameter or O(template_id) is required.
34 type: str
35 template_id:
36 description:
37 - The ID of the template.
38 - The template must be a Tenant Monitoring Policy template.
39 - This parameter or O(template) is required.
40 type: str
41 name:
42 description:
43 - The name of the SPAN Session.
44 type: str
45 aliases: [ span_session ]
46 uuid:
47 description:
48 - The UUID of the SPAN Session.
49 - This parameter is required when the O(name) needs to be updated.
50 type: str
51 aliases: [ span_session_uuid ]
52 description:
53 description:
54 - The description of the SPAN Session.
55 type: str
56 sources:
57 description:
58 - The SPAN Session sources.
59 - Providing a new list of O(sources) will replace the existing sources from the SPAN Session.
60 - Providing an empty list will remove the O(sources=[]) from the SPAN Session.
61 type: list
62 elements: dict
63 suboptions:
64 name:
65 description:
66 - The name of the SPAN Session source.
67 type: str
68 required: true
69 direction:
70 description:
71 - The direction of the SPAN Session source.
72 type: str
73 required: true
74 choices: [ incoming, outgoing, both ]
75 epg_uuid:
76 description:
77 - The UUID of the source Endpoint Group (EPG) to use for the SPAN Session.
78 - This parameter or O(sources.epg) is required.
79 type: str
80 epg:
81 description:
82 - The EPG to use for the SPAN Session source.
83 - This parameter or O(sources.epg_uuid) is required.
84 type: dict
85 suboptions:
86 name:
87 description:
88 - The name of the source EPG.
89 type: str
90 required: true
91 template:
92 description:
93 - The name of the template that contains the source EPG.
94 - This parameter or O(sources.epg.template_id) is required.
95 type: str
96 template_id:
97 description:
98 - The ID of the template that contains the source EPG.
99 - This parameter or O(sources.epg.template) is required.
100 type: str
101 schema_id:
102 description:
103 - The ID of the schema that contains the source EPG.
104 - This parameter or O(sources.epg.schema) is required.
105 type: str
106 schema:
107 description:
108 - The name of the schema that contains the source EPG.
109 - This parameter or O(sources.epg.schema_id) is required.
110 type: str
111 anp:
112 description:
113 - The name of the Application Profile (ANP) that contains the source EPG.
114 - This parameter or O(sources.epg.anp_uuid) is required.
115 type: str
116 anp_uuid:
117 description:
118 - The UUID of the ANP that contains the source EPG.
119 - This parameter or O(sources.epg.anp) is required.
120 type: str
121 admin_state:
122 description:
123 - The administrative state of the SPAN Session.
124 - Defaults to C(enabled) when unset during creation.
125 choices: [ enabled, disabled ]
126 type: str
127 mtu:
128 description:
129 - The MTU truncation size for the SPAN packets.
130 - The value must be in the range 64 - 9216.
131 - Defaults to 1518 when unset during creation.
132 type: int
133 destination_epg:
134 description:
135 - The destination EPG configuration group.
136 type: dict
137 suboptions:
138 epg_uuid:
139 description:
140 - The UUID of the destination EPG to use for the SPAN Session.
141 - This parameter or O(destination_epg.epg) is required when creating the SPAN Session.
142 type: str
143 epg:
144 description:
145 - The destination EPG to use for the SPAN Session.
146 - This parameter or O(destination_epg.epg_uuid) is required when creating the SPAN Session.
147 type: dict
148 suboptions:
149 name:
150 description:
151 - The name of the destination EPG.
152 type: str
153 required: true
154 template:
155 description:
156 - The name of the template that contains the destination EPG.
157 - This parameter or O(destination_epg.epg.template_id) is required.
158 type: str
159 template_id:
160 description:
161 - The ID of the that contains the destination EPG.
162 - This parameter or O(destination_epg.epg.template) is required.
163 type: str
164 schema_id:
165 description:
166 - The ID of the schema that contains the destination EPG.
167 - This parameter or O(destination_epg.epg.schema) is required.
168 type: str
169 schema:
170 description:
171 - The name of the schema that contains the destination EPG.
172 - This parameter or O(destination_epg.epg.schema_id) is required.
173 type: str
174 anp:
175 description:
176 - The name of the ANP that contains the destination EPG.
177 - This parameter or O(destination_epg.epg.anp_uuid) is required.
178 type: str
179 anp_uuid:
180 description:
181 - The UUID of the ANP that contains the destination EPG.
182 - This parameter or O(destination_epg.epg.anp) is required.
183 type: str
184 destination_ip:
185 description:
186 - The destination IP address to route SPAN Session packets.
187 type: str
188 source_ip_prefix:
189 description:
190 - 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.
191 - 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.
192 type: str
193 span_version:
194 description:
195 - The version of the SPAN Session.
196 - Defaults to C(v2) when unset during creation.
197 choices: [ v1, v2 ]
198 type: str
199 enforce_span_version:
200 description:
201 - Enforce the SPAN Session version defined in O(destination_epg.span_version).
202 - Defaults to true when unset during creation.
203 type: bool
204 flow_id:
205 description:
206 - The flow ID of the SPAN Session packets.
207 - The value must be in the range 1 - 1023.
208 - Defaults to 1 when unset during creation.
209 type: int
210 ttl:
211 description:
212 - The time to live (TTL) of the SPAN Session packets.
213 - The value must be in the range 1 - 1023.
214 - Defaults to 1 when unset during creation.
215 type: int
216 dscp:
217 description:
218 - The DSCP value for sending the monitored SPAN Session packets.
219 - Defaults to C(unspecified) when unset during creation.
220 choices:
221 - af11
222 - af12
223 - af13
224 - af21
225 - af22
226 - af23
227 - af31
228 - af32
229 - af33
230 - af41
231 - af42
232 - af43
233 - cs0
234 - cs1
235 - cs2
236 - cs3
237 - cs4
238 - cs5
239 - cs6
240 - cs7
241 - expedited_forwarding
242 - voice_admit
243 - unspecified
244 type: str
245 state:
246 description:
247 - Use C(absent) for removing.
248 - Use C(query) for listing an object or multiple objects.
249 - Use C(present) for creating or updating.
250 type: str
251 choices: [ absent, query, present ]
252 default: query
253 notes:
254 - The O(template), O(destination_epg.epg) and O(sources.epg) must exist before using this module in your playbook.
255 Use M(cisco.mso.ndo_template) to create the Tenant Monitoring Policy template.
256 Use M(cisco.mso.mso_schema_template_anp_epg) to create the EPGs.
257 seealso:
258 - module: cisco.mso.ndo_template
259 - module: cisco.mso.mso_schema_template_anp_epg
260 extends_documentation_fragment: cisco.mso.modules
261 """
262
263 EXAMPLES = r"""
264 - name: Create a new SPAN Session
265 cisco.mso.ndo_tenant_span_session:
266 host: mso_host
267 username: admin
268 password: SomeSecretPassword
269 template: monitoring_tenant_template
270 name: span_session_1
271 sources:
272 - name: source_1
273 epg_uuid: '{{ query_epg_1.current.uuid }}'
274 direction: both
275 - name: source_2
276 direction: outgoing
277 epg:
278 name: epg_2
279 anp: anp_1
280 template: template_1
281 schema: schema_1
282 admin_state: enabled
283 mtu: 9216
284 destination_epg:
285 epg:
286 name: epg_3
287 anp: anp_1
288 template: template_1
289 schema: schema_1
290 destination_ip: 10.1.1.1
291 source_ip_prefix: 10.1.1.1/24
292 span_version: v1
293 enforce_span_version: false
294 flow_id: 15
295 ttl: 128
296 dscp: af11
297 state: present
298 register: create_span_session_1
299
300 - name: Update the name of the SPAN Session using UUID
301 cisco.mso.ndo_tenant_span_session:
302 host: mso_host
303 username: admin
304 password: SomeSecretPassword
305 template_id: "{{ create_monitoring_tenant.current.templateId }}"
306 uuid: "{{ create_span_session_1.current.uuid }}"
307 name: span_session_1_updated
308 state: present
309 register: update_span_session_1
310
311 - name: Query an existing SPAN Session using UUID
312 cisco.mso.ndo_tenant_span_session:
313 host: mso_host
314 username: admin
315 password: SomeSecretPassword
316 template: monitoring_tenant_template
317 uuid: "{{ create_span_session_1.current.uuid }}"
318 state: query
319 register: query_with_uuid
320
321 - name: Query an existing SPAN Session using name
322 cisco.mso.ndo_tenant_span_session:
323 host: mso_host
324 username: admin
325 password: SomeSecretPassword
326 template: monitoring_tenant_template
327 name: span_session_1_updated
328 state: query
329 register: query_with_name
330
331 - name: Query all SPAN Sessions
332 cisco.mso.ndo_tenant_span_session:
333 host: mso_host
334 username: admin
335 password: SomeSecretPassword
336 template: monitoring_tenant_template
337 state: query
338 register: query_all
339
340 - name: Remove all sources from a SPAN Session
341 cisco.mso.ndo_tenant_span_session:
342 host: mso_host
343 username: admin
344 password: SomeSecretPassword
345 template: monitoring_tenant_template
346 name: span_session_1_updated
347 sources: []
348 state: present
349
350 - name: Delete an existing SPAN Session using UUID
351 cisco.mso.ndo_tenant_span_session:
352 host: mso_host
353 username: admin
354 password: SomeSecretPassword
355 template: monitoring_tenant_template
356 uuid: "{{ create_span_session_1.current.uuid }}"
357 state: absent
358
359 - name: Delete an existing SPAN Session using Name
360 cisco.mso.ndo_tenant_span_session:
361 host: mso_host
362 username: admin
363 password: SomeSecretPassword
364 template: monitoring_tenant_template
365 name: span_session_1_updated
366 state: absent
367 """
368
369 RETURN = r"""
370 """
371
372 from ansible.module_utils.basic import AnsibleModule
373 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, epg_object_reference_spec
374 from ansible_collections.cisco.mso.plugins.module_utils.schemas import MSOSchemas
375 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
376 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP, ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP
377 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
378 import copy
379
380
381 def main():
382 argument_spec = mso_argument_spec()
383 argument_spec.update(
384 template=dict(type="str"),
385 template_id=dict(type="str"),
386 name=dict(type="str", aliases=["span_session"]),
387 uuid=dict(type="str", aliases=["span_session_uuid"]),
388 description=dict(type="str"),
389 sources=dict(
390 type="list",
391 elements="dict",
392 mutually_exclusive=[
393 ("epg", "epg_uuid"),
394 ],
395 required_one_of=[
396 ["epg", "epg_uuid"],
397 ],
398 options=dict(
399 name=dict(type="str", required=True),
400 direction=dict(type="str", required=True, choices=["incoming", "outgoing", "both"]),
401 epg_uuid=dict(type="str"),
402 epg=epg_object_reference_spec(),
403 ),
404 ),
405 admin_state=dict(type="str", choices=list(ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP)),
406 mtu=dict(type="int"),
407 destination_epg=dict(
408 type="dict",
409 mutually_exclusive=[
410 ("epg", "epg_uuid"),
411 ],
412 options=dict(
413 epg_uuid=dict(type="str"),
414 epg=epg_object_reference_spec(),
415 destination_ip=dict(type="str"),
416 source_ip_prefix=dict(type="str"),
417 span_version=dict(type="str", choices=["v1", "v2"]),
418 enforce_span_version=dict(type="bool"),
419 flow_id=dict(type="int"),
420 ttl=dict(type="int"),
421 dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)),
422 ),
423 ),
424 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
425 )
426
427 module = AnsibleModule(
428 argument_spec=argument_spec,
429 supports_check_mode=True,
430 mutually_exclusive=[
431 ("template", "template_id"),
432 ],
433 required_if=[
434 ["state", "absent", ["name", "uuid"], True],
435 ["state", "present", ["name", "uuid"], True],
436 ],
437 required_one_of=[["template", "template_id"]],
438 )
439
440 mso = MSOModule(module)
441 mso_schemas = MSOSchemas(mso)
442
443 template_name = module.params.get("template")
444 template_id = module.params.get("template_id")
445 name = module.params.get("name")
446 uuid = module.params.get("uuid")
447 description = module.params.get("description")
448 sources = module.params.get("sources")
449 admin_state = module.params.get("admin_state")
450 mtu = module.params.get("mtu")
451 destination_epg = module.params.get("destination_epg")
452 state = module.params.get("state")
453
454 ops = []
455 match = None
456 reference_collection = {
457 "epgRef": {
458 "name": "epgName",
459 "reference": "epgRef",
460 "type": "epg",
461 "template": "epgTemplateName",
462 "templateId": "epgTemplateId",
463 "schema": "epgSchemaName",
464 "schemaId": "epgSchemaId",
465 },
466 "epg": {
467 "name": "epgName",
468 "reference": "epg",
469 "type": "epg",
470 "template": "epgTemplateName",
471 "templateId": "epgTemplateId",
472 "schema": "epgSchemaName",
473 "schemaId": "epgSchemaId",
474 },
475 }
476
477 mso_template = MSOTemplate(mso, "monitoring_tenant", template_name, template_id)
478 mso_template.validate_template("monitoring")
479 object_description = "SPAN Session"
480 path = "/monitoringTemplate/template/spanSessions"
481 span_session_path = None
482
483 existing_span_sessions = mso_template.template.get("monitoringTemplate", {}).get("template", {}).get("spanSessions") or []
484
485 if name or uuid:
486 match = mso_template.get_object_by_key_value_pairs(
487 object_description,
488 existing_span_sessions,
489 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
490 )
491 if match:
492 span_session_path = "{0}/{1}".format(path, match.index)
493 mso_template.update_config_with_template_and_references(match.details, reference_collection)
494 mso.existing = mso.previous = copy.deepcopy(match.details)
495 else:
496 mso.existing = mso.previous = [mso_template.update_config_with_template_and_references(obj, reference_collection) for obj in existing_span_sessions]
497
498 if state == "present":
499 if uuid and not mso.existing:
500 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
501
502 mso_values = dict(name=name, description=description, destination=dict(mtu=mtu))
503 if admin_state is not None or sources is not None:
504 source_group = dict()
505 if admin_state is not None:
506 source_group["enableAdminState"] = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(admin_state)
507 if sources is not None:
508 source_group["sources"] = format_sources(mso_schemas, sources)
509 mso_values["sourceGroup"] = source_group
510 if destination_epg:
511 mso_values["destination"]["remote"] = dict(
512 epgRef=mso_schemas.get_epg_uuid(destination_epg.get("epg"), destination_epg.get("epg_uuid")),
513 spanVersion=destination_epg.get("span_version"),
514 enforceSpanVersion=destination_epg.get("enforce_span_version"),
515 destIPAddress=destination_epg.get("destination_ip"),
516 srcIPPrefix=destination_epg.get("source_ip_prefix"),
517 flowID=destination_epg.get("flow_id"),
518 ttl=destination_epg.get("ttl"),
519 dscp=TARGET_DSCP_MAP.get(destination_epg.get("dscp")),
520 )
521
522 if match:
523 mso_update_values = {
524 "name": name,
525 "description": description,
526 ("destination", "mtu"): mtu,
527 }
528 source_group = mso_values.get("sourceGroup")
529 if source_group:
530 mso_update_values[("sourceGroup", "enableAdminState")] = source_group.get("enableAdminState")
531 mso_update_values[("sourceGroup", "sources")] = source_group.get("sources")
532 if destination_epg:
533 remote_group = mso_values.get("destination", {}).get("remote")
534 if remote_group:
535 mso_update_values[("destination", "remote", "epgRef")] = remote_group.get("epgRef")
536 mso_update_values[("destination", "remote", "spanVersion")] = remote_group.get("spanVersion")
537 mso_update_values[("destination", "remote", "enforceSpanVersion")] = remote_group.get("enforceSpanVersion")
538 mso_update_values[("destination", "remote", "destIPAddress")] = remote_group.get("destIPAddress")
539 mso_update_values[("destination", "remote", "srcIPPrefix")] = remote_group.get("srcIPPrefix")
540 mso_update_values[("destination", "remote", "flowID")] = remote_group.get("flowID")
541 mso_update_values[("destination", "remote", "ttl")] = remote_group.get("ttl")
542 mso_update_values[("destination", "remote", "dscp")] = remote_group.get("dscp")
543
544 proposed_payload = copy.deepcopy(match.details)
545 append_update_ops_data(ops, proposed_payload, span_session_path, mso_update_values)
546 mso.sanitize(proposed_payload, collate=True)
547 else:
548 mso.sanitize(mso_values)
549 ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
550
551 elif state == "absent":
552 if match:
553 ops.append(dict(op="remove", path=span_session_path))
554
555 if not module.check_mode and ops:
556 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
557 node_profiles = response.get("monitoringTemplate", {}).get("template", {}).get("spanSessions") or []
558 match = mso_template.get_object_by_key_value_pairs(
559 object_description,
560 node_profiles,
561 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
562 )
563 if match:
564 mso_template.update_config_with_template_and_references(match.details, reference_collection)
565 mso.existing = match.details # When the state is present
566 else:
567 mso.existing = {} # When the state is absent
568 elif module.check_mode and state != "query": # When the state is present/absent with check mode
569 mso_template.update_config_with_template_and_references(mso.proposed, reference_collection)
570 mso.existing = mso.proposed if state == "present" else {}
571
572 mso.exit_json()
573
574
575 def format_sources(schemas, sources):
576 source_list = []
577 for source in sources:
578 source_values = {
579 "name": source.get("name"),
580 "direction": source.get("direction"),
581 "epg": schemas.get_epg_uuid(source.get("epg"), source.get("epg_uuid")),
582 }
583 source_list.append(source_values)
584 return source_list
585
586
587 if __name__ == "__main__":
588 main()