| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Tim Cragg (@timcragg) |
| 5 |
# Copyright: (c) 2025, Shreyas Srish (@shrsr) |
| 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_l4l7_device_selection_interface_context |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage L4-L7 Device Selection Policy Logical Interface Contexts (vns:LIfCtx) |
| 19 |
description: |
| 20 |
- Manage L4-L7 Device Selection Policy Logical Interface Contexts |
| 21 |
options: |
| 22 |
tenant: |
| 23 |
description: |
| 24 |
- The name of an existing tenant. |
| 25 |
type: str |
| 26 |
aliases: [ tenant_name ] |
| 27 |
contract: |
| 28 |
description: |
| 29 |
- The name of an existing contract. |
| 30 |
type: str |
| 31 |
aliases: [ contract_name ] |
| 32 |
graph: |
| 33 |
description: |
| 34 |
- The name of an existing Service Graph Template. |
| 35 |
type: str |
| 36 |
aliases: [ service_graph, service_graph_name ] |
| 37 |
node: |
| 38 |
description: |
| 39 |
- The name of an existing Service Graph Node. |
| 40 |
type: str |
| 41 |
aliases: [ node_name ] |
| 42 |
context: |
| 43 |
description: |
| 44 |
- The name of the logical interface context. |
| 45 |
type: str |
| 46 |
l3_destination: |
| 47 |
description: |
| 48 |
- Whether the context is a Layer3 destination. |
| 49 |
- The APIC defaults to C(true) when unset during creation. |
| 50 |
type: bool |
| 51 |
aliases: [ l3_dest ] |
| 52 |
permit_log: |
| 53 |
description: |
| 54 |
- Whether to log permitted traffic. |
| 55 |
- The APIC defaults to C(false) when unset during creation. |
| 56 |
type: bool |
| 57 |
bridge_domain: |
| 58 |
description: |
| 59 |
- The Bridge Domain to bind to the Context. |
| 60 |
type: str |
| 61 |
aliases: [ bd, bd_name ] |
| 62 |
bridge_domain_tenant: |
| 63 |
description: |
| 64 |
- The tenant the Bridge Domain resides in. |
| 65 |
- Omit this variable if both context and Bridge Domain are in the same tenant. |
| 66 |
- Intended use case is for when the Bridge Domain is in the common tenant, but the context is not. |
| 67 |
type: str |
| 68 |
aliases: [ bd_tenant ] |
| 69 |
logical_device: |
| 70 |
description: |
| 71 |
- The Logical Device to bind the context to. |
| 72 |
type: str |
| 73 |
logical_interface: |
| 74 |
description: |
| 75 |
- The Logical Interface to bind the context to. |
| 76 |
type: str |
| 77 |
redirect_policy: |
| 78 |
description: |
| 79 |
- The Redirect Policy to bind the context to. |
| 80 |
type: str |
| 81 |
permit_handoff: |
| 82 |
description: |
| 83 |
- Indicates whether to allow handoff of traffic to the associated logical interface. |
| 84 |
- The APIC defaults to C(false) when unset during creation. |
| 85 |
type: bool |
| 86 |
acl: |
| 87 |
description: |
| 88 |
- Specifies whether an Access Control List (ACL) is applied to the logical interface. |
| 89 |
- The APIC defaults to C(false) when unset during creation. |
| 90 |
type: bool |
| 91 |
description: |
| 92 |
description: |
| 93 |
- A brief description for the Logical Interface Context. |
| 94 |
type: str |
| 95 |
rule_type: |
| 96 |
description: |
| 97 |
- Indicates whether the context uses a specific rule type for traffic handling. |
| 98 |
- The APIC defaults to C(false) when unset during creation. |
| 99 |
type: bool |
| 100 |
l3out: |
| 101 |
description: |
| 102 |
- The name of the Layer 3 Outside (L3Out) object. |
| 103 |
type: str |
| 104 |
l3out_tenant: |
| 105 |
description: |
| 106 |
- The tenant in which the L3Out resides. |
| 107 |
- Omit this variable if both context and L3Out are in the same tenant. |
| 108 |
type: str |
| 109 |
external_epg: |
| 110 |
description: |
| 111 |
- The name of the External Network Instance Profile (External EPG or ExtEpg) associated with the L3Out. |
| 112 |
type: str |
| 113 |
redistribute: |
| 114 |
description: |
| 115 |
- A list of routing protocols whose routes should be redistributed. |
| 116 |
type: list |
| 117 |
elements: str |
| 118 |
choices: [ bgp, ospf, connected, static ] |
| 119 |
state: |
| 120 |
description: |
| 121 |
- Use C(present) or C(absent) for adding or removing. |
| 122 |
- Use C(query) for listing an object or multiple objects. |
| 123 |
type: str |
| 124 |
choices: [ absent, present, query ] |
| 125 |
default: present |
| 126 |
extends_documentation_fragment: |
| 127 |
- cisco.aci.aci |
| 128 |
- cisco.aci.annotation |
| 129 |
|
| 130 |
notes: |
| 131 |
- The I(tenant), I(graph), I(contract) and I(node) must exist before using this module in your playbook. |
| 132 |
The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l4l7_service_graph_template), M(cisco.aci.aci_contract) |
| 133 |
and M(cisco.aci.aci_l4l7_service_graph_template_node) modules can be used for this. |
| 134 |
seealso: |
| 135 |
- module: cisco.aci.aci_tenant |
| 136 |
- module: cisco.aci.aci_l4l7_service_graph_template |
| 137 |
- module: cisco.aci.aci_contract |
| 138 |
- module: cisco.aci.aci_l4l7_service_graph_template_node |
| 139 |
- name: APIC Management Information Model reference |
| 140 |
description: More information about the internal APIC class, B(vns:LIfCtx) |
| 141 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 142 |
author: |
| 143 |
- Tim Cragg (@timcragg) |
| 144 |
- Shreyas Srish (@shrsr) |
| 145 |
""" |
| 146 |
|
| 147 |
EXAMPLES = r""" |
| 148 |
- name: Add a new interface context |
| 149 |
cisco.aci.aci_l4l7_device_selection_interface_context: |
| 150 |
host: apic |
| 151 |
username: admin |
| 152 |
password: SomeSecretPassword |
| 153 |
tenant: my_tenant |
| 154 |
contract: my_contract |
| 155 |
graph: my_graph |
| 156 |
node: my_node |
| 157 |
context: provider |
| 158 |
state: present |
| 159 |
delegate_to: localhost |
| 160 |
|
| 161 |
- name: Query an interface context |
| 162 |
cisco.aci.aci_l4l7_device_selection_interface_context: |
| 163 |
host: apic |
| 164 |
username: admin |
| 165 |
password: SomeSecretPassword |
| 166 |
tenant: my_tenant |
| 167 |
contract: my_contract |
| 168 |
graph: my_graph |
| 169 |
node: my_node |
| 170 |
context: consumer |
| 171 |
state: query |
| 172 |
delegate_to: localhost |
| 173 |
register: query_result |
| 174 |
|
| 175 |
- name: Query all interface contexts |
| 176 |
cisco.aci.aci_l4l7_device_selection_interface_context: |
| 177 |
host: apic |
| 178 |
username: admin |
| 179 |
password: SomeSecretPassword |
| 180 |
state: query |
| 181 |
delegate_to: localhost |
| 182 |
register: query_result |
| 183 |
|
| 184 |
- name: Delete an interface context |
| 185 |
cisco.aci.aci_l4l7_device_selection_interface_context: |
| 186 |
host: apic |
| 187 |
username: admin |
| 188 |
password: SomeSecretPassword |
| 189 |
tenant: my_tenant |
| 190 |
contract: my_contract |
| 191 |
graph: my_graph |
| 192 |
node: my_node |
| 193 |
context: provider |
| 194 |
state: absent |
| 195 |
delegate_to: localhost |
| 196 |
""" |
| 197 |
|
| 198 |
RETURN = r""" |
| 199 |
current: |
| 200 |
description: The existing configuration from the APIC after the module has finished |
| 201 |
returned: success |
| 202 |
type: list |
| 203 |
sample: |
| 204 |
[ |
| 205 |
{ |
| 206 |
"fvTenant": { |
| 207 |
"attributes": { |
| 208 |
"descr": "Production environment", |
| 209 |
"dn": "uni/tn-production", |
| 210 |
"name": "production", |
| 211 |
"nameAlias": "", |
| 212 |
"ownerKey": "", |
| 213 |
"ownerTag": "" |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
] |
| 218 |
error: |
| 219 |
description: The error information as returned from the APIC |
| 220 |
returned: failure |
| 221 |
type: dict |
| 222 |
sample: |
| 223 |
{ |
| 224 |
"code": "122", |
| 225 |
"text": "unknown managed object class foo" |
| 226 |
} |
| 227 |
raw: |
| 228 |
description: The raw output returned by the APIC REST API (xml or json) |
| 229 |
returned: parse error |
| 230 |
type: str |
| 231 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 232 |
sent: |
| 233 |
description: The actual/minimal configuration pushed to the APIC |
| 234 |
returned: info |
| 235 |
type: list |
| 236 |
sample: |
| 237 |
{ |
| 238 |
"fvTenant": { |
| 239 |
"attributes": { |
| 240 |
"descr": "Production environment" |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
previous: |
| 245 |
description: The original configuration from the APIC before the module has started |
| 246 |
returned: info |
| 247 |
type: list |
| 248 |
sample: |
| 249 |
[ |
| 250 |
{ |
| 251 |
"fvTenant": { |
| 252 |
"attributes": { |
| 253 |
"descr": "Production", |
| 254 |
"dn": "uni/tn-production", |
| 255 |
"name": "production", |
| 256 |
"nameAlias": "", |
| 257 |
"ownerKey": "", |
| 258 |
"ownerTag": "" |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
] |
| 263 |
proposed: |
| 264 |
description: The assembled configuration from the user-provided parameters |
| 265 |
returned: info |
| 266 |
type: dict |
| 267 |
sample: |
| 268 |
{ |
| 269 |
"fvTenant": { |
| 270 |
"attributes": { |
| 271 |
"descr": "Production environment", |
| 272 |
"name": "production" |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
filter_string: |
| 277 |
description: The filter string used for the request |
| 278 |
returned: failure or debug |
| 279 |
type: str |
| 280 |
sample: ?rsp-prop-include=config-only |
| 281 |
method: |
| 282 |
description: The HTTP method used for the request to the APIC |
| 283 |
returned: failure or debug |
| 284 |
type: str |
| 285 |
sample: POST |
| 286 |
response: |
| 287 |
description: The HTTP response from the APIC |
| 288 |
returned: failure or debug |
| 289 |
type: str |
| 290 |
sample: OK (30 bytes) |
| 291 |
status: |
| 292 |
description: The HTTP status from the APIC |
| 293 |
returned: failure or debug |
| 294 |
type: int |
| 295 |
sample: 200 |
| 296 |
url: |
| 297 |
description: The HTTP url used for the request to the APIC |
| 298 |
returned: failure or debug |
| 299 |
type: str |
| 300 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 301 |
""" |
| 302 |
|
| 303 |
|
| 304 |
from ansible.module_utils.basic import AnsibleModule |
| 305 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 306 |
|
| 307 |
|
| 308 |
def main(): |
| 309 |
argument_spec = aci_argument_spec() |
| 310 |
argument_spec.update(aci_annotation_spec()) |
| 311 |
argument_spec.update( |
| 312 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 313 |
contract=dict(type="str", aliases=["contract_name"]), |
| 314 |
graph=dict(type="str", aliases=["service_graph", "service_graph_name"]), |
| 315 |
node=dict(type="str", aliases=["node_name"]), |
| 316 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 317 |
context=dict(type="str"), |
| 318 |
l3_destination=dict(type="bool", aliases=["l3_dest"]), |
| 319 |
permit_log=dict(type="bool"), |
| 320 |
bridge_domain=dict(type="str", aliases=["bd", "bd_name"]), |
| 321 |
bridge_domain_tenant=dict(type="str", aliases=["bd_tenant"]), |
| 322 |
logical_device=dict(type="str"), |
| 323 |
logical_interface=dict(type="str"), |
| 324 |
redirect_policy=dict(type="str"), |
| 325 |
permit_handoff=dict(type="bool"), |
| 326 |
acl=dict(type="bool"), |
| 327 |
description=dict(type="str"), |
| 328 |
rule_type=dict(type="bool"), |
| 329 |
l3out=dict(type="str"), |
| 330 |
l3out_tenant=dict(type="str"), |
| 331 |
external_epg=dict(type="str"), |
| 332 |
redistribute=dict(type="list", elements="str", choices=["bgp", "ospf", "connected", "static"]), |
| 333 |
) |
| 334 |
module = AnsibleModule( |
| 335 |
argument_spec=argument_spec, |
| 336 |
supports_check_mode=True, |
| 337 |
required_if=[ |
| 338 |
["state", "absent", ["tenant", "contract", "graph", "node", "context"]], |
| 339 |
["state", "present", ["tenant", "contract", "graph", "node", "context"]], |
| 340 |
], |
| 341 |
required_together=[ |
| 342 |
("l3out", "external_epg"), |
| 343 |
], |
| 344 |
mutually_exclusive=[ |
| 345 |
("bridge_domain", "l3out"), |
| 346 |
("bridge_domain", "l3out_tenant"), |
| 347 |
("bridge_domain", "external_epg"), |
| 348 |
("bridge_domain", "redistribute"), |
| 349 |
("l3out", "bridge_domain_tenant"), |
| 350 |
], |
| 351 |
) |
| 352 |
|
| 353 |
aci = ACIModule(module) |
| 354 |
|
| 355 |
tenant = module.params.get("tenant") |
| 356 |
state = module.params.get("state") |
| 357 |
contract = module.params.get("contract") |
| 358 |
graph = module.params.get("graph") |
| 359 |
node = module.params.get("node") |
| 360 |
context = module.params.get("context") |
| 361 |
l3_destination = aci.boolean(module.params.get("l3_destination")) |
| 362 |
permit_log = aci.boolean(module.params.get("permit_log")) |
| 363 |
bridge_domain = module.params.get("bridge_domain") |
| 364 |
bridge_domain_tenant = module.params.get("bridge_domain_tenant") |
| 365 |
l3out = module.params.get("l3out") |
| 366 |
l3out_tenant = module.params.get("l3out_tenant") |
| 367 |
external_epg = module.params.get("external_epg") |
| 368 |
redistribute = module.params.get("redistribute") |
| 369 |
logical_device = module.params.get("logical_device") |
| 370 |
logical_interface = module.params.get("logical_interface") |
| 371 |
redirect_policy = module.params.get("redirect_policy") |
| 372 |
permit_handoff = aci.boolean(module.params.get("permit_handoff")) |
| 373 |
acl = aci.boolean(module.params.get("acl")) |
| 374 |
description = module.params.get("description") |
| 375 |
rule_type = aci.boolean(module.params.get("rule_type")) |
| 376 |
|
| 377 |
ldev_ctx_rn = "ldevCtx-c-{0}-g-{1}-n-{2}".format(contract, graph, node) if (contract, graph, node) != (None, None, None) else None |
| 378 |
|
| 379 |
aci.construct_url( |
| 380 |
root_class=dict( |
| 381 |
aci_class="fvTenant", |
| 382 |
aci_rn="tn-{0}".format(tenant), |
| 383 |
module_object=tenant, |
| 384 |
target_filter={"name": tenant}, |
| 385 |
), |
| 386 |
subclass_1=dict( |
| 387 |
aci_class="vnsLDevCtx", |
| 388 |
aci_rn=ldev_ctx_rn, |
| 389 |
module_object=ldev_ctx_rn, |
| 390 |
target_filter={"dn": ldev_ctx_rn}, |
| 391 |
), |
| 392 |
subclass_2=dict( |
| 393 |
aci_class="vnsLIfCtx", |
| 394 |
aci_rn="lIfCtx-c-{0}".format(context), |
| 395 |
module_object=context, |
| 396 |
target_filter={"connNameOrLbl": context}, |
| 397 |
), |
| 398 |
child_classes=["vnsRsLIfCtxToBD", "vnsRsLIfCtxToLIf", "vnsRsLIfCtxToSvcRedirectPol", "vnsRsLIfCtxToInstP"], |
| 399 |
) |
| 400 |
|
| 401 |
aci.get_existing() |
| 402 |
|
| 403 |
if state == "present": |
| 404 |
child_configs = [] |
| 405 |
if bridge_domain: |
| 406 |
if bridge_domain_tenant is None: |
| 407 |
bridge_domain_tenant = tenant |
| 408 |
bd_tdn = "uni/tn-{0}/BD-{1}".format(bridge_domain_tenant, bridge_domain) |
| 409 |
child_configs.append({"vnsRsLIfCtxToBD": {"attributes": {"tDn": bd_tdn}}}) |
| 410 |
else: |
| 411 |
bd_tdn = None |
| 412 |
if l3out: |
| 413 |
if l3out_tenant is None: |
| 414 |
l3out_tenant = tenant |
| 415 |
if redistribute is not None: |
| 416 |
redistribute = ",".join(redistribute) |
| 417 |
l3out_tdn = "uni/tn-{0}/out-{1}/instP-{2}".format(l3out_tenant, l3out, external_epg) |
| 418 |
child_configs.append({"vnsRsLIfCtxToInstP": {"attributes": {"tDn": l3out_tdn, "redistribute": redistribute}}}) |
| 419 |
else: |
| 420 |
l3out_tdn = None |
| 421 |
if logical_interface: |
| 422 |
log_intf_tdn = "uni/tn-{0}/lDevVip-{1}/lIf-{2}".format(tenant, logical_device, logical_interface) |
| 423 |
child_configs.append({"vnsRsLIfCtxToLIf": {"attributes": {"tDn": log_intf_tdn}}}) |
| 424 |
else: |
| 425 |
log_intf_tdn = None |
| 426 |
if redirect_policy: |
| 427 |
redir_pol_tdn = "uni/tn-{0}/svcCont/svcRedirectPol-{1}".format(tenant, redirect_policy) |
| 428 |
child_configs.append({"vnsRsLIfCtxToSvcRedirectPol": {"attributes": {"tDn": redir_pol_tdn}}}) |
| 429 |
else: |
| 430 |
redir_pol_tdn = None |
| 431 |
# Validate if existing and remove child objects when do not match provided configuration |
| 432 |
if isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 433 |
for child in aci.existing[0].get("vnsLIfCtx", {}).get("children", {}): |
| 434 |
if child.get("vnsRsLIfCtxToBD") and child.get("vnsRsLIfCtxToBD").get("attributes").get("tDn") != bd_tdn: |
| 435 |
# Appending to child_config list not possible because of APIC Error 103: child (Rn) of class vnsRsLIfCtxToBD is already attached. |
| 436 |
# A seperate delete request to dn of the vnsRsLIfCtxToBD is needed to remove the object prior to adding to child_configs. |
| 437 |
# child_configs.append( |
| 438 |
# { |
| 439 |
# "vnsRsLIfCtxToBD": { |
| 440 |
# "attributes": { |
| 441 |
# "dn": child.get("vnsRsLIfCtxToBD").get("attributes").get("dn"), |
| 442 |
# "status": "deleted", |
| 443 |
# } |
| 444 |
# } |
| 445 |
# } |
| 446 |
# ) |
| 447 |
aci.api_call( |
| 448 |
"DELETE", |
| 449 |
"{0}/api/mo/uni/tn-{1}/ldevCtx-c-{2}-g-{3}-n-{4}/lIfCtx-c-{5}/rsLIfCtxToBD.json".format( |
| 450 |
aci.base_url, tenant, contract, graph, node, context |
| 451 |
), |
| 452 |
) |
| 453 |
elif child.get("vnsRsLIfCtxToInstP") and child.get("vnsRsLIfCtxToInstP").get("attributes").get("tDn") != l3out_tdn: |
| 454 |
# Appending to child_config list not possible because of APIC Error 103: child (Rn) of class vnsRsLIfCtxToInstP is already attached. |
| 455 |
# A seperate delete request to dn of the vnsRsLIfCtxToInstP is needed to remove the object prior to adding to child_configs. |
| 456 |
aci.api_call( |
| 457 |
"DELETE", |
| 458 |
"{0}/api/mo/uni/tn-{1}/ldevCtx-c-{2}-g-{3}-n-{4}/lIfCtx-c-{5}/rsLIfCtxToInstP.json".format( |
| 459 |
aci.base_url, tenant, contract, graph, node, context |
| 460 |
), |
| 461 |
) |
| 462 |
elif child.get("vnsRsLIfCtxToLIf") and child.get("vnsRsLIfCtxToLIf").get("attributes").get("tDn") != log_intf_tdn: |
| 463 |
child_configs.append( |
| 464 |
{ |
| 465 |
"vnsRsLIfCtxToLIf": { |
| 466 |
"attributes": { |
| 467 |
"dn": child.get("vnsRsLIfCtxToLIf").get("attributes").get("dn"), |
| 468 |
"status": "deleted", |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
) |
| 473 |
elif child.get("vnsRsLIfCtxToSvcRedirectPol") and child.get("vnsRsLIfCtxToSvcRedirectPol").get("attributes").get("tDn") != redir_pol_tdn: |
| 474 |
child_configs.append( |
| 475 |
{ |
| 476 |
"vnsRsLIfCtxToSvcRedirectPol": { |
| 477 |
"attributes": { |
| 478 |
"dn": child.get("vnsRsLIfCtxToSvcRedirectPol").get("attributes").get("dn"), |
| 479 |
"status": "deleted", |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
) |
| 484 |
aci.payload( |
| 485 |
aci_class="vnsLIfCtx", |
| 486 |
class_config=dict( |
| 487 |
connNameOrLbl=context, |
| 488 |
l3Dest=l3_destination, |
| 489 |
permitLog=permit_log, |
| 490 |
permitHandoff=permit_handoff, |
| 491 |
acl=acl, |
| 492 |
descr=description, |
| 493 |
ruleType=rule_type, |
| 494 |
), |
| 495 |
child_configs=child_configs, |
| 496 |
) |
| 497 |
aci.get_diff(aci_class="vnsLIfCtx") |
| 498 |
|
| 499 |
aci.post_config() |
| 500 |
|
| 501 |
elif state == "absent": |
| 502 |
aci.delete_config() |
| 503 |
|
| 504 |
aci.exit_json() |
| 505 |
|
| 506 |
|
| 507 |
if __name__ == "__main__": |
| 508 |
main() |