| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Dag Wieers <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_domain_to_vlan_pool |
| 16 |
short_description: Bind Domain to VLAN Pools (infra:RsVlanNs) |
| 17 |
description: |
| 18 |
- Bind Domain to VLAN Pools on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
domain: |
| 21 |
description: |
| 22 |
- Name of the domain being associated with the VLAN Pool. |
| 23 |
type: str |
| 24 |
aliases: [ domain_name, domain_profile ] |
| 25 |
domain_type: |
| 26 |
description: |
| 27 |
- Determines if the Domain is physical (phys) or virtual (vmm). |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
choices: [ fc, l2dom, l3dom, phys, vmm ] |
| 31 |
pool: |
| 32 |
description: |
| 33 |
- The name of the pool. |
| 34 |
type: str |
| 35 |
aliases: [ pool_name, vlan_pool ] |
| 36 |
pool_allocation_mode: |
| 37 |
description: |
| 38 |
- The method used for allocating VLANs to resources. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
choices: [ dynamic, static] |
| 42 |
aliases: [ allocation_mode, mode ] |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Use C(present) or C(absent) for adding or removing. |
| 46 |
- Use C(query) for listing an object or multiple objects. |
| 47 |
type: str |
| 48 |
choices: [ absent, present, query ] |
| 49 |
default: present |
| 50 |
vm_provider: |
| 51 |
description: |
| 52 |
- The VM platform for VMM Domains. |
| 53 |
- Support for Kubernetes was added in ACI v3.0. |
| 54 |
- Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1. |
| 55 |
type: str |
| 56 |
choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix] |
| 57 |
extends_documentation_fragment: |
| 58 |
- cisco.aci.aci |
| 59 |
- cisco.aci.annotation |
| 60 |
|
| 61 |
notes: |
| 62 |
- The C(domain) and C(vlan_pool) parameters should exist before using this module. |
| 63 |
The M(cisco.aci.aci_domain) and M(cisco.aci.aci_vlan_pool) can be used for these. |
| 64 |
seealso: |
| 65 |
- module: cisco.aci.aci_domain |
| 66 |
- module: cisco.aci.aci_vlan_pool |
| 67 |
- name: APIC Management Information Model reference |
| 68 |
description: More information about the internal APIC class B(infra:RsVlanNs). |
| 69 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 70 |
author: |
| 71 |
- Dag Wieers (@dagwieers) |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Bind a VMM domain to VLAN pool |
| 76 |
cisco.aci.aci_domain_to_vlan_pool: |
| 77 |
host: apic |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
domain: vmw_dom |
| 81 |
domain_type: vmm |
| 82 |
pool: vmw_pool |
| 83 |
pool_allocation_mode: dynamic |
| 84 |
vm_provider: vmware |
| 85 |
state: present |
| 86 |
delegate_to: localhost |
| 87 |
|
| 88 |
- name: Remove a VMM domain to VLAN pool binding |
| 89 |
cisco.aci.aci_domain_to_vlan_pool: |
| 90 |
host: apic |
| 91 |
username: admin |
| 92 |
password: SomeSecretPassword |
| 93 |
domain: vmw_dom |
| 94 |
domain_type: vmm |
| 95 |
pool: vmw_pool |
| 96 |
pool_allocation_mode: dynamic |
| 97 |
vm_provider: vmware |
| 98 |
state: absent |
| 99 |
delegate_to: localhost |
| 100 |
|
| 101 |
- name: Bind a physical domain to VLAN pool |
| 102 |
cisco.aci.aci_domain_to_vlan_pool: |
| 103 |
host: apic |
| 104 |
username: admin |
| 105 |
password: SomeSecretPassword |
| 106 |
domain: phys_dom |
| 107 |
domain_type: phys |
| 108 |
pool: phys_pool |
| 109 |
pool_allocation_mode: static |
| 110 |
state: present |
| 111 |
delegate_to: localhost |
| 112 |
|
| 113 |
- name: Bind a physical domain to VLAN pool |
| 114 |
cisco.aci.aci_domain_to_vlan_pool: |
| 115 |
host: apic |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
domain: phys_dom |
| 119 |
domain_type: phys |
| 120 |
pool: phys_pool |
| 121 |
pool_allocation_mode: static |
| 122 |
state: absent |
| 123 |
delegate_to: localhost |
| 124 |
|
| 125 |
- name: Query an domain to VLAN pool binding |
| 126 |
cisco.aci.aci_domain_to_vlan_pool: |
| 127 |
host: apic |
| 128 |
username: admin |
| 129 |
password: SomeSecretPassword |
| 130 |
domain: phys_dom |
| 131 |
domain_type: phys |
| 132 |
pool: phys_pool |
| 133 |
pool_allocation_mode: static |
| 134 |
state: query |
| 135 |
delegate_to: localhost |
| 136 |
register: query_result |
| 137 |
|
| 138 |
- name: Query all domain to VLAN pool bindings |
| 139 |
cisco.aci.aci_domain_to_vlan_pool: |
| 140 |
host: apic |
| 141 |
username: admin |
| 142 |
password: SomeSecretPassword |
| 143 |
domain_type: phys |
| 144 |
state: query |
| 145 |
delegate_to: localhost |
| 146 |
register: query_result |
| 147 |
""" |
| 148 |
|
| 149 |
RETURN = r""" |
| 150 |
current: |
| 151 |
description: The existing configuration from the APIC after the module has finished |
| 152 |
returned: success |
| 153 |
type: list |
| 154 |
sample: |
| 155 |
[ |
| 156 |
{ |
| 157 |
"fvTenant": { |
| 158 |
"attributes": { |
| 159 |
"descr": "Production environment", |
| 160 |
"dn": "uni/tn-production", |
| 161 |
"name": "production", |
| 162 |
"nameAlias": "", |
| 163 |
"ownerKey": "", |
| 164 |
"ownerTag": "" |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
] |
| 169 |
error: |
| 170 |
description: The error information as returned from the APIC |
| 171 |
returned: failure |
| 172 |
type: dict |
| 173 |
sample: |
| 174 |
{ |
| 175 |
"code": "122", |
| 176 |
"text": "unknown managed object class foo" |
| 177 |
} |
| 178 |
raw: |
| 179 |
description: The raw output returned by the APIC REST API (xml or json) |
| 180 |
returned: parse error |
| 181 |
type: str |
| 182 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 183 |
sent: |
| 184 |
description: The actual/minimal configuration pushed to the APIC |
| 185 |
returned: info |
| 186 |
type: list |
| 187 |
sample: |
| 188 |
{ |
| 189 |
"fvTenant": { |
| 190 |
"attributes": { |
| 191 |
"descr": "Production environment" |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
previous: |
| 196 |
description: The original configuration from the APIC before the module has started |
| 197 |
returned: info |
| 198 |
type: list |
| 199 |
sample: |
| 200 |
[ |
| 201 |
{ |
| 202 |
"fvTenant": { |
| 203 |
"attributes": { |
| 204 |
"descr": "Production", |
| 205 |
"dn": "uni/tn-production", |
| 206 |
"name": "production", |
| 207 |
"nameAlias": "", |
| 208 |
"ownerKey": "", |
| 209 |
"ownerTag": "" |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
] |
| 214 |
proposed: |
| 215 |
description: The assembled configuration from the user-provided parameters |
| 216 |
returned: info |
| 217 |
type: dict |
| 218 |
sample: |
| 219 |
{ |
| 220 |
"fvTenant": { |
| 221 |
"attributes": { |
| 222 |
"descr": "Production environment", |
| 223 |
"name": "production" |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
filter_string: |
| 228 |
description: The filter string used for the request |
| 229 |
returned: failure or debug |
| 230 |
type: str |
| 231 |
sample: ?rsp-prop-include=config-only |
| 232 |
method: |
| 233 |
description: The HTTP method used for the request to the APIC |
| 234 |
returned: failure or debug |
| 235 |
type: str |
| 236 |
sample: POST |
| 237 |
response: |
| 238 |
description: The HTTP response from the APIC |
| 239 |
returned: failure or debug |
| 240 |
type: str |
| 241 |
sample: OK (30 bytes) |
| 242 |
status: |
| 243 |
description: The HTTP status from the APIC |
| 244 |
returned: failure or debug |
| 245 |
type: int |
| 246 |
sample: 200 |
| 247 |
url: |
| 248 |
description: The HTTP url used for the request to the APIC |
| 249 |
returned: failure or debug |
| 250 |
type: str |
| 251 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 252 |
""" |
| 253 |
|
| 254 |
from ansible.module_utils.basic import AnsibleModule |
| 255 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 256 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import VM_PROVIDER_MAPPING |
| 257 |
|
| 258 |
|
| 259 |
def main(): |
| 260 |
argument_spec = aci_argument_spec() |
| 261 |
argument_spec.update(aci_annotation_spec()) |
| 262 |
argument_spec.update( |
| 263 |
domain_type=dict(type="str", required=True, choices=["fc", "l2dom", "l3dom", "phys", "vmm"]), |
| 264 |
domain=dict(type="str", aliases=["domain_name", "domain_profile"]), # Not required for querying all objects |
| 265 |
pool=dict(type="str", aliases=["pool_name", "vlan_pool"]), # Not required for querying all objects |
| 266 |
pool_allocation_mode=dict(type="str", required=True, aliases=["allocation_mode", "mode"], choices=["dynamic", "static"]), |
| 267 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 268 |
vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)), |
| 269 |
) |
| 270 |
|
| 271 |
module = AnsibleModule( |
| 272 |
argument_spec=argument_spec, |
| 273 |
supports_check_mode=True, |
| 274 |
required_if=[ |
| 275 |
["domain_type", "vmm", ["vm_provider"]], |
| 276 |
["state", "absent", ["domain", "domain_type", "pool"]], |
| 277 |
["state", "present", ["domain", "domain_type", "pool"]], |
| 278 |
], |
| 279 |
) |
| 280 |
|
| 281 |
domain = module.params.get("domain") |
| 282 |
domain_type = module.params.get("domain_type") |
| 283 |
pool = module.params.get("pool") |
| 284 |
pool_allocation_mode = module.params.get("pool_allocation_mode") |
| 285 |
vm_provider = module.params.get("vm_provider") |
| 286 |
state = module.params.get("state") |
| 287 |
|
| 288 |
# Report when vm_provider is set when type is not virtual |
| 289 |
if domain_type != "vmm" and vm_provider is not None: |
| 290 |
module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".format(domain_type)) |
| 291 |
|
| 292 |
# ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static) |
| 293 |
pool_name = pool |
| 294 |
if pool is not None: |
| 295 |
pool_name = "[{0}]-{1}".format(pool, pool_allocation_mode) |
| 296 |
|
| 297 |
# Compile the full domain for URL building |
| 298 |
if domain_type == "fc": |
| 299 |
domain_class = "fcDomP" |
| 300 |
domain_mo = "uni/fc-{0}".format(domain) |
| 301 |
domain_rn = "fc-{0}".format(domain) |
| 302 |
elif domain_type == "l2dom": |
| 303 |
domain_class = "l2extDomP" |
| 304 |
domain_mo = "uni/l2dom-{0}".format(domain) |
| 305 |
domain_rn = "l2dom-{0}".format(domain) |
| 306 |
elif domain_type == "l3dom": |
| 307 |
domain_class = "l3extDomP" |
| 308 |
domain_mo = "uni/l3dom-{0}".format(domain) |
| 309 |
domain_rn = "l3dom-{0}".format(domain) |
| 310 |
elif domain_type == "phys": |
| 311 |
domain_class = "physDomP" |
| 312 |
domain_mo = "uni/phys-{0}".format(domain) |
| 313 |
domain_rn = "phys-{0}".format(domain) |
| 314 |
else: |
| 315 |
domain_class = "vmmDomP" |
| 316 |
domain_mo = "uni/vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING[vm_provider], domain) |
| 317 |
domain_rn = "vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING[vm_provider], domain) |
| 318 |
|
| 319 |
# Ensure that querying all objects works when only domain_type is provided |
| 320 |
if domain is None: |
| 321 |
domain_mo = None |
| 322 |
|
| 323 |
aci_mo = "uni/infra/vlanns-{0}".format(pool_name) |
| 324 |
|
| 325 |
aci = ACIModule(module) |
| 326 |
aci.construct_url( |
| 327 |
root_class=dict( |
| 328 |
aci_class=domain_class, |
| 329 |
aci_rn=domain_rn, |
| 330 |
module_object=domain_mo, |
| 331 |
target_filter={"name": domain}, |
| 332 |
), |
| 333 |
subclass_1=dict( |
| 334 |
aci_class="infraRsVlanNs", |
| 335 |
aci_rn="rsvlanNs", |
| 336 |
module_object=aci_mo if pool_name else None, |
| 337 |
target_filter={"tDn": aci_mo} if pool_name else {}, |
| 338 |
), |
| 339 |
) |
| 340 |
|
| 341 |
aci.get_existing() |
| 342 |
|
| 343 |
if state == "present": |
| 344 |
aci.payload( |
| 345 |
aci_class="infraRsVlanNs", |
| 346 |
class_config=dict(tDn=aci_mo), |
| 347 |
) |
| 348 |
|
| 349 |
aci.get_diff(aci_class="infraRsVlanNs") |
| 350 |
|
| 351 |
aci.post_config() |
| 352 |
|
| 353 |
elif state == "absent": |
| 354 |
aci.delete_config() |
| 355 |
|
| 356 |
aci.exit_json() |
| 357 |
|
| 358 |
|
| 359 |
if __name__ == "__main__": |
| 360 |
main() |