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