| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross (@akinross) <[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": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: ndo_l3_domain |
| 17 |
short_description: Manage L3 Domains on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage L3 Domains on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Akini Ross (@akinross) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a fabric policy template. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
l3_domain: |
| 31 |
description: |
| 32 |
- The name of the L3 Domain. |
| 33 |
type: str |
| 34 |
aliases: [ name ] |
| 35 |
l3_domain_uuid: |
| 36 |
description: |
| 37 |
- The uuid of the L3 Domain. |
| 38 |
- This parameter is required when the O(l3_domain) needs to be updated. |
| 39 |
type: str |
| 40 |
aliases: [ uuid ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the L3 Domain. |
| 44 |
type: str |
| 45 |
pool: |
| 46 |
description: |
| 47 |
- The name of the VLAN Pool. |
| 48 |
- Providing an empty string will remove the pool from the L3 Domain. |
| 49 |
type: str |
| 50 |
aliases: [ vlan_pool ] |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Use C(absent) for removing. |
| 54 |
- Use C(query) for listing an object or multiple objects. |
| 55 |
- Use C(present) for creating or updating. |
| 56 |
type: str |
| 57 |
choices: [ absent, query, present ] |
| 58 |
default: query |
| 59 |
extends_documentation_fragment: cisco.mso.modules |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = r""" |
| 63 |
- name: Create a new L3 domain |
| 64 |
cisco.mso.ndo_l3_domain: |
| 65 |
host: mso_host |
| 66 |
username: admin |
| 67 |
password: SomeSecretPassword |
| 68 |
template: ansible_tenant_template |
| 69 |
l3_domain: ansible_test_l3_domain |
| 70 |
pool: ansible_test_vlan_pool |
| 71 |
state: present |
| 72 |
register: create |
| 73 |
|
| 74 |
- name: Query a L3 domain with name |
| 75 |
cisco.mso.ndo_l3_domain: |
| 76 |
host: mso_host |
| 77 |
username: admin |
| 78 |
password: SomeSecretPassword |
| 79 |
template: ansible_tenant_template |
| 80 |
l3_domain: ansible_test_l3_domain |
| 81 |
state: query |
| 82 |
register: query_one |
| 83 |
|
| 84 |
- name: Query a L3 domain with UUID |
| 85 |
cisco.mso.ndo_l3_domain: |
| 86 |
host: mso_host |
| 87 |
username: admin |
| 88 |
password: SomeSecretPassword |
| 89 |
template: ansible_tenant_template |
| 90 |
l3_domain_uuid: '{{ create.current.uuid }}' |
| 91 |
state: query |
| 92 |
register: query_with_uuid |
| 93 |
|
| 94 |
- name: Query all L3 domains in the template |
| 95 |
cisco.mso.ndo_l3_domain: |
| 96 |
host: mso_host |
| 97 |
username: admin |
| 98 |
password: SomeSecretPassword |
| 99 |
template: ansible_tenant_template |
| 100 |
state: query |
| 101 |
register: query_all |
| 102 |
|
| 103 |
- name: Delete a vlan pool from a L3 domain |
| 104 |
cisco.mso.ndo_l3_domain: |
| 105 |
host: mso_host |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
template: ansible_tenant_template |
| 109 |
l3_domain: ansible_test_l3_domain |
| 110 |
pool: "" |
| 111 |
state: present |
| 112 |
|
| 113 |
- name: Delete a L3 domain |
| 114 |
cisco.mso.ndo_l3_domain: |
| 115 |
host: mso_host |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
template: ansible_tenant_template |
| 119 |
l3_domain: ansible_test_l3_domain |
| 120 |
state: absent |
| 121 |
""" |
| 122 |
|
| 123 |
RETURN = r""" |
| 124 |
""" |
| 125 |
|
| 126 |
|
| 127 |
import copy |
| 128 |
from ansible.module_utils.basic import AnsibleModule |
| 129 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 130 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 131 |
|
| 132 |
|
| 133 |
def main(): |
| 134 |
argument_spec = mso_argument_spec() |
| 135 |
argument_spec.update( |
| 136 |
template=dict(type="str", required=True), |
| 137 |
l3_domain=dict(type="str", aliases=["name"]), |
| 138 |
l3_domain_uuid=dict(type="str", aliases=["uuid"]), |
| 139 |
description=dict(type="str"), |
| 140 |
pool=dict(type="str", aliases=["vlan_pool"]), |
| 141 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 142 |
) |
| 143 |
|
| 144 |
module = AnsibleModule( |
| 145 |
argument_spec=argument_spec, |
| 146 |
supports_check_mode=True, |
| 147 |
required_if=[ |
| 148 |
["state", "absent", ["l3_domain"]], |
| 149 |
["state", "present", ["l3_domain"]], |
| 150 |
], |
| 151 |
) |
| 152 |
|
| 153 |
mso = MSOModule(module) |
| 154 |
|
| 155 |
template = module.params.get("template") |
| 156 |
l3_domain = module.params.get("l3_domain") |
| 157 |
l3_domain_uuid = module.params.get("l3_domain_uuid") |
| 158 |
pool = module.params.get("pool") |
| 159 |
description = module.params.get("description") |
| 160 |
state = module.params.get("state") |
| 161 |
|
| 162 |
ops = [] |
| 163 |
match = None |
| 164 |
|
| 165 |
mso_template = MSOTemplate(mso, "fabric_policy", template) |
| 166 |
mso_template.validate_template("fabricPolicy") |
| 167 |
|
| 168 |
path = "/fabricPolicyTemplate/template/l3Domains" |
| 169 |
match = get_l3_domain(mso_template, l3_domain_uuid, l3_domain) |
| 170 |
if l3_domain_uuid or l3_domain: |
| 171 |
if match: |
| 172 |
if match.details.get("pool"): |
| 173 |
match.details["poolRef"] = match.details.get("pool") |
| 174 |
match.details["pool"] = mso_template.get_vlan_pool_name(match.details.get("pool")) |
| 175 |
mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object |
| 176 |
elif match: |
| 177 |
for l3_domain in match: |
| 178 |
if l3_domain.get("pool"): |
| 179 |
l3_domain["poolRef"] = l3_domain.get("pool") |
| 180 |
l3_domain["pool"] = mso_template.get_vlan_pool_name(l3_domain.get("pool")) |
| 181 |
mso.existing = match # Query all objects |
| 182 |
|
| 183 |
if state == "present": |
| 184 |
mso.existing = {} |
| 185 |
|
| 186 |
if match: |
| 187 |
if l3_domain and match.details.get("name") != l3_domain: |
| 188 |
ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=l3_domain)) |
| 189 |
match.details["name"] = l3_domain |
| 190 |
|
| 191 |
if description is not None and match.details.get("description") != description: |
| 192 |
ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description)) |
| 193 |
match.details["description"] = description |
| 194 |
|
| 195 |
if pool and match.details.get("pool") != pool: |
| 196 |
ops.append(dict(op="replace", path="{0}/{1}/pool".format(path, match.index), value=mso_template.get_vlan_pool_uuid(pool))) |
| 197 |
match.details["pool"] = pool |
| 198 |
elif pool == "" and match.details.get("pool"): |
| 199 |
ops.append(dict(op="remove", path="{0}/{1}/pool".format(path, match.index))) |
| 200 |
match.details.pop("pool") |
| 201 |
|
| 202 |
mso.sanitize(match.details) |
| 203 |
|
| 204 |
else: |
| 205 |
payload = {"name": l3_domain, "templateId": mso_template.template.get("templateId"), "schemaId": mso_template.template.get("schemaId")} |
| 206 |
if description: |
| 207 |
payload["description"] = description |
| 208 |
|
| 209 |
if pool: |
| 210 |
pool_uuid = mso_template.get_vlan_pool_uuid(pool) |
| 211 |
payload["pool"] = pool_uuid |
| 212 |
|
| 213 |
ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload))) |
| 214 |
|
| 215 |
if pool: |
| 216 |
payload["pool"] = pool |
| 217 |
payload["poolRef"] = pool_uuid |
| 218 |
|
| 219 |
mso.sanitize(payload) |
| 220 |
|
| 221 |
mso.existing = mso.proposed |
| 222 |
|
| 223 |
elif state == "absent": |
| 224 |
if match: |
| 225 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index))) |
| 226 |
mso.existing = {} |
| 227 |
|
| 228 |
if not module.check_mode and ops: |
| 229 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 230 |
match = get_l3_domain(mso_template, l3_domain_uuid, l3_domain) |
| 231 |
if match: |
| 232 |
if match.details.get("pool"): |
| 233 |
match.details["poolRef"] = match.details.get("pool") |
| 234 |
match.details["pool"] = mso_template.get_vlan_pool_name(match.details.get("pool")) |
| 235 |
mso.existing = match.details # When the state is present |
| 236 |
else: |
| 237 |
mso.existing = {} # When the state is absent |
| 238 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 239 |
mso.proposed["poolRef"] = mso_template.get_vlan_pool_uuid(pool) |
| 240 |
mso.existing = mso.proposed if state == "present" else {} |
| 241 |
|
| 242 |
mso.exit_json() |
| 243 |
|
| 244 |
|
| 245 |
def get_l3_domain(mso_template, uuid=None, name=None, fail_module=False): |
| 246 |
match = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("l3Domains", []) |
| 247 |
if uuid or name: # Query a specific object |
| 248 |
return mso_template.get_object_by_key_value_pairs("L3 Domain", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 249 |
return match # Query all objects |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == "__main__": |
| 253 |
main() |