| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Tim Cragg (@timcragg) |
| 5 |
# Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]> |
| 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: aci_l3out_interface |
| 17 |
short_description: Manage Layer 3 Outside (L3Out) interfaces (l3ext:RsPathL3OutAtt) |
| 18 |
description: |
| 19 |
- Manage L3Out interfaces on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- Name of an existing tenant. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
required: true |
| 27 |
l3out: |
| 28 |
description: |
| 29 |
- Name of an existing L3Out. |
| 30 |
type: str |
| 31 |
aliases: [ l3out_name ] |
| 32 |
required: true |
| 33 |
node_profile: |
| 34 |
description: |
| 35 |
- Name of the node profile. |
| 36 |
type: str |
| 37 |
aliases: [ node_profile_name, logical_node ] |
| 38 |
required: true |
| 39 |
interface_profile: |
| 40 |
description: |
| 41 |
- Name of the interface profile. |
| 42 |
type: str |
| 43 |
aliases: [ interface_profile_name, logical_interface ] |
| 44 |
required: true |
| 45 |
pod_id: |
| 46 |
description: |
| 47 |
- Pod to build the interface on. |
| 48 |
type: str |
| 49 |
node_id: |
| 50 |
description: |
| 51 |
- Node to build the interface on for Port-channels and single ports. |
| 52 |
- Hyphen separated pair of nodes (e.g. "201-202") for vPCs. |
| 53 |
type: str |
| 54 |
path_ep: |
| 55 |
description: |
| 56 |
- Path to interface. |
| 57 |
- Interface Policy Group name for Port-channels and vPCs. |
| 58 |
- Port number for single ports (e.g. "eth1/12"). |
| 59 |
type: str |
| 60 |
encap: |
| 61 |
description: |
| 62 |
- The encapsulation on the interface (e.g. "vlan-500"). |
| 63 |
type: str |
| 64 |
encap_scope: |
| 65 |
description: |
| 66 |
- The scope of the encapsulation on the interface. |
| 67 |
type: str |
| 68 |
choices: [ vrf, local ] |
| 69 |
address: |
| 70 |
description: |
| 71 |
- IP address. |
| 72 |
type: str |
| 73 |
aliases: [ addr, ip_address] |
| 74 |
mtu: |
| 75 |
description: |
| 76 |
- Interface MTU. |
| 77 |
type: str |
| 78 |
ipv6_dad: |
| 79 |
description: |
| 80 |
- IPv6 DAD feature. |
| 81 |
type: str |
| 82 |
choices: [ enabled, disabled] |
| 83 |
interface_type: |
| 84 |
description: |
| 85 |
- Type of interface to build. |
| 86 |
type: str |
| 87 |
choices: [ l3-port, sub-interface, ext-svi ] |
| 88 |
mode: |
| 89 |
description: |
| 90 |
- Interface mode, only used if instance_type is ext-svi. |
| 91 |
type: str |
| 92 |
choices: [ regular, native, untagged ] |
| 93 |
state: |
| 94 |
description: |
| 95 |
- Use C(present) or C(absent) for adding or removing. |
| 96 |
- Use C(query) for listing an object or multiple objects. |
| 97 |
type: str |
| 98 |
choices: [ absent, present, query ] |
| 99 |
default: present |
| 100 |
auto_state: |
| 101 |
description: |
| 102 |
- SVI auto state. |
| 103 |
type: str |
| 104 |
choices: [ enabled, disabled ] |
| 105 |
description: |
| 106 |
description: |
| 107 |
- The description of the interface. |
| 108 |
type: str |
| 109 |
aliases: [ descr] |
| 110 |
mac: |
| 111 |
description: |
| 112 |
- The MAC address of the interface. |
| 113 |
type: str |
| 114 |
aliases: [ mac_address ] |
| 115 |
micro_bfd: |
| 116 |
description: |
| 117 |
- Enable micro BFD on the interface. |
| 118 |
- Micro BFD should only be configured on Infra SR-MPLS L3Outs Direct Port Channel Interfaces. |
| 119 |
type: bool |
| 120 |
micro_bfd_destination: |
| 121 |
description: |
| 122 |
- The micro BFD destination address of the interface. |
| 123 |
type: str |
| 124 |
aliases: [ micro_bfd_address, micro_bfd_destination_address ] |
| 125 |
micro_bfd_timer: |
| 126 |
description: |
| 127 |
- The micro BFD start timer in seconds. |
| 128 |
- The APIC defaults to C(0) when unset during creation. |
| 129 |
type: int |
| 130 |
aliases: [ micro_bfd_start_timer, micro_bfd_start ] |
| 131 |
extends_documentation_fragment: |
| 132 |
- cisco.aci.aci |
| 133 |
- cisco.aci.annotation |
| 134 |
|
| 135 |
notes: |
| 136 |
- The C(tenant), C(l3out), C(node_profile) and the C(interface_profile) must exist before using this module in your playbook. |
| 137 |
The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l3out), M(cisco.aci.aci_l3out_logical_node_profile) and |
| 138 |
M(cisco.aci.aci_l3out_logical_interface_profile) can be used for this. |
| 139 |
seealso: |
| 140 |
- module: cisco.aci.aci_tenant |
| 141 |
- module: cisco.aci.aci_l3out |
| 142 |
- module: cisco.aci.aci_l3out_logical_node_profile |
| 143 |
- module: cisco.aci.aci_l3out_logical_interface_profile |
| 144 |
- name: APIC Management Information Model reference |
| 145 |
description: More information about the internal APIC class B(l3ext:RsPathL3OutAtt) |
| 146 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 147 |
author: |
| 148 |
- Tim Cragg (@timcragg) |
| 149 |
- Akini Ross (@akinross) |
| 150 |
""" |
| 151 |
|
| 152 |
EXAMPLES = r""" |
| 153 |
- name: Add a new routed interface |
| 154 |
cisco.aci.aci_l3out_interface: |
| 155 |
host: apic |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
tenant: my_tenant |
| 159 |
l3out: my_l3out |
| 160 |
node_profile: my_node_profile |
| 161 |
interface_profile: my_interface_profile |
| 162 |
pod_id: 1 |
| 163 |
node_id: 201 |
| 164 |
path_ep: eth1/12 |
| 165 |
interface_type: l3-port |
| 166 |
address: 192.168.10.1/27 |
| 167 |
state: present |
| 168 |
delegate_to: localhost |
| 169 |
|
| 170 |
- name: Add a new SVI vPC |
| 171 |
cisco.aci.aci_l3out_interface: |
| 172 |
host: apic |
| 173 |
username: admin |
| 174 |
password: SomeSecretPassword |
| 175 |
tenant: my_tenant |
| 176 |
l3out: my_l3out |
| 177 |
node_profile: my_node_profile |
| 178 |
interface_profile: my_interface_profile |
| 179 |
pod_id: 1 |
| 180 |
node_id: 201-202 |
| 181 |
path_ep: my_vpc_ipg |
| 182 |
interface_type: ext-svi |
| 183 |
encap: vlan-800 |
| 184 |
mode: regular |
| 185 |
state: present |
| 186 |
delegate_to: localhost |
| 187 |
|
| 188 |
- name: Add direct port channel interface in the infra SR-MPLS l3out interface profile with micro BFD enabled |
| 189 |
aci_l3out_interface: |
| 190 |
host: apic |
| 191 |
username: admin |
| 192 |
password: SomeSecretPassword |
| 193 |
tenant: infra |
| 194 |
l3out: ansible_infra_sr_mpls_l3out |
| 195 |
interface_profile: ansible_infra_sr_mpls_l3out_interface_profile |
| 196 |
pod_id: 1 |
| 197 |
node_id: 101 |
| 198 |
path_ep: pc_ansible_test |
| 199 |
interface_type: l3-port |
| 200 |
addr: 192.168.90.1/24 |
| 201 |
micro_bfd: true |
| 202 |
micro_bfd_destination: 192.168.90.2 |
| 203 |
micro_bfd_timer: 75 |
| 204 |
state: present |
| 205 |
delegate_to: localhost |
| 206 |
|
| 207 |
- name: Delete an interface |
| 208 |
cisco.aci.aci_l3out_interface: |
| 209 |
host: apic |
| 210 |
username: admin |
| 211 |
password: SomeSecretPassword |
| 212 |
tenant: my_tenant |
| 213 |
l3out: my_l3out |
| 214 |
node_profile: my_node_profile |
| 215 |
interface_profile: my_interface_profile |
| 216 |
pod_id: 1 |
| 217 |
node_id: 201 |
| 218 |
path_ep: eth1/12 |
| 219 |
state: absent |
| 220 |
delegate_to: localhost |
| 221 |
|
| 222 |
- name: Query an interface |
| 223 |
cisco.aci.aci_l3out_interface: |
| 224 |
host: apic |
| 225 |
username: admin |
| 226 |
password: SomeSecretPassword |
| 227 |
tenant: my_tenant |
| 228 |
l3out: my_l3out |
| 229 |
node_profile: my_node_profile |
| 230 |
interface_profile: my_interface_profile |
| 231 |
pod_id: 1 |
| 232 |
node_id: 201 |
| 233 |
path_ep: eth1/12 |
| 234 |
state: query |
| 235 |
delegate_to: localhost |
| 236 |
register: query_result |
| 237 |
""" |
| 238 |
|
| 239 |
RETURN = r""" |
| 240 |
current: |
| 241 |
description: The existing configuration from the APIC after the module has finished |
| 242 |
returned: success |
| 243 |
type: list |
| 244 |
sample: |
| 245 |
[ |
| 246 |
{ |
| 247 |
"fvTenant": { |
| 248 |
"attributes": { |
| 249 |
"descr": "Production environment", |
| 250 |
"dn": "uni/tn-production", |
| 251 |
"name": "production", |
| 252 |
"nameAlias": "", |
| 253 |
"ownerKey": "", |
| 254 |
"ownerTag": "" |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
] |
| 259 |
error: |
| 260 |
description: The error information as returned from the APIC |
| 261 |
returned: failure |
| 262 |
type: dict |
| 263 |
sample: |
| 264 |
{ |
| 265 |
"code": "122", |
| 266 |
"text": "unknown managed object class foo" |
| 267 |
} |
| 268 |
raw: |
| 269 |
description: The raw output returned by the APIC REST API (xml or json) |
| 270 |
returned: parse error |
| 271 |
type: str |
| 272 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 273 |
sent: |
| 274 |
description: The actual/minimal configuration pushed to the APIC |
| 275 |
returned: info |
| 276 |
type: list |
| 277 |
sample: |
| 278 |
{ |
| 279 |
"fvTenant": { |
| 280 |
"attributes": { |
| 281 |
"descr": "Production environment" |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
previous: |
| 286 |
description: The original configuration from the APIC before the module has started |
| 287 |
returned: info |
| 288 |
type: list |
| 289 |
sample: |
| 290 |
[ |
| 291 |
{ |
| 292 |
"fvTenant": { |
| 293 |
"attributes": { |
| 294 |
"descr": "Production", |
| 295 |
"dn": "uni/tn-production", |
| 296 |
"name": "production", |
| 297 |
"nameAlias": "", |
| 298 |
"ownerKey": "", |
| 299 |
"ownerTag": "" |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
] |
| 304 |
proposed: |
| 305 |
description: The assembled configuration from the user-provided parameters |
| 306 |
returned: info |
| 307 |
type: dict |
| 308 |
sample: |
| 309 |
{ |
| 310 |
"fvTenant": { |
| 311 |
"attributes": { |
| 312 |
"descr": "Production environment", |
| 313 |
"name": "production" |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
filter_string: |
| 318 |
description: The filter string used for the request |
| 319 |
returned: failure or debug |
| 320 |
type: str |
| 321 |
sample: ?rsp-prop-include=config-only |
| 322 |
method: |
| 323 |
description: The HTTP method used for the request to the APIC |
| 324 |
returned: failure or debug |
| 325 |
type: str |
| 326 |
sample: POST |
| 327 |
response: |
| 328 |
description: The HTTP response from the APIC |
| 329 |
returned: failure or debug |
| 330 |
type: str |
| 331 |
sample: OK (30 bytes) |
| 332 |
status: |
| 333 |
description: The HTTP status from the APIC |
| 334 |
returned: failure or debug |
| 335 |
type: int |
| 336 |
sample: 200 |
| 337 |
url: |
| 338 |
description: The HTTP url used for the request to the APIC |
| 339 |
returned: failure or debug |
| 340 |
type: str |
| 341 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 342 |
""" |
| 343 |
|
| 344 |
|
| 345 |
from ansible.module_utils.basic import AnsibleModule |
| 346 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 347 |
|
| 348 |
|
| 349 |
def main(): |
| 350 |
argument_spec = aci_argument_spec() |
| 351 |
argument_spec.update(aci_annotation_spec()) |
| 352 |
argument_spec.update( |
| 353 |
tenant=dict(type="str", aliases=["tenant_name"], required=True), |
| 354 |
l3out=dict(type="str", aliases=["l3out_name"], required=True), |
| 355 |
node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"], required=True), |
| 356 |
interface_profile=dict(type="str", aliases=["interface_profile_name", "logical_interface"], required=True), |
| 357 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 358 |
pod_id=dict(type="str"), |
| 359 |
node_id=dict(type="str"), |
| 360 |
path_ep=dict(type="str"), |
| 361 |
address=dict(type="str", aliases=["addr", "ip_address"]), |
| 362 |
mtu=dict(type="str"), |
| 363 |
ipv6_dad=dict(type="str", choices=["enabled", "disabled"]), |
| 364 |
interface_type=dict(type="str", choices=["l3-port", "sub-interface", "ext-svi"]), |
| 365 |
mode=dict(type="str", choices=["regular", "native", "untagged"]), |
| 366 |
encap=dict(type="str"), |
| 367 |
encap_scope=dict(type="str", choices=["vrf", "local"]), |
| 368 |
auto_state=dict(type="str", choices=["enabled", "disabled"]), |
| 369 |
description=dict(type="str", aliases=["descr"]), |
| 370 |
mac=dict(type="str", aliases=["mac_address"]), |
| 371 |
micro_bfd=dict(type="bool"), |
| 372 |
micro_bfd_destination=dict(type="str", aliases=["micro_bfd_address", "micro_bfd_destination_address"]), |
| 373 |
micro_bfd_timer=dict(type="int", aliases=["micro_bfd_start_timer", "micro_bfd_start"]), |
| 374 |
) |
| 375 |
|
| 376 |
module = AnsibleModule( |
| 377 |
argument_spec=argument_spec, |
| 378 |
supports_check_mode=True, |
| 379 |
required_if=[ |
| 380 |
["state", "present", ["interface_type", "pod_id", "node_id", "path_ep"]], |
| 381 |
["state", "absent", ["pod_id", "node_id", "path_ep"]], |
| 382 |
["micro_bfd", True, ["micro_bfd_destination"]], |
| 383 |
], |
| 384 |
required_by={"micro_bfd_timer": "micro_bfd", "micro_bfd_destination": "micro_bfd"}, |
| 385 |
) |
| 386 |
|
| 387 |
aci = ACIModule(module) |
| 388 |
|
| 389 |
tenant = module.params.get("tenant") |
| 390 |
l3out = module.params.get("l3out") |
| 391 |
node_profile = module.params.get("node_profile") |
| 392 |
interface_profile = module.params.get("interface_profile") |
| 393 |
state = module.params.get("state") |
| 394 |
pod_id = module.params.get("pod_id") |
| 395 |
node_id = module.params.get("node_id") |
| 396 |
path_ep = module.params.get("path_ep") |
| 397 |
address = module.params.get("address") |
| 398 |
mtu = module.params.get("mtu") |
| 399 |
ipv6_dad = module.params.get("ipv6_dad") |
| 400 |
interface_type = module.params.get("interface_type") |
| 401 |
mode = module.params.get("mode") |
| 402 |
encap = module.params.get("encap") |
| 403 |
encap_scope = module.params.get("encap_scope") |
| 404 |
auto_state = module.params.get("auto_state") |
| 405 |
description = module.params.get("description") |
| 406 |
mac = module.params.get("mac") |
| 407 |
micro_bfd = aci.boolean(module.params.get("micro_bfd")) |
| 408 |
micro_bfd_destination = module.params.get("micro_bfd_destination") |
| 409 |
micro_bfd_timer = module.params.get("micro_bfd_timer") |
| 410 |
|
| 411 |
if node_id and "-" in node_id: |
| 412 |
path_type = "protpaths" |
| 413 |
else: |
| 414 |
path_type = "paths" |
| 415 |
|
| 416 |
path_dn = None |
| 417 |
if pod_id and node_id and path_ep: |
| 418 |
path_dn = "topology/pod-{0}/{1}-{2}/pathep-[{3}]".format(pod_id, path_type, node_id, path_ep) |
| 419 |
|
| 420 |
child_classes = [] |
| 421 |
child_configs = [] |
| 422 |
if micro_bfd is not None: |
| 423 |
child_classes.append("bfdMicroBfdP") |
| 424 |
child_configs.append(dict(bfdMicroBfdP=dict(attributes=dict(adminState=micro_bfd, dst=micro_bfd_destination, stTm=micro_bfd_timer)))) |
| 425 |
|
| 426 |
aci.construct_url( |
| 427 |
root_class=dict( |
| 428 |
aci_class="fvTenant", |
| 429 |
aci_rn="tn-{0}".format(tenant), |
| 430 |
module_object=tenant, |
| 431 |
target_filter={"name": tenant}, |
| 432 |
), |
| 433 |
subclass_1=dict( |
| 434 |
aci_class="l3extOut", |
| 435 |
aci_rn="out-{0}".format(l3out), |
| 436 |
module_object=l3out, |
| 437 |
target_filter={"name": l3out}, |
| 438 |
), |
| 439 |
subclass_2=dict( |
| 440 |
aci_class="l3extLNodeP", |
| 441 |
aci_rn="lnodep-{0}".format(node_profile), |
| 442 |
module_object=node_profile, |
| 443 |
target_filter={"name": node_profile}, |
| 444 |
), |
| 445 |
subclass_3=dict( |
| 446 |
aci_class="l3extLIfP", |
| 447 |
aci_rn="lifp-{0}".format(interface_profile), |
| 448 |
module_object=interface_profile, |
| 449 |
target_filter={"name": interface_profile}, |
| 450 |
), |
| 451 |
subclass_4=dict( |
| 452 |
aci_class="l3extRsPathL3OutAtt", |
| 453 |
aci_rn="rspathL3OutAtt-[{0}]".format(path_dn), |
| 454 |
module_object=path_dn, |
| 455 |
target_filter={"tDn": path_dn}, |
| 456 |
), |
| 457 |
child_classes=child_classes, |
| 458 |
) |
| 459 |
|
| 460 |
aci.get_existing() |
| 461 |
|
| 462 |
if state == "present": |
| 463 |
aci.payload( |
| 464 |
aci_class="l3extRsPathL3OutAtt", |
| 465 |
class_config=dict( |
| 466 |
tDn=path_dn, |
| 467 |
addr=address, |
| 468 |
ipv6Dad=ipv6_dad, |
| 469 |
mtu=mtu, |
| 470 |
ifInstT=interface_type, |
| 471 |
mode=mode, |
| 472 |
encap=encap, |
| 473 |
encapScope="ctx" if encap_scope == "vrf" else encap_scope, |
| 474 |
autostate=auto_state, |
| 475 |
descr=description, |
| 476 |
mac=mac, |
| 477 |
), |
| 478 |
child_configs=child_configs, |
| 479 |
) |
| 480 |
|
| 481 |
aci.get_diff(aci_class="l3extRsPathL3OutAtt") |
| 482 |
|
| 483 |
aci.post_config() |
| 484 |
|
| 485 |
elif state == "absent": |
| 486 |
aci.delete_config() |
| 487 |
|
| 488 |
aci.exit_json() |
| 489 |
|
| 490 |
|
| 491 |
if __name__ == "__main__": |
| 492 |
main() |