| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, nkatarmal-crest <[email protected]> |
| 5 |
# Copyright: (c) 2020, Cindy Zhao <[email protected]> |
| 6 |
# Copyright: (c) 2024, Samita Bhattacharjee <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_cloud_subnet |
| 16 |
short_description: Manage Cloud Subnet (cloud:Subnet) |
| 17 |
description: |
| 18 |
- Manage Cloud Subnet on Cisco Cloud ACI. |
| 19 |
notes: |
| 20 |
- More information about the internal APIC class B(cloud:Subnet) from |
| 21 |
L(the APIC Management Information Model reference,https://developer.cisco.com/docs/apic-mim-ref/). |
| 22 |
author: |
| 23 |
- Nirav (@nirav) |
| 24 |
- Cindy Zhao (@cizhao) |
| 25 |
- Samita Bhattacharjee (@samitab) |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the Cloud Subnet. |
| 30 |
type: str |
| 31 |
aliases: [subnet] |
| 32 |
description: |
| 33 |
description: |
| 34 |
- Description of the Cloud Subnet. |
| 35 |
type: str |
| 36 |
address: |
| 37 |
description: |
| 38 |
- Ip address of the Cloud Subnet. |
| 39 |
type: str |
| 40 |
aliases: [ip] |
| 41 |
name_alias: |
| 42 |
description: |
| 43 |
- An alias for the name of the current object. This relates to the nameAlias field in ACI and is used to rename object without changing the DN. |
| 44 |
type: str |
| 45 |
tenant: |
| 46 |
description: |
| 47 |
- The name of tenant. |
| 48 |
type: str |
| 49 |
required: true |
| 50 |
cloud_context_profile: |
| 51 |
description: |
| 52 |
- The name of cloud context profile. |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
cidr: |
| 56 |
description: |
| 57 |
- Address of cloud cidr. |
| 58 |
type: str |
| 59 |
required: true |
| 60 |
aws_availability_zone: |
| 61 |
description: |
| 62 |
- The cloud zone which is attached to the given cloud context profile. |
| 63 |
- Only used when it is an AWS Cloud APIC. |
| 64 |
type: str |
| 65 |
aliases: [availability_zone, av_zone, zone] |
| 66 |
vnet_gateway: |
| 67 |
description: |
| 68 |
- Determine if a vNet Gateway Router will be deployed or not. |
| 69 |
- Only used when it is an Azure Cloud APIC. |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
azure_region: |
| 73 |
description: |
| 74 |
- The Azure cloud region to attach this subnet to. |
| 75 |
- Only used when it is an Azure Cloud APIC. |
| 76 |
type: str |
| 77 |
aliases: [az_region] |
| 78 |
state: |
| 79 |
description: |
| 80 |
- Use C(present) or C(absent) for adding or removing. |
| 81 |
- Use C(query) for listing an object or multiple objects. |
| 82 |
choices: [ absent, present, query ] |
| 83 |
default: present |
| 84 |
type: str |
| 85 |
extends_documentation_fragment: |
| 86 |
- cisco.aci.aci |
| 87 |
- cisco.aci.annotation |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Create AWS aci cloud subnet |
| 92 |
cisco.aci.aci_cloud_subnet: |
| 93 |
host: apic |
| 94 |
username: userName |
| 95 |
password: somePassword |
| 96 |
validate_certs: false |
| 97 |
tenant: anstest |
| 98 |
cloud_context_profile: aws_cloudCtxProfile |
| 99 |
cidr: '10.10.0.0/16' |
| 100 |
aws_availability_zone: us-west-1a |
| 101 |
address: 10.10.0.1 |
| 102 |
delegate_to: localhost |
| 103 |
|
| 104 |
- name: Create Azure aci cloud subnet |
| 105 |
cisco.aci.aci_cloud_subnet: |
| 106 |
host: apic |
| 107 |
username: userName |
| 108 |
password: somePassword |
| 109 |
validate_certs: false |
| 110 |
tenant: anstest |
| 111 |
cloud_context_profile: azure_cloudCtxProfile |
| 112 |
cidr: '10.10.0.0/16' |
| 113 |
azure_region: westus2 |
| 114 |
vnet_gateway: true |
| 115 |
address: 10.10.0.1 |
| 116 |
delegate_to: localhost |
| 117 |
|
| 118 |
- name: Query a specific subnet |
| 119 |
cisco.aci.aci_cloud_subnet: |
| 120 |
host: apic |
| 121 |
username: userName |
| 122 |
password: somePassword |
| 123 |
validate_certs: false |
| 124 |
tenant: anstest |
| 125 |
cloud_context_profile: ctx_profile_1 |
| 126 |
cidr: '10.10.0.0/16' |
| 127 |
address: 10.10.0.1 |
| 128 |
state: query |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Query all subnets |
| 132 |
cisco.aci.aci_cloud_subnet: |
| 133 |
host: apic |
| 134 |
username: userName |
| 135 |
password: somePassword |
| 136 |
validate_certs: false |
| 137 |
tenant: anstest |
| 138 |
cloud_context_profile: ctx_profile_1 |
| 139 |
cidr: '10.10.0.0/16' |
| 140 |
state: query |
| 141 |
delegate_to: localhost |
| 142 |
""" |
| 143 |
|
| 144 |
RETURN = r""" |
| 145 |
current: |
| 146 |
description: The existing configuration from the APIC after the module has finished |
| 147 |
returned: success |
| 148 |
type: list |
| 149 |
sample: |
| 150 |
[ |
| 151 |
{ |
| 152 |
"fvTenant": { |
| 153 |
"attributes": { |
| 154 |
"descr": "Production environment", |
| 155 |
"dn": "uni/tn-production", |
| 156 |
"name": "production", |
| 157 |
"nameAlias": "", |
| 158 |
"ownerKey": "", |
| 159 |
"ownerTag": "" |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
] |
| 164 |
error: |
| 165 |
description: The error information as returned from the APIC |
| 166 |
returned: failure |
| 167 |
type: dict |
| 168 |
sample: |
| 169 |
{ |
| 170 |
"code": "122", |
| 171 |
"text": "unknown managed object class foo" |
| 172 |
} |
| 173 |
raw: |
| 174 |
description: The raw output returned by the APIC REST API (xml or json) |
| 175 |
returned: parse error |
| 176 |
type: str |
| 177 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 178 |
sent: |
| 179 |
description: The actual/minimal configuration pushed to the APIC |
| 180 |
returned: info |
| 181 |
type: list |
| 182 |
sample: |
| 183 |
{ |
| 184 |
"fvTenant": { |
| 185 |
"attributes": { |
| 186 |
"descr": "Production environment" |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
previous: |
| 191 |
description: The original configuration from the APIC before the module has started |
| 192 |
returned: info |
| 193 |
type: list |
| 194 |
sample: |
| 195 |
[ |
| 196 |
{ |
| 197 |
"fvTenant": { |
| 198 |
"attributes": { |
| 199 |
"descr": "Production", |
| 200 |
"dn": "uni/tn-production", |
| 201 |
"name": "production", |
| 202 |
"nameAlias": "", |
| 203 |
"ownerKey": "", |
| 204 |
"ownerTag": "" |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
] |
| 209 |
proposed: |
| 210 |
description: The assembled configuration from the user-provided parameters |
| 211 |
returned: info |
| 212 |
type: dict |
| 213 |
sample: |
| 214 |
{ |
| 215 |
"fvTenant": { |
| 216 |
"attributes": { |
| 217 |
"descr": "Production environment", |
| 218 |
"name": "production" |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
filter_string: |
| 223 |
description: The filter string used for the request |
| 224 |
returned: failure or debug |
| 225 |
type: str |
| 226 |
sample: ?rsp-prop-include=config-only |
| 227 |
method: |
| 228 |
description: The HTTP method used for the request to the APIC |
| 229 |
returned: failure or debug |
| 230 |
type: str |
| 231 |
sample: POST |
| 232 |
response: |
| 233 |
description: The HTTP response from the APIC |
| 234 |
returned: failure or debug |
| 235 |
type: str |
| 236 |
sample: OK (30 bytes) |
| 237 |
status: |
| 238 |
description: The HTTP status from the APIC |
| 239 |
returned: failure or debug |
| 240 |
type: int |
| 241 |
sample: 200 |
| 242 |
url: |
| 243 |
description: The HTTP url used for the request to the APIC |
| 244 |
returned: failure or debug |
| 245 |
type: str |
| 246 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 247 |
""" |
| 248 |
|
| 249 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 250 |
from ansible.module_utils.basic import AnsibleModule |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
argument_spec = aci_argument_spec() |
| 255 |
argument_spec.update(aci_annotation_spec()) |
| 256 |
argument_spec.update( |
| 257 |
name=dict(type="str", aliases=["subnet"]), |
| 258 |
description=dict(type="str"), |
| 259 |
address=dict(type="str", aliases=["ip"]), |
| 260 |
name_alias=dict(type="str"), |
| 261 |
vnet_gateway=dict(type="bool", default=False), |
| 262 |
tenant=dict(type="str", required=True), |
| 263 |
cloud_context_profile=dict(type="str", required=True), |
| 264 |
cidr=dict(type="str", required=True), |
| 265 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 266 |
aws_availability_zone=dict(type="str", aliases=["availability_zone", "av_zone", "zone"]), |
| 267 |
azure_region=dict(type="str", aliases=["az_region"]), |
| 268 |
) |
| 269 |
|
| 270 |
module = AnsibleModule( |
| 271 |
argument_spec=argument_spec, |
| 272 |
supports_check_mode=True, |
| 273 |
required_if=[ |
| 274 |
["state", "absent", ["tenant", "cloud_context_profile", "cidr", "address"]], |
| 275 |
["state", "present", ["tenant", "cloud_context_profile", "cidr", "address"]], |
| 276 |
], |
| 277 |
mutually_exclusive=[("aws_availability_zone", "azure_region")], |
| 278 |
) |
| 279 |
|
| 280 |
name = module.params.get("name") |
| 281 |
description = module.params.get("description") |
| 282 |
address = module.params.get("address") |
| 283 |
name_alias = module.params.get("name_alias") |
| 284 |
vnet_gateway = module.params.get("vnet_gateway") |
| 285 |
tenant = module.params.get("tenant") |
| 286 |
cloud_context_profile = module.params.get("cloud_context_profile") |
| 287 |
cidr = module.params.get("cidr") |
| 288 |
state = module.params.get("state") |
| 289 |
aws_availability_zone = module.params.get("aws_availability_zone") |
| 290 |
azure_region = module.params.get("azure_region") |
| 291 |
child_configs = [] |
| 292 |
|
| 293 |
aci = ACIModule(module) |
| 294 |
aci.construct_url( |
| 295 |
root_class=dict(aci_class="fvTenant", aci_rn="tn-{0}".format(tenant), target_filter='eq(fvTenant.name, "{0}")'.format(tenant), module_object=tenant), |
| 296 |
subclass_1=dict( |
| 297 |
aci_class="cloudCtxProfile", |
| 298 |
aci_rn="ctxprofile-{0}".format(cloud_context_profile), |
| 299 |
target_filter='eq(cloudCtxProfile.name, "{0}")'.format(cloud_context_profile), |
| 300 |
module_object=cloud_context_profile, |
| 301 |
), |
| 302 |
subclass_2=dict(aci_class="cloudCidr", aci_rn="cidr-[{0}]".format(cidr), target_filter='eq(cloudCidr.addr, "{0}")'.format(cidr), module_object=cidr), |
| 303 |
subclass_3=dict( |
| 304 |
aci_class="cloudSubnet", aci_rn="subnet-[{0}]".format(address), target_filter='eq(cloudSubnet.ip, "{0}")'.format(address), module_object=address |
| 305 |
), |
| 306 |
child_classes=["cloudRsZoneAttach"], |
| 307 |
) |
| 308 |
|
| 309 |
aci.get_existing() |
| 310 |
|
| 311 |
if state == "present": |
| 312 |
# in aws cloud apic |
| 313 |
if aws_availability_zone: |
| 314 |
region = aws_availability_zone[:-1] |
| 315 |
tDn = "uni/clouddomp/provp-aws/region-{0}/zone-{1}".format(region, aws_availability_zone) |
| 316 |
child_configs.append({"cloudRsZoneAttach": {"attributes": {"tDn": tDn}}}) |
| 317 |
# in azure cloud apic |
| 318 |
if azure_region: |
| 319 |
tDn = "uni/clouddomp/provp-azure/region-{0}/zone-default".format(azure_region) |
| 320 |
child_configs.append({"cloudRsZoneAttach": {"attributes": {"tDn": tDn}}}) |
| 321 |
if vnet_gateway: |
| 322 |
usage = "gateway" |
| 323 |
else: |
| 324 |
usage = "user" |
| 325 |
|
| 326 |
aci.payload( |
| 327 |
aci_class="cloudSubnet", |
| 328 |
class_config=dict( |
| 329 |
name=name, |
| 330 |
descr=description, |
| 331 |
ip=address, |
| 332 |
nameAlias=name_alias, |
| 333 |
scope="private", |
| 334 |
usage=usage, |
| 335 |
), |
| 336 |
child_configs=child_configs, |
| 337 |
) |
| 338 |
|
| 339 |
aci.get_diff(aci_class="cloudSubnet") |
| 340 |
|
| 341 |
aci.post_config() |
| 342 |
|
| 343 |
elif state == "absent": |
| 344 |
aci.delete_config() |
| 345 |
|
| 346 |
aci.exit_json() |
| 347 |
|
| 348 |
|
| 349 |
if __name__ == "__main__": |
| 350 |
main() |