| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Dev Sinha (@DevSinha13) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 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_vrf_fallback_route_group |
| 15 |
version_added: "2.12.0" |
| 16 |
short_description: Manage VRF Fallback Route Groups (fv:FBRGroup, fv:FBRoute, and fv:FBRMember) |
| 17 |
description: |
| 18 |
- Manage VRF Fallback Route Groups on Cisco ACI fabrics. |
| 19 |
- Fallback Route Groups are used to specify routes and next-hop addresses for VRFs. |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- The name of the Tenant associated with the VRF Fallback Route Group. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
vrf: |
| 27 |
description: |
| 28 |
- The name of the VRF associated with the VRF Fallback Route Group. |
| 29 |
type: str |
| 30 |
aliases: [ context, vrf_name ] |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The name of the VRF Fallback Route Group. |
| 34 |
type: str |
| 35 |
aliases: [ vrf_fallback_route_group ] |
| 36 |
prefix_address: |
| 37 |
description: |
| 38 |
- The fallback route (prefix address) for the VRF Fallback Route Group. |
| 39 |
- If not specified, the existing fallback route will remain unchanged. |
| 40 |
- To delete the fallback route, pass an empty string as the attribute value. |
| 41 |
type: str |
| 42 |
aliases: [ fallback_route ] |
| 43 |
fallback_members: |
| 44 |
description: |
| 45 |
- A list of fallback member IP addresses (next-hop addresses) for the VRF Fallback Route Group. |
| 46 |
- Members not in the list will be removed from the configuration. |
| 47 |
- If not specified, the existing fallback members will remain unchanged. |
| 48 |
- To delete all the fallback members, pass an empty list. |
| 49 |
type: list |
| 50 |
elements: str |
| 51 |
aliases: [ next_hop_address ] |
| 52 |
description: |
| 53 |
description: |
| 54 |
- The description for the VRF Fallback Route Group. |
| 55 |
type: str |
| 56 |
aliases: [ descr ] |
| 57 |
state: |
| 58 |
description: |
| 59 |
- Use C(present) or C(absent) for adding or removing. |
| 60 |
- Use C(query) for listing an object or multiple objects. |
| 61 |
type: str |
| 62 |
choices: [ absent, present, query ] |
| 63 |
default: present |
| 64 |
extends_documentation_fragment: |
| 65 |
- cisco.aci.aci |
| 66 |
- cisco.aci.annotation |
| 67 |
- cisco.aci.owner |
| 68 |
|
| 69 |
notes: |
| 70 |
- The C(tenant) and C(vrf) must exist before using this module. |
| 71 |
Use the M(cisco.aci.aci_tenant) and M(cisco.aci.aci_vrf) modules to create them if needed. |
| 72 |
seealso: |
| 73 |
- module: cisco.aci.aci_vrf |
| 74 |
- module: cisco.aci.aci_tenant |
| 75 |
- name: APIC Management Information Model reference |
| 76 |
description: More information about the internal APIC class B(fv:FBRGroup), B(fv:FBRoute), and B(fv:FBRMember). |
| 77 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 78 |
author: |
| 79 |
- Dev Sinha (@devsinha13) |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: Create a new VRF Fallback Route Group |
| 84 |
cisco.aci.aci_vrf_fallback_route_group: |
| 85 |
host: apic |
| 86 |
username: admin |
| 87 |
password: SomeSecretPassword |
| 88 |
tenant: ansible_test |
| 89 |
vrf: vrf_test |
| 90 |
vrf_fallback_route_group: test_fallback_route_group |
| 91 |
fallback_route: 1.1.1.1/24 |
| 92 |
fallback_members: |
| 93 |
- 192.168.1.1 |
| 94 |
- 192.168.1.2 |
| 95 |
description: Test Fallback Route Group |
| 96 |
state: present |
| 97 |
delegate_to: localhost |
| 98 |
|
| 99 |
- name: Update fallback members in an existing VRF Fallback Route Group |
| 100 |
cisco.aci.aci_vrf_fallback_route_group: |
| 101 |
host: apic |
| 102 |
username: admin |
| 103 |
password: SomeSecretPassword |
| 104 |
tenant: ansible_test |
| 105 |
vrf: vrf_test |
| 106 |
vrf_fallback_route_group: test_fallback_route_group |
| 107 |
fallback_members: |
| 108 |
- 192.168.1.1 |
| 109 |
- 192.168.1.3 |
| 110 |
state: present |
| 111 |
delegate_to: localhost |
| 112 |
|
| 113 |
- name: Delete children for VRF Fallback Route Group |
| 114 |
cisco.aci.aci_vrf_fallback_route_group: |
| 115 |
host: apic |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
tenant: ansible_test |
| 119 |
vrf: vrf_test |
| 120 |
vrf_fallback_route_group: test_fallback_route_group |
| 121 |
fallback_route: "" |
| 122 |
fallback_members: [] |
| 123 |
state: present |
| 124 |
delegate_to: localhost |
| 125 |
|
| 126 |
- name: Query a VRF Fallback Route Group |
| 127 |
cisco.aci.aci_vrf_fallback_route_group: |
| 128 |
host: apic |
| 129 |
username: admin |
| 130 |
password: SomeSecretPassword |
| 131 |
tenant: ansible_test |
| 132 |
vrf: vrf_test |
| 133 |
vrf_fallback_route_group: test_fallback_route_group |
| 134 |
state: query |
| 135 |
delegate_to: localhost |
| 136 |
register: query_result |
| 137 |
|
| 138 |
- name: Query all VRF Fallback Route Groups |
| 139 |
cisco.aci.aci_vrf_fallback_route_group: |
| 140 |
host: apic |
| 141 |
username: admin |
| 142 |
password: SomeSecretPassword |
| 143 |
state: query |
| 144 |
delegate_to: localhost |
| 145 |
register: query_result |
| 146 |
|
| 147 |
- name: Delete a VRF Fallback Route Group |
| 148 |
cisco.aci.aci_vrf_fallback_route_group: |
| 149 |
host: apic |
| 150 |
username: admin |
| 151 |
password: SomeSecretPassword |
| 152 |
tenant: ansible_test |
| 153 |
vrf: vrf_test |
| 154 |
vrf_fallback_route_group: test_fallback_route_group |
| 155 |
state: absent |
| 156 |
delegate_to: localhost |
| 157 |
""" |
| 158 |
|
| 159 |
RETURN = r""" |
| 160 |
current: |
| 161 |
description: The existing configuration from the APIC after the module has finished |
| 162 |
returned: success |
| 163 |
type: list |
| 164 |
sample: |
| 165 |
[ |
| 166 |
{ |
| 167 |
"fvTenant": { |
| 168 |
"attributes": { |
| 169 |
"descr": "Production environment", |
| 170 |
"dn": "uni/tn-production", |
| 171 |
"name": "production", |
| 172 |
"nameAlias": "", |
| 173 |
"ownerKey": "", |
| 174 |
"ownerTag": "" |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
] |
| 179 |
error: |
| 180 |
description: The error information as returned from the APIC |
| 181 |
returned: failure |
| 182 |
type: dict |
| 183 |
sample: |
| 184 |
{ |
| 185 |
"code": "122", |
| 186 |
"text": "unknown managed object class foo" |
| 187 |
} |
| 188 |
raw: |
| 189 |
description: The raw output returned by the APIC REST API (xml or json) |
| 190 |
returned: parse error |
| 191 |
type: str |
| 192 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 193 |
sent: |
| 194 |
description: The actual/minimal configuration pushed to the APIC |
| 195 |
returned: info |
| 196 |
type: list |
| 197 |
sample: |
| 198 |
{ |
| 199 |
"fvTenant": { |
| 200 |
"attributes": { |
| 201 |
"descr": "Production environment" |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
previous: |
| 206 |
description: The original configuration from the APIC before the module has started |
| 207 |
returned: info |
| 208 |
type: list |
| 209 |
sample: |
| 210 |
[ |
| 211 |
{ |
| 212 |
"fvTenant": { |
| 213 |
"attributes": { |
| 214 |
"descr": "Production", |
| 215 |
"dn": "uni/tn-production", |
| 216 |
"name": "production", |
| 217 |
"nameAlias": "", |
| 218 |
"ownerKey": "", |
| 219 |
"ownerTag": "" |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
] |
| 224 |
proposed: |
| 225 |
description: The assembled configuration from the user-provided parameters |
| 226 |
returned: info |
| 227 |
type: dict |
| 228 |
sample: |
| 229 |
{ |
| 230 |
"fvTenant": { |
| 231 |
"attributes": { |
| 232 |
"descr": "Production environment", |
| 233 |
"name": "production" |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
filter_string: |
| 238 |
description: The filter string used for the request |
| 239 |
returned: failure or debug |
| 240 |
type: str |
| 241 |
sample: ?rsp-prop-include=config-only |
| 242 |
method: |
| 243 |
description: The HTTP method used for the request to the APIC |
| 244 |
returned: failure or debug |
| 245 |
type: str |
| 246 |
sample: POST |
| 247 |
response: |
| 248 |
description: The HTTP response from the APIC |
| 249 |
returned: failure or debug |
| 250 |
type: str |
| 251 |
sample: OK (30 bytes) |
| 252 |
status: |
| 253 |
description: The HTTP status from the APIC |
| 254 |
returned: failure or debug |
| 255 |
type: int |
| 256 |
sample: 200 |
| 257 |
url: |
| 258 |
description: The HTTP url used for the request to the APIC |
| 259 |
returned: failure or debug |
| 260 |
type: str |
| 261 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 262 |
""" |
| 263 |
|
| 264 |
|
| 265 |
from ansible.module_utils.basic import AnsibleModule |
| 266 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 267 |
|
| 268 |
|
| 269 |
def main(): |
| 270 |
argument_spec = aci_argument_spec() |
| 271 |
argument_spec.update(aci_annotation_spec()) |
| 272 |
argument_spec.update(aci_owner_spec()) |
| 273 |
argument_spec.update( |
| 274 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 275 |
vrf=dict(type="str", aliases=["context", "vrf_name"]), |
| 276 |
vrf_fallback_route_group=dict(type="str", aliases=["name"]), |
| 277 |
fallback_route=dict(type="str", aliases=["prefix_address"]), |
| 278 |
fallback_members=dict(type="list", elements="str", aliases=["next_hop_address"]), |
| 279 |
description=dict(type="str", aliases=["descr"]), |
| 280 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 281 |
) |
| 282 |
|
| 283 |
module = AnsibleModule( |
| 284 |
argument_spec=argument_spec, |
| 285 |
supports_check_mode=True, |
| 286 |
required_if=[ |
| 287 |
["state", "absent", ["tenant", "vrf"]], |
| 288 |
["state", "present", ["tenant", "vrf"]], |
| 289 |
], |
| 290 |
) |
| 291 |
|
| 292 |
tenant = module.params.get("tenant") |
| 293 |
vrf = module.params.get("vrf") |
| 294 |
vrf_fallback_route_group = module.params.get("vrf_fallback_route_group") |
| 295 |
fallback_route = module.params.get("fallback_route") |
| 296 |
fallback_members = module.params.get("fallback_members") |
| 297 |
description = module.params.get("description") |
| 298 |
state = module.params.get("state") |
| 299 |
|
| 300 |
aci = ACIModule(module) |
| 301 |
|
| 302 |
aci.construct_url( |
| 303 |
root_class=dict( |
| 304 |
aci_class="fvTenant", |
| 305 |
aci_rn="tn-{0}".format(tenant), |
| 306 |
module_object=tenant, |
| 307 |
target_filter={"name": tenant}, |
| 308 |
), |
| 309 |
subclass_1=dict( |
| 310 |
aci_class="fvCtx", |
| 311 |
aci_rn="ctx-{0}".format(vrf), |
| 312 |
module_object=vrf, |
| 313 |
target_filter={"name": vrf}, |
| 314 |
), |
| 315 |
subclass_2=dict( |
| 316 |
aci_class="fvFBRGroup", |
| 317 |
aci_rn="fbrg-{0}".format(vrf_fallback_route_group), |
| 318 |
module_object=vrf_fallback_route_group, |
| 319 |
target_filter={"name": vrf_fallback_route_group}, |
| 320 |
), |
| 321 |
child_classes=["fvFBRMember", "fvFBRoute"], |
| 322 |
) |
| 323 |
|
| 324 |
aci.get_existing() |
| 325 |
|
| 326 |
if state == "present": |
| 327 |
|
| 328 |
child_configs = [] |
| 329 |
|
| 330 |
existing_members = [] |
| 331 |
existing_route = None |
| 332 |
|
| 333 |
if isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 334 |
for child in aci.existing[0].get("fvFBRGroup", {}).get("children", {}): |
| 335 |
existing_member = child.get("fvFBRMember", {}).get("attributes", {}).get("rnhAddr") |
| 336 |
if existing_member: |
| 337 |
existing_members.append(existing_member) |
| 338 |
route = child.get("fvFBRoute", {}).get("attributes", {}).get("fbrPrefix") |
| 339 |
if route: |
| 340 |
existing_route = route |
| 341 |
|
| 342 |
if fallback_members is not None: |
| 343 |
fallback_members_set = set(fallback_members) |
| 344 |
|
| 345 |
existing_members_set = set(existing_members) |
| 346 |
|
| 347 |
for member in fallback_members_set - existing_members_set: |
| 348 |
child_configs.append(dict(fvFBRMember=dict(attributes=dict(rnhAddr=member)))) |
| 349 |
for existing_member in existing_members_set - fallback_members_set: |
| 350 |
child_configs.append(dict(fvFBRMember=dict(attributes=dict(rnhAddr=existing_member, status="deleted")))) |
| 351 |
|
| 352 |
if fallback_route is not None and fallback_route != existing_route: |
| 353 |
if existing_route: |
| 354 |
# Appending to child_config list not possible because of APIC Error 182: Multiple fallback routes not allowed in one group. |
| 355 |
# A seperate delete request to dn of the fvFBRoute is needed to remove the object prior to adding to child_configs. |
| 356 |
# Failed child_config is displayed in below: |
| 357 |
# child_configs.append( |
| 358 |
# dict(fvFBRoute=dit(attributes=dict(fbrPrefix=existing_route, status="deleted"))), |
| 359 |
# ) |
| 360 |
aci.api_call( |
| 361 |
"DELETE", |
| 362 |
"{0}/api/mo/uni/tn-{1}/ctx-{2}/fbrg-{3}/pfx-[{4}].json".format(aci.base_url, tenant, vrf, vrf_fallback_route_group, existing_route), |
| 363 |
) |
| 364 |
if fallback_route: |
| 365 |
child_configs.append( |
| 366 |
dict(fvFBRoute=dict(attributes=dict(fbrPrefix=fallback_route))), |
| 367 |
) |
| 368 |
|
| 369 |
aci.payload( |
| 370 |
aci_class="fvFBRGroup", |
| 371 |
class_config=dict( |
| 372 |
descr=description, |
| 373 |
name=vrf_fallback_route_group, |
| 374 |
), |
| 375 |
child_configs=child_configs, |
| 376 |
) |
| 377 |
|
| 378 |
aci.get_diff(aci_class="fvFBRGroup") |
| 379 |
|
| 380 |
aci.post_config() |
| 381 |
elif state == "absent": |
| 382 |
aci.delete_config() |
| 383 |
|
| 384 |
aci.exit_json() |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == "__main__": |
| 388 |
main() |