| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Akini Ross <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_access_span_dst_group |
| 16 |
short_description: Manage Access SPAN destination groups (span:DestGrp) |
| 17 |
description: |
| 18 |
- Manage Access SPAN destination groups on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
destination_group: |
| 21 |
description: |
| 22 |
- The name of the Access SPAN destination group. |
| 23 |
type: str |
| 24 |
aliases: [ name, dst_group ] |
| 25 |
description: |
| 26 |
description: |
| 27 |
- The description of the Access SPAN destination group. |
| 28 |
type: str |
| 29 |
aliases: [ descr ] |
| 30 |
access_interface: |
| 31 |
description: |
| 32 |
- The destination access interface. |
| 33 |
- The I(access_interface) and I(destination_epg) cannot be configured simultaneously. |
| 34 |
type: dict |
| 35 |
suboptions: |
| 36 |
pod: |
| 37 |
description: |
| 38 |
- The pod id part of the destination path. |
| 39 |
type: int |
| 40 |
required: true |
| 41 |
aliases: [ pod_id, pod_number ] |
| 42 |
node: |
| 43 |
description: |
| 44 |
- The node id part of the destination path. |
| 45 |
type: int |
| 46 |
required: true |
| 47 |
aliases: [ node_id ] |
| 48 |
path: |
| 49 |
description: |
| 50 |
- The interface part of the destination path. |
| 51 |
- When path is of type port a interface like C(eth1/7) must be provided. |
| 52 |
- When path is of type direct_port_channel the name of a policy group like C(test_PolGrp) must be provided. |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
mtu: |
| 56 |
description: |
| 57 |
- The MTU truncation size for the packets. |
| 58 |
- The APIC defaults to C(1518) when unset during creation. |
| 59 |
type: int |
| 60 |
destination_epg: |
| 61 |
description: |
| 62 |
- The destination end point group. |
| 63 |
- The I(access_interface) and I(destination_epg) cannot be configured simultaneously. |
| 64 |
type: dict |
| 65 |
suboptions: |
| 66 |
tenant: |
| 67 |
description: |
| 68 |
- The name of the tenant. |
| 69 |
type: str |
| 70 |
required: true |
| 71 |
aliases: [ tenant_name ] |
| 72 |
ap: |
| 73 |
description: |
| 74 |
- The name of application profile. |
| 75 |
type: str |
| 76 |
required: true |
| 77 |
aliases: [ ap_name, app_profile, app_profile_name ] |
| 78 |
epg: |
| 79 |
description: |
| 80 |
- The name of the end point group. |
| 81 |
type: str |
| 82 |
required: true |
| 83 |
aliases: [ epg_name ] |
| 84 |
span_version: |
| 85 |
description: |
| 86 |
- The SPAN version. |
| 87 |
- The APIC defaults to C(version_2) when unset during creation. |
| 88 |
type: str |
| 89 |
choices: [ version_1, version_2 ] |
| 90 |
version_enforced: |
| 91 |
description: |
| 92 |
- Enforce SPAN version. |
| 93 |
type: bool |
| 94 |
source_ip: |
| 95 |
description: |
| 96 |
- The source IP address or prefix. |
| 97 |
type: str |
| 98 |
required: true |
| 99 |
destination_ip: |
| 100 |
description: |
| 101 |
- The destination IP address. |
| 102 |
type: str |
| 103 |
required: true |
| 104 |
flow_id: |
| 105 |
description: |
| 106 |
- The flow ID of the SPAN packet. |
| 107 |
- The APIC defaults to C(1) when unset during creation. |
| 108 |
type: int |
| 109 |
ttl: |
| 110 |
description: |
| 111 |
- The time to live of the span session packets. |
| 112 |
- The APIC defaults to C(64) when unset during creation. |
| 113 |
type: int |
| 114 |
mtu: |
| 115 |
description: |
| 116 |
- The MTU truncation size for the packets. |
| 117 |
- The APIC defaults to C(1518) when unset during creation. |
| 118 |
type: int |
| 119 |
dscp: |
| 120 |
description: |
| 121 |
- The DSCP value for sending the monitored packets using ERSPAN. |
| 122 |
- The APIC defaults to C(unspecified) when unset during creation. |
| 123 |
type: str |
| 124 |
choices: [ CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, unspecified ] |
| 125 |
state: |
| 126 |
description: |
| 127 |
- Use C(present) or C(absent) for adding or removing. |
| 128 |
- Use C(query) for listing an object or multiple objects. |
| 129 |
type: str |
| 130 |
choices: [ absent, present, query ] |
| 131 |
default: present |
| 132 |
name_alias: |
| 133 |
description: |
| 134 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 135 |
type: str |
| 136 |
extends_documentation_fragment: |
| 137 |
- cisco.aci.aci |
| 138 |
- cisco.aci.annotation |
| 139 |
- cisco.aci.owner |
| 140 |
|
| 141 |
seealso: |
| 142 |
- name: APIC Management Information Model reference |
| 143 |
description: More information about the internal APIC class B(span:DestGrp). |
| 144 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 145 |
author: |
| 146 |
- Akini Ross (@akinross) |
| 147 |
""" |
| 148 |
|
| 149 |
EXAMPLES = r""" |
| 150 |
- name: Add a Access SPAN destination group of type EPG |
| 151 |
cisco.aci.aci_access_span_dst_group: |
| 152 |
host: apic |
| 153 |
username: admin |
| 154 |
password: SomeSecretPassword |
| 155 |
destination_group: group1 |
| 156 |
description: Test span |
| 157 |
destination_epg: |
| 158 |
tenant: Test1 |
| 159 |
ap: ap1 |
| 160 |
epg: ep1 |
| 161 |
span_version: version_1 |
| 162 |
version_enforced: false |
| 163 |
destination_ip: 10.0.0.1 |
| 164 |
source_ip: 10.0.2.1 |
| 165 |
ttl: 2 |
| 166 |
mtu: 1500 |
| 167 |
flow_id: 1 |
| 168 |
dscp: CS1 |
| 169 |
state: present |
| 170 |
delegate_to: localhost |
| 171 |
|
| 172 |
- name: Add a Access SPAN destination group of type access interface port |
| 173 |
cisco.aci.aci_access_span_dst_group: |
| 174 |
host: apic |
| 175 |
username: admin |
| 176 |
password: SomeSecretPassword |
| 177 |
destination_group: group1 |
| 178 |
description: Test span |
| 179 |
access_interface: |
| 180 |
pod: 1 |
| 181 |
node: 101 |
| 182 |
path: 1/1 |
| 183 |
mtu: 1500 |
| 184 |
state: present |
| 185 |
delegate_to: localhost |
| 186 |
|
| 187 |
- name: Add a Access SPAN destination group of type access interface direct_port_channel |
| 188 |
cisco.aci.aci_access_span_dst_group: |
| 189 |
host: apic |
| 190 |
username: admin |
| 191 |
password: SomeSecretPassword |
| 192 |
destination_group: group1 |
| 193 |
description: Test span |
| 194 |
access_interface: |
| 195 |
pod: 1 |
| 196 |
node: 101 |
| 197 |
path: Switch101_1-ports-1-2_PolGrp |
| 198 |
mtu: 1500 |
| 199 |
state: present |
| 200 |
delegate_to: localhost |
| 201 |
|
| 202 |
- name: Remove a Access SPAN destination group |
| 203 |
cisco.aci.aci_access_span_dst_group: |
| 204 |
host: apic |
| 205 |
username: admin |
| 206 |
password: SomeSecretPassword |
| 207 |
destination_group: group1 |
| 208 |
state: absent |
| 209 |
delegate_to: localhost |
| 210 |
|
| 211 |
- name: Query a Access SPAN destination group |
| 212 |
cisco.aci.aci_access_span_dst_group: |
| 213 |
host: apic |
| 214 |
username: admin |
| 215 |
password: SomeSecretPassword |
| 216 |
destination_group: group1 |
| 217 |
state: query |
| 218 |
delegate_to: localhost |
| 219 |
register: query_result |
| 220 |
|
| 221 |
- name: Query all Access SPAN destination groups |
| 222 |
cisco.aci.aci_access_span_dst_group: |
| 223 |
host: apic |
| 224 |
username: admin |
| 225 |
password: SomeSecretPassword |
| 226 |
state: query |
| 227 |
delegate_to: localhost |
| 228 |
register: query_result |
| 229 |
""" |
| 230 |
|
| 231 |
RETURN = r""" |
| 232 |
current: |
| 233 |
description: The existing configuration from the APIC after the module has finished |
| 234 |
returned: success |
| 235 |
type: list |
| 236 |
sample: |
| 237 |
[ |
| 238 |
{ |
| 239 |
"fvTenant": { |
| 240 |
"attributes": { |
| 241 |
"descr": "Production environment", |
| 242 |
"dn": "uni/tn-production", |
| 243 |
"name": "production", |
| 244 |
"nameAlias": "", |
| 245 |
"ownerKey": "", |
| 246 |
"ownerTag": "" |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
] |
| 251 |
error: |
| 252 |
description: The error information as returned from the APIC |
| 253 |
returned: failure |
| 254 |
type: dict |
| 255 |
sample: |
| 256 |
{ |
| 257 |
"code": "122", |
| 258 |
"text": "unknown managed object class foo" |
| 259 |
} |
| 260 |
raw: |
| 261 |
description: The raw output returned by the APIC REST API (xml or json) |
| 262 |
returned: parse error |
| 263 |
type: str |
| 264 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 265 |
sent: |
| 266 |
description: The actual/minimal configuration pushed to the APIC |
| 267 |
returned: info |
| 268 |
type: list |
| 269 |
sample: |
| 270 |
{ |
| 271 |
"fvTenant": { |
| 272 |
"attributes": { |
| 273 |
"descr": "Production environment" |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
previous: |
| 278 |
description: The original configuration from the APIC before the module has started |
| 279 |
returned: info |
| 280 |
type: list |
| 281 |
sample: |
| 282 |
[ |
| 283 |
{ |
| 284 |
"fvTenant": { |
| 285 |
"attributes": { |
| 286 |
"descr": "Production", |
| 287 |
"dn": "uni/tn-production", |
| 288 |
"name": "production", |
| 289 |
"nameAlias": "", |
| 290 |
"ownerKey": "", |
| 291 |
"ownerTag": "" |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
] |
| 296 |
proposed: |
| 297 |
description: The assembled configuration from the user-provided parameters |
| 298 |
returned: info |
| 299 |
type: dict |
| 300 |
sample: |
| 301 |
{ |
| 302 |
"fvTenant": { |
| 303 |
"attributes": { |
| 304 |
"descr": "Production environment", |
| 305 |
"name": "production" |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
filter_string: |
| 310 |
description: The filter string used for the request |
| 311 |
returned: failure or debug |
| 312 |
type: str |
| 313 |
sample: ?rsp-prop-include=config-only |
| 314 |
method: |
| 315 |
description: The HTTP method used for the request to the APIC |
| 316 |
returned: failure or debug |
| 317 |
type: str |
| 318 |
sample: POST |
| 319 |
response: |
| 320 |
description: The HTTP response from the APIC |
| 321 |
returned: failure or debug |
| 322 |
type: str |
| 323 |
sample: OK (30 bytes) |
| 324 |
status: |
| 325 |
description: The HTTP status from the APIC |
| 326 |
returned: failure or debug |
| 327 |
type: int |
| 328 |
sample: 200 |
| 329 |
url: |
| 330 |
description: The HTTP url used for the request to the APIC |
| 331 |
returned: failure or debug |
| 332 |
type: str |
| 333 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 334 |
""" |
| 335 |
|
| 336 |
from ansible.module_utils.basic import AnsibleModule |
| 337 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec, destination_epg_spec |
| 338 |
|
| 339 |
|
| 340 |
def access_interface_spec(): |
| 341 |
return dict( |
| 342 |
pod=dict(type="int", required=True, aliases=["pod_id", "pod_number"]), |
| 343 |
node=dict(type="int", required=True, aliases=["node_id"]), |
| 344 |
path=dict(type="str", required=True), |
| 345 |
mtu=dict(type="int"), |
| 346 |
) |
| 347 |
|
| 348 |
|
| 349 |
def main(): |
| 350 |
argument_spec = aci_argument_spec() |
| 351 |
argument_spec.update(aci_annotation_spec()) |
| 352 |
argument_spec.update(aci_owner_spec()) |
| 353 |
argument_spec.update( |
| 354 |
destination_group=dict(type="str", aliases=["name", "dst_group"]), # Not required for querying all objects |
| 355 |
description=dict(type="str", aliases=["descr"]), |
| 356 |
access_interface=dict(type="dict", options=access_interface_spec()), |
| 357 |
destination_epg=dict(type="dict", options=destination_epg_spec()), |
| 358 |
name_alias=dict(type="str"), |
| 359 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 360 |
) |
| 361 |
|
| 362 |
module = AnsibleModule( |
| 363 |
argument_spec=argument_spec, |
| 364 |
supports_check_mode=True, |
| 365 |
required_if=[ |
| 366 |
["state", "absent", ["destination_group"]], |
| 367 |
["state", "present", ["destination_group"]], |
| 368 |
["state", "present", ["access_interface", "destination_epg"], True], |
| 369 |
], |
| 370 |
mutually_exclusive=[ |
| 371 |
("access_interface", "destination_epg"), |
| 372 |
], |
| 373 |
) |
| 374 |
|
| 375 |
aci = ACIModule(module) |
| 376 |
|
| 377 |
destination_group = module.params.get("destination_group") |
| 378 |
description = module.params.get("description") |
| 379 |
access_interface = module.params.get("access_interface") |
| 380 |
destination_epg = module.params.get("destination_epg") |
| 381 |
state = module.params.get("state") |
| 382 |
name_alias = module.params.get("name_alias") |
| 383 |
|
| 384 |
aci.construct_url( |
| 385 |
root_class=dict( |
| 386 |
aci_class="infra", |
| 387 |
aci_rn="infra", |
| 388 |
), |
| 389 |
subclass_1=dict( |
| 390 |
aci_class="spanDestGrp", |
| 391 |
aci_rn="destgrp-{0}".format(destination_group), |
| 392 |
module_object=destination_group, |
| 393 |
target_filter={"name": destination_group}, |
| 394 |
), |
| 395 |
child_classes=["spanDest", "spanRsDestEpg", "spanRsDestPathEp"], |
| 396 |
) |
| 397 |
|
| 398 |
aci.get_existing() |
| 399 |
|
| 400 |
if state == "present": |
| 401 |
if destination_epg: |
| 402 |
attributes = dict( |
| 403 |
tDn="uni/tn-{0}/ap-{1}/epg-{2}".format(destination_epg.get("tenant"), destination_epg.get("ap"), destination_epg.get("epg")), |
| 404 |
ip=destination_epg.get("destination_ip"), |
| 405 |
srcIpPrefix=destination_epg.get("source_ip"), |
| 406 |
) |
| 407 |
if destination_epg.get("span_version") is not None: |
| 408 |
attributes["ver"] = "ver1" if destination_epg.get("span_version") == "version_1" else "ver2" |
| 409 |
if destination_epg.get("version_enforced") is not None: |
| 410 |
attributes["verEnforced"] = "yes" if destination_epg.get("version_enforced") else "no" |
| 411 |
if destination_epg.get("ttl") is not None: |
| 412 |
attributes["ttl"] = str(destination_epg.get("ttl")) |
| 413 |
if destination_epg.get("mtu") is not None: |
| 414 |
attributes["mtu"] = str(destination_epg.get("mtu")) |
| 415 |
if destination_epg.get("flow_id") is not None: |
| 416 |
attributes["flowId"] = str(destination_epg.get("flow_id")) |
| 417 |
if destination_epg.get("dscp") is not None: |
| 418 |
attributes["dscp"] = destination_epg.get("dscp") |
| 419 |
span_rs_dest = dict(spanRsDestEpg=dict(attributes=attributes)) |
| 420 |
|
| 421 |
else: |
| 422 |
attributes = dict( |
| 423 |
tDn="topology/pod-{0}/paths-{1}/pathep-[{2}]".format(access_interface.get("pod"), access_interface.get("node"), access_interface.get("path")) |
| 424 |
) |
| 425 |
if access_interface.get("mtu") is not None: |
| 426 |
attributes["mtu"] = str(access_interface.get("mtu")) |
| 427 |
span_rs_dest = dict(spanRsDestPathEp=dict(attributes=attributes)) |
| 428 |
|
| 429 |
aci.payload( |
| 430 |
aci_class="spanDestGrp", |
| 431 |
class_config=dict(name=destination_group, descr=description, nameAlias=name_alias), |
| 432 |
child_configs=[dict(spanDest=dict(attributes=dict(name=destination_group), children=[span_rs_dest]))], |
| 433 |
) |
| 434 |
|
| 435 |
aci.get_diff(aci_class="spanDestGrp") |
| 436 |
|
| 437 |
aci.post_config() |
| 438 |
|
| 439 |
elif state == "absent": |
| 440 |
aci.delete_config() |
| 441 |
|
| 442 |
aci.exit_json() |
| 443 |
|
| 444 |
|
| 445 |
if __name__ == "__main__": |
| 446 |
main() |