| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Jacob McGill (@jmcgill298) |
| 5 |
# Copyright: (c) 2025, 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": "certified"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_bd_subnet |
| 17 |
short_description: Manage Subnets (fv:Subnet) |
| 18 |
description: |
| 19 |
- Manage Subnets on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
bd: |
| 22 |
description: |
| 23 |
- The name of the Bridge Domain. |
| 24 |
type: str |
| 25 |
aliases: [ bd_name ] |
| 26 |
description: |
| 27 |
description: |
| 28 |
- The description for the Subnet. |
| 29 |
type: str |
| 30 |
aliases: [ descr ] |
| 31 |
enable_vip: |
| 32 |
description: |
| 33 |
- Determines if the Subnet should be treated as a VIP; used when the BD is extended to multiple sites. |
| 34 |
- The APIC defaults to C(false) when unset during creation. |
| 35 |
type: bool |
| 36 |
gateway: |
| 37 |
description: |
| 38 |
- The IPv4 or IPv6 gateway address for the Subnet. |
| 39 |
type: str |
| 40 |
aliases: [ gateway_ip ] |
| 41 |
mask: |
| 42 |
description: |
| 43 |
- The subnet mask for the Subnet. |
| 44 |
- This is the number associated with CIDR notation. |
| 45 |
- For IPv4 addresses, accepted values range between C(0) and C(32). |
| 46 |
- For IPv6 addresses, accepted Values range between C(0) and C(128). |
| 47 |
type: int |
| 48 |
aliases: [ subnet_mask ] |
| 49 |
nd_prefix_policy: |
| 50 |
description: |
| 51 |
- The IPv6 Neighbor Discovery Prefix Policy to associate with the Subnet. |
| 52 |
type: str |
| 53 |
preferred: |
| 54 |
description: |
| 55 |
- Determines if the Subnet is preferred over all available Subnets. Only one Subnet per Address Family (IPv4/IPv6). |
| 56 |
can be preferred in the Bridge Domain. |
| 57 |
- The APIC defaults to C(false) when unset during creation. |
| 58 |
type: bool |
| 59 |
route_profile: |
| 60 |
description: |
| 61 |
- The Route Profile to the associate with the Subnet. |
| 62 |
type: str |
| 63 |
route_profile_l3_out: |
| 64 |
description: |
| 65 |
- The L3 Out that contains the associated Route Profile. |
| 66 |
type: str |
| 67 |
scope: |
| 68 |
description: |
| 69 |
- Determines the scope of the Subnet. |
| 70 |
- The C(private) option only allows communication with hosts in the same VRF. |
| 71 |
- The C(public) option allows the Subnet to be advertised outside of the ACI Fabric, and allows communication with |
| 72 |
hosts in other VRFs. |
| 73 |
- The shared option limits communication to hosts in either the same VRF or the shared VRF. |
| 74 |
- The value is a list of options, C(private) and C(public) are mutually exclusive, but both can be used with C(shared). |
| 75 |
- The APIC defaults to C(private) when unset during creation. |
| 76 |
type: list |
| 77 |
elements: str |
| 78 |
choices: |
| 79 |
- private |
| 80 |
- public |
| 81 |
- shared |
| 82 |
subnet_control: |
| 83 |
description: |
| 84 |
- Determines the Subnet's Control State. |
| 85 |
- The C(querier_ip) option is used to treat the gateway_ip as an IGMP querier source IP. |
| 86 |
- The C(nd_ra) option is used to treat the gateway_ip address as a Neighbor Discovery Router Advertisement Prefix. |
| 87 |
- The C(no_gw) option is used to remove default gateway functionality from the gateway address. |
| 88 |
- The APIC defaults to C(nd_ra) when unset during creation. |
| 89 |
type: list |
| 90 |
elements: str |
| 91 |
choices: [ nd_ra, no_gw, querier_ip, unspecified ] |
| 92 |
subnet_name: |
| 93 |
description: |
| 94 |
- The name of the Subnet. |
| 95 |
type: str |
| 96 |
aliases: [ name ] |
| 97 |
ip_data_plane_learning: |
| 98 |
description: |
| 99 |
- Whether IP data plane learning is enabled or disabled. |
| 100 |
- The APIC defaults to C(enabled) when unset during creation. |
| 101 |
type: str |
| 102 |
choices: [ enabled, disabled ] |
| 103 |
aliases: [ ip_dataplane_learning ] |
| 104 |
tenant: |
| 105 |
description: |
| 106 |
- The name of the Tenant. |
| 107 |
type: str |
| 108 |
aliases: [ tenant_name ] |
| 109 |
state: |
| 110 |
description: |
| 111 |
- Use C(present) or C(absent) for adding or removing. |
| 112 |
- Use C(query) for listing an object or multiple objects. |
| 113 |
type: str |
| 114 |
choices: [ absent, present, query ] |
| 115 |
default: present |
| 116 |
name_alias: |
| 117 |
description: |
| 118 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 119 |
type: str |
| 120 |
extends_documentation_fragment: |
| 121 |
- cisco.aci.aci |
| 122 |
- cisco.aci.annotation |
| 123 |
|
| 124 |
notes: |
| 125 |
- The C(gateway) parameter is the root key used to access the Subnet (not name), so the C(gateway) |
| 126 |
is required when the state is C(absent) or C(present). |
| 127 |
- The C(tenant) and C(bd) used must exist before using this module in your playbook. |
| 128 |
The M(cisco.aci.aci_tenant) module and M(cisco.aci.aci_bd) can be used for these. |
| 129 |
seealso: |
| 130 |
- module: cisco.aci.aci_bd |
| 131 |
- module: cisco.aci.aci_tenant |
| 132 |
- name: APIC Management Information Model reference |
| 133 |
description: More information about the internal APIC class B(fv:Subnet). |
| 134 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 135 |
author: |
| 136 |
- Jacob McGill (@jmcgill298) |
| 137 |
- Akini Ross (@akinross) |
| 138 |
""" |
| 139 |
|
| 140 |
EXAMPLES = r""" |
| 141 |
- name: Create a tenant |
| 142 |
cisco.aci.aci_tenant: |
| 143 |
host: apic |
| 144 |
username: admin |
| 145 |
password: SomeSecretPassword |
| 146 |
tenant: production |
| 147 |
state: present |
| 148 |
delegate_to: localhost |
| 149 |
|
| 150 |
- name: Create a bridge domain |
| 151 |
cisco.aci.aci_bd: |
| 152 |
host: apic |
| 153 |
username: admin |
| 154 |
password: SomeSecretPassword |
| 155 |
tenant: production |
| 156 |
bd: database |
| 157 |
state: present |
| 158 |
delegate_to: localhost |
| 159 |
|
| 160 |
- name: Create a subnet |
| 161 |
cisco.aci.aci_bd_subnet: |
| 162 |
host: apic |
| 163 |
username: admin |
| 164 |
password: SomeSecretPassword |
| 165 |
tenant: production |
| 166 |
bd: database |
| 167 |
gateway: 10.1.1.1 |
| 168 |
mask: 24 |
| 169 |
state: present |
| 170 |
delegate_to: localhost |
| 171 |
|
| 172 |
- name: Create a subnet with options |
| 173 |
cisco.aci.aci_bd_subnet: |
| 174 |
host: apic |
| 175 |
username: admin |
| 176 |
password: SomeSecretPassword |
| 177 |
tenant: production |
| 178 |
bd: database |
| 179 |
subnet_name: sql |
| 180 |
gateway: 10.1.2.1 |
| 181 |
mask: 23 |
| 182 |
description: SQL Servers |
| 183 |
scope: public |
| 184 |
route_profile_l3_out: corp |
| 185 |
route_profile: corp_route_profile |
| 186 |
subnet_control: |
| 187 |
- no_gw |
| 188 |
- querier_ip |
| 189 |
state: present |
| 190 |
delegate_to: localhost |
| 191 |
|
| 192 |
- name: Update a subnets scope to private and shared |
| 193 |
cisco.aci.aci_bd_subnet: |
| 194 |
host: apic |
| 195 |
username: admin |
| 196 |
password: SomeSecretPassword |
| 197 |
tenant: production |
| 198 |
bd: database |
| 199 |
gateway: 10.1.1.1 |
| 200 |
mask: 24 |
| 201 |
scope: [private, shared] |
| 202 |
state: present |
| 203 |
delegate_to: localhost |
| 204 |
|
| 205 |
- name: Get all subnets |
| 206 |
cisco.aci.aci_bd_subnet: |
| 207 |
host: apic |
| 208 |
username: admin |
| 209 |
password: SomeSecretPassword |
| 210 |
state: query |
| 211 |
delegate_to: localhost |
| 212 |
|
| 213 |
- name: Get all subnets of specific gateway in specified tenant |
| 214 |
cisco.aci.aci_bd_subnet: |
| 215 |
host: apic |
| 216 |
username: admin |
| 217 |
password: SomeSecretPassword |
| 218 |
tenant: production |
| 219 |
gateway: 10.1.1.1 |
| 220 |
mask: 24 |
| 221 |
state: query |
| 222 |
delegate_to: localhost |
| 223 |
register: query_result |
| 224 |
|
| 225 |
- name: Get specific subnet |
| 226 |
cisco.aci.aci_bd_subnet: |
| 227 |
host: apic |
| 228 |
username: admin |
| 229 |
password: SomeSecretPassword |
| 230 |
tenant: production |
| 231 |
bd: database |
| 232 |
gateway: 10.1.1.1 |
| 233 |
mask: 24 |
| 234 |
state: query |
| 235 |
delegate_to: localhost |
| 236 |
register: query_result |
| 237 |
|
| 238 |
- name: Delete a subnet |
| 239 |
cisco.aci.aci_bd_subnet: |
| 240 |
host: apic |
| 241 |
username: admin |
| 242 |
password: SomeSecretPassword |
| 243 |
tenant: production |
| 244 |
bd: database |
| 245 |
gateway: 10.1.1.1 |
| 246 |
mask: 24 |
| 247 |
state: absent |
| 248 |
delegate_to: localhost |
| 249 |
""" |
| 250 |
|
| 251 |
RETURN = r""" |
| 252 |
current: |
| 253 |
description: The existing configuration from the APIC after the module has finished |
| 254 |
returned: success |
| 255 |
type: list |
| 256 |
sample: |
| 257 |
[ |
| 258 |
{ |
| 259 |
"fvTenant": { |
| 260 |
"attributes": { |
| 261 |
"descr": "Production environment", |
| 262 |
"dn": "uni/tn-production", |
| 263 |
"name": "production", |
| 264 |
"nameAlias": "", |
| 265 |
"ownerKey": "", |
| 266 |
"ownerTag": "" |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
] |
| 271 |
error: |
| 272 |
description: The error information as returned from the APIC |
| 273 |
returned: failure |
| 274 |
type: dict |
| 275 |
sample: |
| 276 |
{ |
| 277 |
"code": "122", |
| 278 |
"text": "unknown managed object class foo" |
| 279 |
} |
| 280 |
raw: |
| 281 |
description: The raw output returned by the APIC REST API (xml or json) |
| 282 |
returned: parse error |
| 283 |
type: str |
| 284 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 285 |
sent: |
| 286 |
description: The actual/minimal configuration pushed to the APIC |
| 287 |
returned: info |
| 288 |
type: list |
| 289 |
sample: |
| 290 |
{ |
| 291 |
"fvTenant": { |
| 292 |
"attributes": { |
| 293 |
"descr": "Production environment" |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
previous: |
| 298 |
description: The original configuration from the APIC before the module has started |
| 299 |
returned: info |
| 300 |
type: list |
| 301 |
sample: |
| 302 |
[ |
| 303 |
{ |
| 304 |
"fvTenant": { |
| 305 |
"attributes": { |
| 306 |
"descr": "Production", |
| 307 |
"dn": "uni/tn-production", |
| 308 |
"name": "production", |
| 309 |
"nameAlias": "", |
| 310 |
"ownerKey": "", |
| 311 |
"ownerTag": "" |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
] |
| 316 |
proposed: |
| 317 |
description: The assembled configuration from the user-provided parameters |
| 318 |
returned: info |
| 319 |
type: dict |
| 320 |
sample: |
| 321 |
{ |
| 322 |
"fvTenant": { |
| 323 |
"attributes": { |
| 324 |
"descr": "Production environment", |
| 325 |
"name": "production" |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
filter_string: |
| 330 |
description: The filter string used for the request |
| 331 |
returned: failure or debug |
| 332 |
type: str |
| 333 |
sample: ?rsp-prop-include=config-only |
| 334 |
method: |
| 335 |
description: The HTTP method used for the request to the APIC |
| 336 |
returned: failure or debug |
| 337 |
type: str |
| 338 |
sample: POST |
| 339 |
response: |
| 340 |
description: The HTTP response from the APIC |
| 341 |
returned: failure or debug |
| 342 |
type: str |
| 343 |
sample: OK (30 bytes) |
| 344 |
status: |
| 345 |
description: The HTTP status from the APIC |
| 346 |
returned: failure or debug |
| 347 |
type: int |
| 348 |
sample: 200 |
| 349 |
url: |
| 350 |
description: The HTTP url used for the request to the APIC |
| 351 |
returned: failure or debug |
| 352 |
type: str |
| 353 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 354 |
""" |
| 355 |
|
| 356 |
import re |
| 357 |
from ansible.module_utils.basic import AnsibleModule |
| 358 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 359 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import SUBNET_CONTROL_MAPPING_BD_SUBNET, IPV4_REGEX |
| 360 |
|
| 361 |
|
| 362 |
def main(): |
| 363 |
argument_spec = aci_argument_spec() |
| 364 |
argument_spec.update(aci_annotation_spec()) |
| 365 |
argument_spec.update( |
| 366 |
bd=dict(type="str", aliases=["bd_name"]), # Not required for querying all objects |
| 367 |
description=dict(type="str", aliases=["descr"]), |
| 368 |
enable_vip=dict(type="bool"), |
| 369 |
gateway=dict(type="str", aliases=["gateway_ip"]), # Not required for querying all objects |
| 370 |
mask=dict(type="int", aliases=["subnet_mask"]), # Not required for querying all objects |
| 371 |
subnet_name=dict(type="str", aliases=["name"]), |
| 372 |
nd_prefix_policy=dict(type="str"), |
| 373 |
preferred=dict(type="bool"), |
| 374 |
route_profile=dict(type="str"), |
| 375 |
route_profile_l3_out=dict(type="str"), |
| 376 |
scope=dict(type="list", elements="str", choices=["private", "public", "shared"]), |
| 377 |
subnet_control=dict(type="list", elements="str", choices=list(SUBNET_CONTROL_MAPPING_BD_SUBNET)), |
| 378 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 379 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 380 |
name_alias=dict(type="str"), |
| 381 |
ip_data_plane_learning=dict(type="str", choices=["enabled", "disabled"], aliases=["ip_dataplane_learning"]), |
| 382 |
) |
| 383 |
|
| 384 |
module = AnsibleModule( |
| 385 |
argument_spec=argument_spec, |
| 386 |
supports_check_mode=True, |
| 387 |
required_together=[["gateway", "mask"]], |
| 388 |
required_if=[ |
| 389 |
["state", "present", ["bd", "gateway", "mask", "tenant"]], |
| 390 |
["state", "absent", ["bd", "gateway", "mask", "tenant"]], |
| 391 |
], |
| 392 |
) |
| 393 |
|
| 394 |
aci = ACIModule(module) |
| 395 |
|
| 396 |
description = module.params.get("description") |
| 397 |
enable_vip = aci.boolean(module.params.get("enable_vip")) |
| 398 |
tenant = module.params.get("tenant") |
| 399 |
bd = module.params.get("bd") |
| 400 |
gateway = module.params.get("gateway") |
| 401 |
mask = module.params.get("mask") |
| 402 |
|
| 403 |
if mask: |
| 404 |
# Assumption for simplicity of code that when a valid IPv4 address is not provided the intend is IPv6. |
| 405 |
if re.search(IPV4_REGEX, gateway) and mask not in range(0, 33): |
| 406 |
module.fail_json(msg="Valid Subnet Masks are 0 to 32 for IPv4 Addresses") |
| 407 |
elif mask not in range(0, 129): |
| 408 |
module.fail_json(msg="Valid Subnet Masks are 0 to 128 for IPv6 Addresses") |
| 409 |
|
| 410 |
if gateway is not None: |
| 411 |
gateway = "{0}/{1}".format(gateway, str(mask)) |
| 412 |
subnet_name = module.params.get("subnet_name") |
| 413 |
nd_prefix_policy = module.params.get("nd_prefix_policy") |
| 414 |
preferred = aci.boolean(module.params.get("preferred")) |
| 415 |
route_profile = module.params.get("route_profile") |
| 416 |
route_profile_l3_out = module.params.get("route_profile_l3_out") |
| 417 |
scope = module.params.get("scope") |
| 418 |
if scope is not None: |
| 419 |
if "private" in scope and "public" in scope: |
| 420 |
module.fail_json(msg="Parameter 'scope' cannot be both 'private' and 'public', got: %s" % scope) |
| 421 |
else: |
| 422 |
scope = ",".join(sorted(scope)) |
| 423 |
state = module.params.get("state") |
| 424 |
subnet_control = module.params.get("subnet_control") |
| 425 |
if subnet_control: |
| 426 |
if len(subnet_control) > 1 and "unspecified" in subnet_control: |
| 427 |
module.fail_json(msg="Parameter 'subnet_control' cannot contain 'unspecified' in combination with other values, got: %s" % subnet_control) |
| 428 |
subnet_control = ",".join(sorted([SUBNET_CONTROL_MAPPING_BD_SUBNET[ctrl] for ctrl in subnet_control])) |
| 429 |
name_alias = module.params.get("name_alias") |
| 430 |
ip_data_plane_learning = module.params.get("ip_data_plane_learning") |
| 431 |
aci.construct_url( |
| 432 |
root_class=dict( |
| 433 |
aci_class="fvTenant", |
| 434 |
aci_rn="tn-{0}".format(tenant), |
| 435 |
module_object=tenant, |
| 436 |
target_filter={"name": tenant}, |
| 437 |
), |
| 438 |
subclass_1=dict( |
| 439 |
aci_class="fvBD", |
| 440 |
aci_rn="BD-{0}".format(bd), |
| 441 |
module_object=bd, |
| 442 |
target_filter={"name": bd}, |
| 443 |
), |
| 444 |
subclass_2=dict( |
| 445 |
aci_class="fvSubnet", |
| 446 |
aci_rn="subnet-[{0}]".format(gateway), |
| 447 |
module_object=gateway, |
| 448 |
target_filter={"ip": gateway}, |
| 449 |
), |
| 450 |
child_classes=["fvRsBDSubnetToProfile", "fvRsNdPfxPol"], |
| 451 |
) |
| 452 |
|
| 453 |
aci.get_existing() |
| 454 |
|
| 455 |
if state == "present": |
| 456 |
class_config = dict( |
| 457 |
ctrl=subnet_control, |
| 458 |
descr=description, |
| 459 |
ip=gateway, |
| 460 |
name=subnet_name, |
| 461 |
preferred=preferred, |
| 462 |
scope=scope, |
| 463 |
virtual=enable_vip, |
| 464 |
nameAlias=name_alias, |
| 465 |
) |
| 466 |
|
| 467 |
if ip_data_plane_learning: |
| 468 |
class_config["ipDPLearning"] = ip_data_plane_learning |
| 469 |
|
| 470 |
aci.payload( |
| 471 |
aci_class="fvSubnet", |
| 472 |
class_config=class_config, |
| 473 |
child_configs=[ |
| 474 |
{"fvRsBDSubnetToProfile": {"attributes": {"tnL3extOutName": route_profile_l3_out, "tnRtctrlProfileName": route_profile}}}, |
| 475 |
{"fvRsNdPfxPol": {"attributes": {"tnNdPfxPolName": nd_prefix_policy}}}, |
| 476 |
], |
| 477 |
) |
| 478 |
|
| 479 |
aci.get_diff(aci_class="fvSubnet") |
| 480 |
|
| 481 |
aci.post_config() |
| 482 |
|
| 483 |
elif state == "absent": |
| 484 |
aci.delete_config() |
| 485 |
|
| 486 |
aci.exit_json() |
| 487 |
|
| 488 |
|
| 489 |
if __name__ == "__main__": |
| 490 |
main() |