| 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_src_group_src |
| 16 |
short_description: Manage Access SPAN sources (span:Src) |
| 17 |
description: |
| 18 |
- Manage Access SPAN sources on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
description: |
| 21 |
description: |
| 22 |
- The description for Access SPAN source. |
| 23 |
type: str |
| 24 |
aliases: [ descr ] |
| 25 |
source_group: |
| 26 |
description: |
| 27 |
- The name of the Access SPAN source group. |
| 28 |
type: str |
| 29 |
aliases: [ src_group ] |
| 30 |
source: |
| 31 |
description: |
| 32 |
- The name of the Access SPAN source. |
| 33 |
type: str |
| 34 |
aliases: [ name, src ] |
| 35 |
direction: |
| 36 |
description: |
| 37 |
- The direction of the SPAN source. |
| 38 |
- The APIC defaults to C(both) when unset during creation. |
| 39 |
type: str |
| 40 |
choices: [ incoming, outgoing, both ] |
| 41 |
filter_group: |
| 42 |
description: |
| 43 |
- The name of the Access SPAN filter group to associate with the source. |
| 44 |
type: str |
| 45 |
drop_packets: |
| 46 |
description: |
| 47 |
- Enable SPAN for only dropped packets. |
| 48 |
- The APIC defaults to C(false) when unset during creation. |
| 49 |
type: bool |
| 50 |
epg: |
| 51 |
description: |
| 52 |
- The SPAN source EPG details. |
| 53 |
- The I(epg) and I(routed_outside) cannot be configured simultaneously. |
| 54 |
type: dict |
| 55 |
suboptions: |
| 56 |
tenant: |
| 57 |
description: |
| 58 |
- The name of the SPAN source Tenant. |
| 59 |
type: str |
| 60 |
required: true |
| 61 |
aliases: [ tenant_name ] |
| 62 |
ap: |
| 63 |
description: |
| 64 |
- The name of the SPAN source AP. |
| 65 |
type: str |
| 66 |
required: true |
| 67 |
aliases: [ ap_name, app_profile, app_profile_name ] |
| 68 |
epg: |
| 69 |
description: |
| 70 |
- The name of the SPAN source EPG. |
| 71 |
type: str |
| 72 |
required: true |
| 73 |
aliases: [ epg_name ] |
| 74 |
routed_outside: |
| 75 |
description: |
| 76 |
- The Routed Outside details. |
| 77 |
- The I(epg) and I(routed_outside) cannot be configured simultaneously. |
| 78 |
type: dict |
| 79 |
suboptions: |
| 80 |
tenant: |
| 81 |
description: |
| 82 |
- The name of the SPAN source Tenant. |
| 83 |
type: str |
| 84 |
aliases: [ tenant_name ] |
| 85 |
l3out: |
| 86 |
description: |
| 87 |
- The name of the SPAN source L3Out. |
| 88 |
type: str |
| 89 |
aliases: [ l3out_name ] |
| 90 |
encap: |
| 91 |
description: |
| 92 |
- The VLAN associated with this Routed Outside. |
| 93 |
type: int |
| 94 |
aliases: [ vlan, vlan_id, encap_id ] |
| 95 |
state: |
| 96 |
description: |
| 97 |
- Use C(present) or C(absent) for adding or removing. |
| 98 |
- Use C(query) for listing an object or multiple objects. |
| 99 |
type: str |
| 100 |
choices: [ absent, present, query ] |
| 101 |
default: present |
| 102 |
name_alias: |
| 103 |
description: |
| 104 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 105 |
type: str |
| 106 |
extends_documentation_fragment: |
| 107 |
- cisco.aci.aci |
| 108 |
- cisco.aci.annotation |
| 109 |
- cisco.aci.owner |
| 110 |
|
| 111 |
notes: |
| 112 |
- The I(filter_group) and I(source_group) must exist before using this module in your playbook. |
| 113 |
The M(cisco.aci.aci_access_span_filter_group) and M(cisco.aci.aci_access_span_src_group) modules can be used for this. |
| 114 |
seealso: |
| 115 |
- module: cisco.aci.aci_access_span_filter_group |
| 116 |
- module: cisco.aci.aci_access_span_src_group |
| 117 |
- module: cisco.aci.aci_tenant |
| 118 |
- module: cisco.aci.aci_ap |
| 119 |
- module: cisco.aci.aci_epg |
| 120 |
- module: cisco.aci.aci_l3out |
| 121 |
- name: APIC Management Information Model reference |
| 122 |
description: More information about the internal APIC class B(span:Src). |
| 123 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 124 |
author: |
| 125 |
- Akini Ross (@akinross) |
| 126 |
""" |
| 127 |
|
| 128 |
EXAMPLES = r""" |
| 129 |
- name: Create a Access SPAN source |
| 130 |
cisco.aci.aci_access_span_src_group_src: |
| 131 |
host: apic |
| 132 |
username: admin |
| 133 |
password: SomeSecretPassword |
| 134 |
source_group: my_span_source_group |
| 135 |
source: my_source |
| 136 |
state: present |
| 137 |
delegate_to: localhost |
| 138 |
|
| 139 |
- name: Delete a Access SPAN source |
| 140 |
cisco.aci.aci_access_span_src_group_src: |
| 141 |
host: apic |
| 142 |
username: admin |
| 143 |
password: SomeSecretPassword |
| 144 |
source_group: my_span_source_group |
| 145 |
source: my_source |
| 146 |
state: absent |
| 147 |
delegate_to: localhost |
| 148 |
|
| 149 |
- name: Query all Access SPAN sources |
| 150 |
cisco.aci.aci_access_span_src_group_src: |
| 151 |
host: apic |
| 152 |
username: admin |
| 153 |
password: SomeSecretPassword |
| 154 |
state: query |
| 155 |
delegate_to: localhost |
| 156 |
register: query_result |
| 157 |
|
| 158 |
- name: Query a specific Access SPAN source |
| 159 |
cisco.aci.aci_access_span_src_group_src: |
| 160 |
host: apic |
| 161 |
username: admin |
| 162 |
password: SomeSecretPassword |
| 163 |
source_group: my_span_source_group |
| 164 |
source: my_source |
| 165 |
state: query |
| 166 |
delegate_to: localhost |
| 167 |
register: query_result |
| 168 |
""" |
| 169 |
|
| 170 |
RETURN = r""" |
| 171 |
current: |
| 172 |
description: The existing configuration from the APIC after the module has finished |
| 173 |
returned: success |
| 174 |
type: list |
| 175 |
sample: |
| 176 |
[ |
| 177 |
{ |
| 178 |
"fvTenant": { |
| 179 |
"attributes": { |
| 180 |
"descr": "Production environment", |
| 181 |
"dn": "uni/tn-production", |
| 182 |
"name": "production", |
| 183 |
"nameAlias": "", |
| 184 |
"ownerKey": "", |
| 185 |
"ownerTag": "" |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
] |
| 190 |
error: |
| 191 |
description: The error information as returned from the APIC |
| 192 |
returned: failure |
| 193 |
type: dict |
| 194 |
sample: |
| 195 |
{ |
| 196 |
"code": "122", |
| 197 |
"text": "unknown managed object class foo" |
| 198 |
} |
| 199 |
raw: |
| 200 |
description: The raw output returned by the APIC REST API (xml or json) |
| 201 |
returned: parse error |
| 202 |
type: str |
| 203 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 204 |
sent: |
| 205 |
description: The actual/minimal configuration pushed to the APIC |
| 206 |
returned: info |
| 207 |
type: list |
| 208 |
sample: |
| 209 |
{ |
| 210 |
"fvTenant": { |
| 211 |
"attributes": { |
| 212 |
"descr": "Production environment" |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
previous: |
| 217 |
description: The original configuration from the APIC before the module has started |
| 218 |
returned: info |
| 219 |
type: list |
| 220 |
sample: |
| 221 |
[ |
| 222 |
{ |
| 223 |
"fvTenant": { |
| 224 |
"attributes": { |
| 225 |
"descr": "Production", |
| 226 |
"dn": "uni/tn-production", |
| 227 |
"name": "production", |
| 228 |
"nameAlias": "", |
| 229 |
"ownerKey": "", |
| 230 |
"ownerTag": "" |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
] |
| 235 |
proposed: |
| 236 |
description: The assembled configuration from the user-provided parameters |
| 237 |
returned: info |
| 238 |
type: dict |
| 239 |
sample: |
| 240 |
{ |
| 241 |
"fvTenant": { |
| 242 |
"attributes": { |
| 243 |
"descr": "Production environment", |
| 244 |
"name": "production" |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
filter_string: |
| 249 |
description: The filter string used for the request |
| 250 |
returned: failure or debug |
| 251 |
type: str |
| 252 |
sample: ?rsp-prop-include=config-only |
| 253 |
method: |
| 254 |
description: The HTTP method used for the request to the APIC |
| 255 |
returned: failure or debug |
| 256 |
type: str |
| 257 |
sample: POST |
| 258 |
response: |
| 259 |
description: The HTTP response from the APIC |
| 260 |
returned: failure or debug |
| 261 |
type: str |
| 262 |
sample: OK (30 bytes) |
| 263 |
status: |
| 264 |
description: The HTTP status from the APIC |
| 265 |
returned: failure or debug |
| 266 |
type: int |
| 267 |
sample: 200 |
| 268 |
url: |
| 269 |
description: The HTTP url used for the request to the APIC |
| 270 |
returned: failure or debug |
| 271 |
type: str |
| 272 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 273 |
""" |
| 274 |
|
| 275 |
from ansible.module_utils.basic import AnsibleModule |
| 276 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 277 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import SPAN_DIRECTION_MAP |
| 278 |
|
| 279 |
|
| 280 |
def main(): |
| 281 |
argument_spec = aci_argument_spec() |
| 282 |
argument_spec.update(aci_annotation_spec()) |
| 283 |
argument_spec.update(aci_owner_spec()) |
| 284 |
argument_spec.update( |
| 285 |
source_group=dict(type="str", aliases=["src_group"]), # Not required for querying all objects |
| 286 |
description=dict(type="str", aliases=["descr"]), |
| 287 |
source=dict(type="str", aliases=["name", "src"]), # Not required for querying all objects |
| 288 |
direction=dict(type="str", choices=list(SPAN_DIRECTION_MAP.keys())), |
| 289 |
filter_group=dict(type="str"), |
| 290 |
drop_packets=dict(type="bool"), |
| 291 |
epg=dict( |
| 292 |
type="dict", |
| 293 |
options=dict( |
| 294 |
epg=dict(type="str", required=True, aliases=["epg_name"]), |
| 295 |
ap=dict(type="str", required=True, aliases=["ap_name", "app_profile", "app_profile_name"]), |
| 296 |
tenant=dict(type="str", required=True, aliases=["tenant_name"]), |
| 297 |
), |
| 298 |
), |
| 299 |
routed_outside=dict( |
| 300 |
type="dict", |
| 301 |
options=dict( |
| 302 |
encap=dict(type="int", aliases=["vlan", "vlan_id", "encap_id"]), |
| 303 |
l3out=dict(type="str", aliases=["l3out_name"]), |
| 304 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 305 |
), |
| 306 |
), |
| 307 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 308 |
name_alias=dict(type="str"), |
| 309 |
) |
| 310 |
|
| 311 |
module = AnsibleModule( |
| 312 |
argument_spec=argument_spec, |
| 313 |
supports_check_mode=True, |
| 314 |
required_if=[ |
| 315 |
["state", "absent", ["source_group", "source"]], |
| 316 |
["state", "present", ["source_group", "source"]], |
| 317 |
], |
| 318 |
mutually_exclusive=[ |
| 319 |
("epg", "routed_outside"), |
| 320 |
], |
| 321 |
) |
| 322 |
|
| 323 |
aci = ACIModule(module) |
| 324 |
|
| 325 |
description = module.params.get("description") |
| 326 |
source_group = module.params.get("source_group") |
| 327 |
source = module.params.get("source") |
| 328 |
direction = module.params.get("direction") |
| 329 |
filter_group = module.params.get("filter_group") |
| 330 |
drop_packets = module.params.get("drop_packets") |
| 331 |
epg = module.params.get("epg") |
| 332 |
routed_outside = module.params.get("routed_outside") |
| 333 |
state = module.params.get("state") |
| 334 |
name_alias = module.params.get("name_alias") |
| 335 |
|
| 336 |
if filter_group and drop_packets: |
| 337 |
module.fail_json(msg="Setting 'drop_packets' to 'true' is not allowed when 'filter_group' is configured on the source.") |
| 338 |
elif epg and drop_packets: |
| 339 |
module.fail_json(msg="Setting 'drop_packets' to 'true' is not allowed when 'epg' is configured on the source.") |
| 340 |
elif routed_outside and drop_packets: |
| 341 |
module.fail_json(msg="Setting 'drop_packets' to 'true' is not allowed when 'routed_outside' is configured on the source.") |
| 342 |
|
| 343 |
aci.construct_url( |
| 344 |
root_class=dict( |
| 345 |
aci_class="infra", |
| 346 |
aci_rn="infra", |
| 347 |
), |
| 348 |
subclass_1=dict( |
| 349 |
aci_class="spanSrcGrp", |
| 350 |
aci_rn="srcgrp-{0}".format(source_group), |
| 351 |
module_object=source_group, |
| 352 |
target_filter={"name": source_group}, |
| 353 |
), |
| 354 |
subclass_2=dict( |
| 355 |
aci_class="spanSrc", |
| 356 |
aci_rn="src-{0}".format(source), |
| 357 |
module_object=source, |
| 358 |
target_filter={"name": source}, |
| 359 |
), |
| 360 |
child_classes=["spanRsSrcToFilterGrp", "spanRsSrcToEpg", "spanRsSrcToL3extOut"], |
| 361 |
) |
| 362 |
|
| 363 |
aci.get_existing() |
| 364 |
|
| 365 |
if state == "present": |
| 366 |
# Create new child configs payload |
| 367 |
child_configs = [] |
| 368 |
filter_group_tdn = epg_dn = l3ext_out_dn = None |
| 369 |
|
| 370 |
if filter_group: |
| 371 |
filter_group_tdn = "uni/infra/filtergrp-{0}".format(filter_group) |
| 372 |
child_configs.append({"spanRsSrcToFilterGrp": {"attributes": {"tDn": filter_group_tdn}}}) |
| 373 |
if epg: |
| 374 |
epg_dn = "uni/tn-{0}/ap-{1}/epg-{2}".format(epg.get("tenant"), epg.get("ap"), epg.get("epg")) |
| 375 |
child_configs.append({"spanRsSrcToEpg": {"attributes": {"tDn": epg_dn}}}) |
| 376 |
elif routed_outside: |
| 377 |
# encap is set to unknown when not provided to ensure change is executed and detected properly on update |
| 378 |
encap = "vlan-{0}".format(routed_outside.get("encap")) if routed_outside.get("encap") else "unknown" |
| 379 |
if routed_outside.get("tenant") and routed_outside.get("l3out"): |
| 380 |
l3ext_out_dn = "uni/tn-{0}/out-{1}".format(routed_outside.get("tenant"), routed_outside.get("l3out")) |
| 381 |
else: |
| 382 |
# tDn is set to "" when not provided to ensure change is executed and detected properly on update |
| 383 |
l3ext_out_dn = "" |
| 384 |
child_configs.append({"spanRsSrcToL3extOut": {"attributes": {"encap": encap, "tDn": l3ext_out_dn}}}) |
| 385 |
|
| 386 |
# Validate if existing and remove child objects when do not match provided configuration |
| 387 |
if isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 388 |
# Commented validate code to avoid making additional API request which is handled by APIC error |
| 389 |
# Keeping for informational purposes |
| 390 |
# Validate drop_packets are set on parent correctly |
| 391 |
# if aci.api_call("GET", "{0}/rssrcGrpToFilterGrp.json".format(source_group_path)) != [] and drop_packets: |
| 392 |
# module.fail_json(msg="It is not allowed to configure 'drop_packets: true' when a filter group is configured on the source group.") |
| 393 |
|
| 394 |
source_path = "/api/mo/uni/infra/srcgrp-{0}/src-{1}".format(source_group, source) |
| 395 |
for child in aci.existing[0].get("spanSrc", {}).get("children", {}): |
| 396 |
if child.get("spanRsSrcToFilterGrp") and child.get("spanRsSrcToFilterGrp").get("attributes").get("tDn") != filter_group_tdn: |
| 397 |
# Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcGrpToFilterGrp is already attached. |
| 398 |
# A seperate delete request to dn of the spanRsSrcGrpToFilterGrp is needed to remove the object prior to adding to child_configs. |
| 399 |
# Failed child_config is displayed in below: |
| 400 |
# |
| 401 |
# child_configs.append( |
| 402 |
# { |
| 403 |
# "spanRsSrcGrpToFilterGrp": { |
| 404 |
# "attributes": { |
| 405 |
# "dn": "uni/infra/srcgrp-{0}/src-{1}/rssrcGrpToFilterGrp".format(source_group, source), |
| 406 |
# "status": "deleted", |
| 407 |
# } |
| 408 |
# } |
| 409 |
# } |
| 410 |
# ) |
| 411 |
aci.api_call("DELETE", "{0}/rssrcToFilterGrp.json".format(source_path)) |
| 412 |
elif child.get("spanRsSrcToEpg") and child.get("spanRsSrcToEpg").get("attributes").get("tDn") != epg_dn: |
| 413 |
# Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcToEpg is already attached. |
| 414 |
aci.api_call("DELETE", "{0}/rssrcToEpg.json".format(source_path)) |
| 415 |
elif child.get("spanRsSrcToL3extOut") and child.get("spanRsSrcToL3extOut").get("attributes").get("tDn") != l3ext_out_dn: |
| 416 |
# Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcToL3extOut is already attached. |
| 417 |
aci.api_call("DELETE", "{0}/rssrcToL3extOut.json".format(source_path)) |
| 418 |
|
| 419 |
aci.payload( |
| 420 |
aci_class="spanSrc", |
| 421 |
class_config=dict( |
| 422 |
descr=description, |
| 423 |
name=source, |
| 424 |
dir=SPAN_DIRECTION_MAP.get(direction), |
| 425 |
spanOnDrop=aci.boolean(drop_packets), |
| 426 |
nameAlias=name_alias, |
| 427 |
), |
| 428 |
child_configs=child_configs, |
| 429 |
) |
| 430 |
|
| 431 |
aci.get_diff(aci_class="spanSrc") |
| 432 |
|
| 433 |
aci.post_config() |
| 434 |
|
| 435 |
elif state == "absent": |
| 436 |
aci.delete_config() |
| 437 |
|
| 438 |
aci.exit_json() |
| 439 |
|
| 440 |
|
| 441 |
if __name__ == "__main__": |
| 442 |
main() |