| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 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_domain |
| 15 |
short_description: Manage physical, virtual, bridged, routed or FC domain profiles (phys:DomP, vmm:DomP, l2ext:DomP, l3ext:DomP, and fc:DomP) |
| 16 |
description: |
| 17 |
- Manage physical, virtual, bridged, routed or FC domain profiles on Cisco ACI fabrics. |
| 18 |
options: |
| 19 |
domain: |
| 20 |
description: |
| 21 |
- Name of the physical, virtual, bridged routed or FC domain profile. |
| 22 |
type: str |
| 23 |
aliases: [ domain_name, domain_profile, name ] |
| 24 |
domain_type: |
| 25 |
description: |
| 26 |
- The type of domain profile. |
| 27 |
- 'C(fc): The FC domain profile is a policy pertaining to single FC Management domain' |
| 28 |
- 'C(l2dom): The external bridged domain profile is a policy for managing L2 bridged infrastructure bridged outside the fabric.' |
| 29 |
- 'C(l3dom): The external routed domain profile is a policy for managing L3 routed infrastructure outside the fabric.' |
| 30 |
- 'C(phys): The physical domain profile stores the physical resources and encap resources that should be used for EPGs associated with this domain.' |
| 31 |
- 'C(vmm): The VMM domain profile is a policy for grouping VM controllers with similar networking policy requirements.' |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
choices: [ fc, l2dom, l3dom, phys, vmm ] |
| 35 |
aliases: [ type ] |
| 36 |
dscp: |
| 37 |
description: |
| 38 |
- The target Differentiated Service (DSCP) value. |
| 39 |
- The APIC defaults to C(unspecified) when unset during creation. |
| 40 |
type: str |
| 41 |
choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ] |
| 42 |
aliases: [ target ] |
| 43 |
encap_mode: |
| 44 |
description: |
| 45 |
- The layer 2 encapsulation protocol to use with the virtual switch. |
| 46 |
type: str |
| 47 |
choices: [ unknown, vlan, vxlan ] |
| 48 |
add_infra_pg: |
| 49 |
description: |
| 50 |
- Configure port groups for infra VLAN (e.g. Virtual APIC). |
| 51 |
type: bool |
| 52 |
aliases: [ infra_pg ] |
| 53 |
tag_collection: |
| 54 |
description: |
| 55 |
- Enables Cisco APIC to collect VMs that have been assigned tags in VMware vCenter for microsegmentation. |
| 56 |
type: bool |
| 57 |
multicast_address: |
| 58 |
description: |
| 59 |
- The multicast IP address to use for the virtual switch. |
| 60 |
type: str |
| 61 |
state: |
| 62 |
description: |
| 63 |
- Use C(present) or C(absent) for adding or removing. |
| 64 |
- Use C(query) for listing an object or multiple objects. |
| 65 |
type: str |
| 66 |
choices: [ absent, present, query ] |
| 67 |
default: present |
| 68 |
name_alias: |
| 69 |
description: |
| 70 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 71 |
type: str |
| 72 |
vm_provider: |
| 73 |
description: |
| 74 |
- The VM platform for VMM Domains. |
| 75 |
- Support for Kubernetes was added in ACI v3.0. |
| 76 |
- Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1. |
| 77 |
type: str |
| 78 |
choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ] |
| 79 |
vswitch: |
| 80 |
description: |
| 81 |
- The virtual switch to use for vmm domains. |
| 82 |
- The APIC defaults to C(default) when unset during creation. |
| 83 |
type: str |
| 84 |
choices: [ avs, default, dvs, unknown ] |
| 85 |
access_mode: |
| 86 |
description: |
| 87 |
- Access mode for vmm domains |
| 88 |
- This parameter cannot be changed after a domain is created |
| 89 |
type: str |
| 90 |
choices: [ read-only, read-write ] |
| 91 |
enable_vm_folder: |
| 92 |
description: |
| 93 |
- Enable VM folder data retrieval |
| 94 |
type: bool |
| 95 |
extends_documentation_fragment: |
| 96 |
- cisco.aci.aci |
| 97 |
- cisco.aci.annotation |
| 98 |
- cisco.aci.owner |
| 99 |
|
| 100 |
seealso: |
| 101 |
- module: cisco.aci.aci_aep_to_domain |
| 102 |
- module: cisco.aci.aci_domain_to_encap_pool |
| 103 |
- module: cisco.aci.aci_domain_to_vlan_pool |
| 104 |
- name: APIC Management Information Model reference |
| 105 |
description: More information about the internal APIC classes B(phys:DomP), |
| 106 |
B(vmm:DomP), B(l2ext:DomP), B(l3ext:DomP) and B(fc:DomP) |
| 107 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 108 |
author: |
| 109 |
- Dag Wieers (@dagwieers) |
| 110 |
""" |
| 111 |
|
| 112 |
EXAMPLES = r""" |
| 113 |
- name: Add a new physical domain |
| 114 |
cisco.aci.aci_domain: |
| 115 |
host: apic |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
domain: phys_dom |
| 119 |
domain_type: phys |
| 120 |
state: present |
| 121 |
|
| 122 |
- name: Remove a physical domain |
| 123 |
cisco.aci.aci_domain: |
| 124 |
host: apic |
| 125 |
username: admin |
| 126 |
password: SomeSecretPassword |
| 127 |
domain: phys_dom |
| 128 |
domain_type: phys |
| 129 |
state: absent |
| 130 |
|
| 131 |
- name: Add a new VMM domain |
| 132 |
cisco.aci.aci_domain: |
| 133 |
host: apic |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
domain: hyperv_dom |
| 137 |
domain_type: vmm |
| 138 |
vm_provider: microsoft |
| 139 |
state: present |
| 140 |
delegate_to: localhost |
| 141 |
|
| 142 |
- name: Remove a VMM domain |
| 143 |
cisco.aci.aci_domain: |
| 144 |
host: apic |
| 145 |
username: admin |
| 146 |
password: SomeSecretPassword |
| 147 |
domain: hyperv_dom |
| 148 |
domain_type: vmm |
| 149 |
vm_provider: microsoft |
| 150 |
state: absent |
| 151 |
delegate_to: localhost |
| 152 |
|
| 153 |
- name: Query a specific physical domain |
| 154 |
cisco.aci.aci_domain: |
| 155 |
host: apic |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
domain: phys_dom |
| 159 |
domain_type: phys |
| 160 |
state: query |
| 161 |
delegate_to: localhost |
| 162 |
register: query_result |
| 163 |
|
| 164 |
- name: Query all domains |
| 165 |
cisco.aci.aci_domain: |
| 166 |
host: apic |
| 167 |
username: admin |
| 168 |
password: SomeSecretPassword |
| 169 |
domain_type: phys |
| 170 |
state: query |
| 171 |
delegate_to: localhost |
| 172 |
register: query_result |
| 173 |
""" |
| 174 |
|
| 175 |
RETURN = r""" |
| 176 |
current: |
| 177 |
description: The existing configuration from the APIC after the module has finished |
| 178 |
returned: success |
| 179 |
type: list |
| 180 |
sample: |
| 181 |
[ |
| 182 |
{ |
| 183 |
"fvTenant": { |
| 184 |
"attributes": { |
| 185 |
"descr": "Production environment", |
| 186 |
"dn": "uni/tn-production", |
| 187 |
"name": "production", |
| 188 |
"nameAlias": "", |
| 189 |
"ownerKey": "", |
| 190 |
"ownerTag": "" |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
] |
| 195 |
error: |
| 196 |
description: The error information as returned from the APIC |
| 197 |
returned: failure |
| 198 |
type: dict |
| 199 |
sample: |
| 200 |
{ |
| 201 |
"code": "122", |
| 202 |
"text": "unknown managed object class foo" |
| 203 |
} |
| 204 |
raw: |
| 205 |
description: The raw output returned by the APIC REST API (xml or json) |
| 206 |
returned: parse error |
| 207 |
type: str |
| 208 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 209 |
sent: |
| 210 |
description: The actual/minimal configuration pushed to the APIC |
| 211 |
returned: info |
| 212 |
type: list |
| 213 |
sample: |
| 214 |
{ |
| 215 |
"fvTenant": { |
| 216 |
"attributes": { |
| 217 |
"descr": "Production environment" |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
previous: |
| 222 |
description: The original configuration from the APIC before the module has started |
| 223 |
returned: info |
| 224 |
type: list |
| 225 |
sample: |
| 226 |
[ |
| 227 |
{ |
| 228 |
"fvTenant": { |
| 229 |
"attributes": { |
| 230 |
"descr": "Production", |
| 231 |
"dn": "uni/tn-production", |
| 232 |
"name": "production", |
| 233 |
"nameAlias": "", |
| 234 |
"ownerKey": "", |
| 235 |
"ownerTag": "" |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
] |
| 240 |
proposed: |
| 241 |
description: The assembled configuration from the user-provided parameters |
| 242 |
returned: info |
| 243 |
type: dict |
| 244 |
sample: |
| 245 |
{ |
| 246 |
"fvTenant": { |
| 247 |
"attributes": { |
| 248 |
"descr": "Production environment", |
| 249 |
"name": "production" |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
filter_string: |
| 254 |
description: The filter string used for the request |
| 255 |
returned: failure or debug |
| 256 |
type: str |
| 257 |
sample: ?rsp-prop-include=config-only |
| 258 |
method: |
| 259 |
description: The HTTP method used for the request to the APIC |
| 260 |
returned: failure or debug |
| 261 |
type: str |
| 262 |
sample: POST |
| 263 |
response: |
| 264 |
description: The HTTP response from the APIC |
| 265 |
returned: failure or debug |
| 266 |
type: str |
| 267 |
sample: OK (30 bytes) |
| 268 |
status: |
| 269 |
description: The HTTP status from the APIC |
| 270 |
returned: failure or debug |
| 271 |
type: int |
| 272 |
sample: 200 |
| 273 |
url: |
| 274 |
description: The HTTP url used for the request to the APIC |
| 275 |
returned: failure or debug |
| 276 |
type: str |
| 277 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 278 |
""" |
| 279 |
|
| 280 |
from ansible.module_utils.basic import AnsibleModule |
| 281 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 282 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import VM_PROVIDER_MAPPING |
| 283 |
|
| 284 |
VSWITCH_MAPPING = dict( |
| 285 |
avs="n1kv", |
| 286 |
default="default", |
| 287 |
dvs="default", |
| 288 |
unknown="unknown", |
| 289 |
) |
| 290 |
|
| 291 |
BOOL_TO_ACI_MAPPING = {True: "yes", False: "no", None: None} |
| 292 |
|
| 293 |
|
| 294 |
def main(): |
| 295 |
argument_spec = aci_argument_spec() |
| 296 |
argument_spec.update(aci_annotation_spec()) |
| 297 |
argument_spec.update(aci_owner_spec()) |
| 298 |
argument_spec.update( |
| 299 |
domain_type=dict(type="str", required=True, choices=["fc", "l2dom", "l3dom", "phys", "vmm"], aliases=["type"]), |
| 300 |
domain=dict(type="str", aliases=["domain_name", "domain_profile", "name"]), # Not required for querying all objects |
| 301 |
dscp=dict( |
| 302 |
type="str", |
| 303 |
choices=[ |
| 304 |
"AF11", |
| 305 |
"AF12", |
| 306 |
"AF13", |
| 307 |
"AF21", |
| 308 |
"AF22", |
| 309 |
"AF23", |
| 310 |
"AF31", |
| 311 |
"AF32", |
| 312 |
"AF33", |
| 313 |
"AF41", |
| 314 |
"AF42", |
| 315 |
"AF43", |
| 316 |
"CS0", |
| 317 |
"CS1", |
| 318 |
"CS2", |
| 319 |
"CS3", |
| 320 |
"CS4", |
| 321 |
"CS5", |
| 322 |
"CS6", |
| 323 |
"CS7", |
| 324 |
"EF", |
| 325 |
"VA", |
| 326 |
"unspecified", |
| 327 |
], |
| 328 |
aliases=["target"], |
| 329 |
), |
| 330 |
encap_mode=dict(type="str", choices=["unknown", "vlan", "vxlan"]), |
| 331 |
add_infra_pg=dict(type="bool", aliases=["infra_pg"]), |
| 332 |
tag_collection=dict(type="bool"), |
| 333 |
multicast_address=dict(type="str"), |
| 334 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 335 |
vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)), |
| 336 |
vswitch=dict(type="str", choices=["avs", "default", "dvs", "unknown"]), |
| 337 |
name_alias=dict(type="str"), |
| 338 |
access_mode=dict(type="str", choices=["read-write", "read-only"]), |
| 339 |
enable_vm_folder=dict(type="bool"), |
| 340 |
) |
| 341 |
|
| 342 |
module = AnsibleModule( |
| 343 |
argument_spec=argument_spec, |
| 344 |
supports_check_mode=True, |
| 345 |
required_if=[ |
| 346 |
["domain_type", "vmm", ["vm_provider"]], |
| 347 |
["state", "absent", ["domain", "domain_type"]], |
| 348 |
["state", "present", ["domain", "domain_type"]], |
| 349 |
], |
| 350 |
) |
| 351 |
|
| 352 |
dscp = module.params.get("dscp") |
| 353 |
domain = module.params.get("domain") |
| 354 |
domain_type = module.params.get("domain_type") |
| 355 |
encap_mode = module.params.get("encap_mode") |
| 356 |
add_infra_pg = BOOL_TO_ACI_MAPPING[module.params.get("add_infra_pg")] |
| 357 |
tag_collection = BOOL_TO_ACI_MAPPING[module.params.get("tag_collection")] |
| 358 |
multicast_address = module.params.get("multicast_address") |
| 359 |
vm_provider = module.params.get("vm_provider") |
| 360 |
vswitch = module.params.get("vswitch") |
| 361 |
if vswitch is not None: |
| 362 |
vswitch = VSWITCH_MAPPING.get(vswitch) |
| 363 |
access_mode = module.params.get("access_mode") |
| 364 |
enable_vm_folder = BOOL_TO_ACI_MAPPING[module.params.get("enable_vm_folder")] |
| 365 |
state = module.params.get("state") |
| 366 |
name_alias = module.params.get("name_alias") |
| 367 |
|
| 368 |
if domain_type != "vmm": |
| 369 |
if vm_provider is not None: |
| 370 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'vm_provider'".format(domain_type)) |
| 371 |
if encap_mode is not None: |
| 372 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'encap_mode'".format(domain_type)) |
| 373 |
if multicast_address is not None: |
| 374 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'multicast_address'".format(domain_type)) |
| 375 |
if vswitch is not None: |
| 376 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'vswitch'".format(domain_type)) |
| 377 |
if access_mode is not None: |
| 378 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'access_mode'".format(domain_type)) |
| 379 |
if enable_vm_folder is not None: |
| 380 |
module.fail_json(msg="Domain type '{0}' cannot have parameter 'enable_vm_folder'".format(domain_type)) |
| 381 |
|
| 382 |
if dscp is not None and domain_type not in ["l2dom", "l3dom"]: |
| 383 |
module.fail_json(msg="DSCP values can only be assigned to 'l2ext and 'l3ext' domains") |
| 384 |
|
| 385 |
# Compile the full domain for URL building |
| 386 |
if domain_type == "fc": |
| 387 |
domain_class = "fcDomP" |
| 388 |
domain_mo = "uni/fc-{0}".format(domain) |
| 389 |
domain_rn = "fc-{0}".format(domain) |
| 390 |
elif domain_type == "l2dom": |
| 391 |
domain_class = "l2extDomP" |
| 392 |
domain_mo = "uni/l2dom-{0}".format(domain) |
| 393 |
domain_rn = "l2dom-{0}".format(domain) |
| 394 |
elif domain_type == "l3dom": |
| 395 |
domain_class = "l3extDomP" |
| 396 |
domain_mo = "uni/l3dom-{0}".format(domain) |
| 397 |
domain_rn = "l3dom-{0}".format(domain) |
| 398 |
elif domain_type == "phys": |
| 399 |
domain_class = "physDomP" |
| 400 |
domain_mo = "uni/phys-{0}".format(domain) |
| 401 |
domain_rn = "phys-{0}".format(domain) |
| 402 |
elif domain_type == "vmm": |
| 403 |
domain_class = "vmmDomP" |
| 404 |
domain_mo = "uni/vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING.get(vm_provider), domain) |
| 405 |
domain_rn = "vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING.get(vm_provider), domain) |
| 406 |
|
| 407 |
# Ensure that querying all objects works when only domain_type is provided |
| 408 |
if domain is None: |
| 409 |
domain_mo = None |
| 410 |
|
| 411 |
aci = ACIModule(module) |
| 412 |
aci.construct_url( |
| 413 |
root_class=dict( |
| 414 |
aci_class=domain_class, |
| 415 |
aci_rn=domain_rn, |
| 416 |
module_object=domain_mo, |
| 417 |
target_filter={"name": domain}, |
| 418 |
), |
| 419 |
) |
| 420 |
|
| 421 |
aci.get_existing() |
| 422 |
|
| 423 |
if state == "present": |
| 424 |
aci.payload( |
| 425 |
aci_class=domain_class, |
| 426 |
class_config=dict( |
| 427 |
encapMode=encap_mode, |
| 428 |
mcastAddr=multicast_address, |
| 429 |
configInfraPg=add_infra_pg, |
| 430 |
enableTag=tag_collection, |
| 431 |
mode=vswitch, |
| 432 |
name=domain, |
| 433 |
targetDscp=dscp, |
| 434 |
nameAlias=name_alias, |
| 435 |
accessMode=access_mode, |
| 436 |
enableVmFolder=enable_vm_folder, |
| 437 |
), |
| 438 |
) |
| 439 |
|
| 440 |
aci.get_diff(aci_class=domain_class) |
| 441 |
|
| 442 |
aci.post_config() |
| 443 |
|
| 444 |
elif state == "absent": |
| 445 |
aci.delete_config() |
| 446 |
|
| 447 |
aci.exit_json() |
| 448 |
|
| 449 |
|
| 450 |
if __name__ == "__main__": |
| 451 |
main() |