| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
--- |
| 14 |
module: aci_tenant_span_dst_group |
| 15 |
short_description: Manage SPAN destination groups (span:DestGrp) |
| 16 |
description: |
| 17 |
- Manage SPAN destination groups on Cisco ACI fabrics. |
| 18 |
options: |
| 19 |
destination_group: |
| 20 |
description: |
| 21 |
- The name of the SPAN destination group. |
| 22 |
type: str |
| 23 |
aliases: [ name, dst_group ] |
| 24 |
description: |
| 25 |
description: |
| 26 |
- The description of the SPAN destination group. |
| 27 |
type: str |
| 28 |
aliases: [ descr ] |
| 29 |
tenant: |
| 30 |
description: |
| 31 |
- The name of the tenant. |
| 32 |
type: str |
| 33 |
aliases: [ tenant_name ] |
| 34 |
destination_epg: |
| 35 |
description: |
| 36 |
- The destination end point group. |
| 37 |
type: dict |
| 38 |
suboptions: |
| 39 |
epg: |
| 40 |
description: |
| 41 |
- The name of the end point group. |
| 42 |
type: str |
| 43 |
ap: |
| 44 |
description: |
| 45 |
- The name of application profile. |
| 46 |
type: str |
| 47 |
tenant: |
| 48 |
description: |
| 49 |
- The name of the tenant. |
| 50 |
type: str |
| 51 |
aliases: [ tenant_name ] |
| 52 |
source_ip: |
| 53 |
description: |
| 54 |
- The source IP address or prefix. |
| 55 |
type: str |
| 56 |
destination_ip: |
| 57 |
description: |
| 58 |
- The destination IP address. |
| 59 |
type: str |
| 60 |
span_version: |
| 61 |
description: |
| 62 |
- SPAN version. |
| 63 |
type: str |
| 64 |
choices: [ version_1, version_2 ] |
| 65 |
flow_id: |
| 66 |
description: |
| 67 |
- The flow ID of the SPAN packet. |
| 68 |
type: int |
| 69 |
ttl: |
| 70 |
description: |
| 71 |
- The time to live of the span session packets. |
| 72 |
type: int |
| 73 |
mtu: |
| 74 |
description: |
| 75 |
- The MTU truncation size for the packets. |
| 76 |
type: int |
| 77 |
dscp: |
| 78 |
description: |
| 79 |
- The DSCP value for sending the monitored packets using ERSPAN. |
| 80 |
type: str |
| 81 |
choices: [ CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, unspecified ] |
| 82 |
version_enforced: |
| 83 |
description: |
| 84 |
- Enforce SPAN version. |
| 85 |
type: bool |
| 86 |
state: |
| 87 |
description: |
| 88 |
- Use C(present) or C(absent) for adding or removing. |
| 89 |
- Use C(query) for listing an object or multiple objects. |
| 90 |
type: str |
| 91 |
choices: [ absent, present, query ] |
| 92 |
default: present |
| 93 |
name_alias: |
| 94 |
description: |
| 95 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 96 |
type: str |
| 97 |
extends_documentation_fragment: |
| 98 |
- cisco.aci.aci |
| 99 |
- cisco.aci.annotation |
| 100 |
- cisco.aci.owner |
| 101 |
|
| 102 |
notes: |
| 103 |
- The C(tenant) used must exist before using this module in your playbook. |
| 104 |
The M(cisco.aci.aci_tenant) module can be used for this. |
| 105 |
seealso: |
| 106 |
- module: cisco.aci.aci_tenant |
| 107 |
- name: APIC Management Information Model reference |
| 108 |
description: More information about the internal APIC class B(span:DestGrp). |
| 109 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 110 |
author: |
| 111 |
- Dag Wieers (@dagwieers) |
| 112 |
- Shreyas Srish (@shrsr) |
| 113 |
""" |
| 114 |
|
| 115 |
EXAMPLES = r""" |
| 116 |
- name: Add SPAN destination group |
| 117 |
cisco.aci.aci_tenant_span_dst_group: |
| 118 |
host: apic |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
tenant: Tenant1 |
| 122 |
destination_epg: |
| 123 |
tenant: Test1 |
| 124 |
ap: ap1 |
| 125 |
epg: ep1 |
| 126 |
destination_group: group1 |
| 127 |
description: Test span |
| 128 |
destination_ip: 10.0.0.1 |
| 129 |
source_ip: 10.0.2.1 |
| 130 |
version_enforced: false |
| 131 |
span_version: version_1 |
| 132 |
ttl: 2 |
| 133 |
mtu: 1500 |
| 134 |
flow_id: 1 |
| 135 |
dscp: CS1 |
| 136 |
state: present |
| 137 |
delegate_to: localhost |
| 138 |
|
| 139 |
- name: Remove SPAN destination group |
| 140 |
cisco.aci.aci_tenant_span_dst_group: |
| 141 |
host: apic |
| 142 |
username: admin |
| 143 |
password: SomeSecretPassword |
| 144 |
tenant: Tenant1 |
| 145 |
destination_group: group1 |
| 146 |
state: absent |
| 147 |
delegate_to: localhost |
| 148 |
|
| 149 |
- name: Query SPAN destination group |
| 150 |
cisco.aci.aci_tenant_span_dst_group: |
| 151 |
host: apic |
| 152 |
username: admin |
| 153 |
password: SomeSecretPassword |
| 154 |
tenant: Tenant1 |
| 155 |
destination_group: group1 |
| 156 |
state: query |
| 157 |
delegate_to: localhost |
| 158 |
register: query_result |
| 159 |
|
| 160 |
- name: Query all SPAN destination groups |
| 161 |
cisco.aci.aci_tenant_span_dst_group: |
| 162 |
host: apic |
| 163 |
username: admin |
| 164 |
password: SomeSecretPassword |
| 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 |
|
| 278 |
|
| 279 |
def destination_epg_spec(): |
| 280 |
return dict( |
| 281 |
epg=dict(type="str"), |
| 282 |
ap=dict(type="str"), |
| 283 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 284 |
) |
| 285 |
|
| 286 |
|
| 287 |
def main(): |
| 288 |
argument_spec = aci_argument_spec() |
| 289 |
argument_spec.update(aci_annotation_spec()) |
| 290 |
argument_spec.update(aci_owner_spec()) |
| 291 |
argument_spec.update( |
| 292 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 293 |
destination_epg=dict(type="dict", options=destination_epg_spec()), |
| 294 |
destination_group=dict(type="str", aliases=["name", "dst_group"]), # Not required for querying all objects |
| 295 |
description=dict(type="str", aliases=["descr"]), |
| 296 |
name_alias=dict(type="str"), |
| 297 |
source_ip=dict(type="str"), |
| 298 |
destination_ip=dict(type="str"), |
| 299 |
mtu=dict(type="int"), |
| 300 |
ttl=dict(type="int"), |
| 301 |
flow_id=dict(type="int"), |
| 302 |
version_enforced=dict(type="bool"), |
| 303 |
span_version=dict(type="str", choices=["version_1", "version_2"]), |
| 304 |
dscp=dict( |
| 305 |
type="str", |
| 306 |
choices=[ |
| 307 |
"CS0", |
| 308 |
"CS1", |
| 309 |
"CS2", |
| 310 |
"CS3", |
| 311 |
"CS4", |
| 312 |
"CS5", |
| 313 |
"CS6", |
| 314 |
"CS7", |
| 315 |
"EF", |
| 316 |
"VA", |
| 317 |
"AF11", |
| 318 |
"AF12", |
| 319 |
"AF13", |
| 320 |
"AF21", |
| 321 |
"AF22", |
| 322 |
"AF23", |
| 323 |
"AF31", |
| 324 |
"AF32", |
| 325 |
"AF33", |
| 326 |
"AF41", |
| 327 |
"AF42", |
| 328 |
"AF43", |
| 329 |
"unspecified", |
| 330 |
], |
| 331 |
), |
| 332 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 333 |
) |
| 334 |
|
| 335 |
module = AnsibleModule( |
| 336 |
argument_spec=argument_spec, |
| 337 |
supports_check_mode=True, |
| 338 |
required_if=[ |
| 339 |
["state", "absent", ["destination_group", "tenant"]], |
| 340 |
["state", "present", ["destination_group", "destination_ip", "source_ip", "destination_epg", "tenant"]], |
| 341 |
], |
| 342 |
) |
| 343 |
|
| 344 |
aci = ACIModule(module) |
| 345 |
|
| 346 |
destination_epg = module.params.get("destination_epg") |
| 347 |
destination_group = module.params.get("destination_group") |
| 348 |
description = module.params.get("description") |
| 349 |
state = module.params.get("state") |
| 350 |
tenant = module.params.get("tenant") |
| 351 |
destination_ip = module.params.get("destination_ip") |
| 352 |
source_ip = module.params.get("source_ip") |
| 353 |
span_version = module.params.get("span_version") |
| 354 |
name_alias = module.params.get("name_alias") |
| 355 |
dscp = module.params.get("dscp") |
| 356 |
mtu = str(module.params.get("mtu")) |
| 357 |
ttl = str(module.params.get("ttl")) |
| 358 |
flow_id = str(module.params.get("flow_id")) |
| 359 |
version_enforced = module.params.get("version_enforced") |
| 360 |
|
| 361 |
aci.construct_url( |
| 362 |
root_class=dict( |
| 363 |
aci_class="fvTenant", |
| 364 |
aci_rn="tn-{0}".format(tenant), |
| 365 |
module_object=tenant, |
| 366 |
target_filter={"name": tenant}, |
| 367 |
), |
| 368 |
subclass_1=dict( |
| 369 |
aci_class="spanDestGrp", |
| 370 |
aci_rn="destgrp-{0}".format(destination_group), |
| 371 |
module_object=destination_group, |
| 372 |
target_filter={"name": destination_group}, |
| 373 |
), |
| 374 |
child_classes=["spanDest", "spanRsDestEpg"], |
| 375 |
) |
| 376 |
|
| 377 |
aci.get_existing() |
| 378 |
|
| 379 |
if state == "present": |
| 380 |
dest_tdn = "uni/tn-{0}/ap-{1}/epg-{2}".format(destination_epg["tenant"], destination_epg["ap"], destination_epg["epg"]) |
| 381 |
|
| 382 |
if version_enforced is True: |
| 383 |
version_enforced = "yes" |
| 384 |
else: |
| 385 |
version_enforced = "no" |
| 386 |
|
| 387 |
if span_version == "version_1": |
| 388 |
span_version = "ver1" |
| 389 |
else: |
| 390 |
span_version = "ver2" |
| 391 |
|
| 392 |
child_configs = [ |
| 393 |
dict( |
| 394 |
spanDest=dict( |
| 395 |
attributes=dict(name=destination_group), |
| 396 |
children=[ |
| 397 |
dict( |
| 398 |
spanRsDestEpg=dict( |
| 399 |
attributes=dict( |
| 400 |
ip=destination_ip, |
| 401 |
srcIpPrefix=source_ip, |
| 402 |
ver=span_version, |
| 403 |
verEnforced=version_enforced, |
| 404 |
ttl=ttl, |
| 405 |
mtu=mtu, |
| 406 |
flowId=flow_id, |
| 407 |
dscp=dscp, |
| 408 |
tDn=dest_tdn, |
| 409 |
) |
| 410 |
) |
| 411 |
) |
| 412 |
], |
| 413 |
) |
| 414 |
), |
| 415 |
] |
| 416 |
|
| 417 |
aci.payload( |
| 418 |
aci_class="spanDestGrp", |
| 419 |
class_config=dict( |
| 420 |
name=destination_group, |
| 421 |
descr=description, |
| 422 |
nameAlias=name_alias, |
| 423 |
), |
| 424 |
child_configs=child_configs, |
| 425 |
) |
| 426 |
|
| 427 |
aci.get_diff(aci_class="spanDestGrp") |
| 428 |
|
| 429 |
aci.post_config() |
| 430 |
|
| 431 |
elif state == "absent": |
| 432 |
aci.delete_config() |
| 433 |
|
| 434 |
aci.exit_json() |
| 435 |
|
| 436 |
|
| 437 |
if __name__ == "__main__": |
| 438 |
main() |