| 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_vlan_pool |
| 17 |
short_description: Manage VLAN Pools on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage VLAN Pools 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 |
vlan_pool: |
| 31 |
description: |
| 32 |
- The name of the VLAN Pool. |
| 33 |
type: str |
| 34 |
aliases: [ name ] |
| 35 |
vlan_pool_uuid: |
| 36 |
description: |
| 37 |
- The uuid of the VLAN Pool. |
| 38 |
- This parameter is required when the O(vlan_pool) needs to be updated. |
| 39 |
type: str |
| 40 |
aliases: [ uuid ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the VLAN Pool. |
| 44 |
type: str |
| 45 |
vlan_ranges: |
| 46 |
description: |
| 47 |
- A list of vlan ranges attached to the VLAN Pool. |
| 48 |
- The list of configured vlan ranges must contain at least one entry. |
| 49 |
- When the list of vlan ranges is null the update will not change existing entry configuration. |
| 50 |
type: list |
| 51 |
elements: dict |
| 52 |
suboptions: |
| 53 |
from_vlan: |
| 54 |
description: |
| 55 |
- The start of the VLAN range. |
| 56 |
type: int |
| 57 |
required: true |
| 58 |
aliases: [ from ] |
| 59 |
to_vlan: |
| 60 |
description: |
| 61 |
- The end of the VLAN range. |
| 62 |
type: int |
| 63 |
required: true |
| 64 |
aliases: [ to ] |
| 65 |
state: |
| 66 |
description: |
| 67 |
- Use C(absent) for removing. |
| 68 |
- Use C(query) for listing an object or multiple objects. |
| 69 |
- Use C(present) for creating or updating. |
| 70 |
type: str |
| 71 |
choices: [ absent, query, present ] |
| 72 |
default: query |
| 73 |
extends_documentation_fragment: cisco.mso.modules |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Create a new vlan pool |
| 78 |
cisco.mso.ndo_vlan_pool: |
| 79 |
host: mso_host |
| 80 |
username: admin |
| 81 |
password: SomeSecretPassword |
| 82 |
template: ansible_tenant_template |
| 83 |
vlan_pool: ansible_test_vlan_pool |
| 84 |
vlan_ranges: |
| 85 |
- from_vlan: 100 |
| 86 |
to_vlan: 200 |
| 87 |
- from_vlan: 300 |
| 88 |
to_vlan: 400 |
| 89 |
state: present |
| 90 |
register: create |
| 91 |
|
| 92 |
- name: Query a vlan pool with name |
| 93 |
cisco.mso.ndo_vlan_pool: |
| 94 |
host: mso_host |
| 95 |
username: admin |
| 96 |
password: SomeSecretPassword |
| 97 |
template: ansible_tenant_template |
| 98 |
vlan_pool: ansible_test_vlan_pool |
| 99 |
state: query |
| 100 |
register: query_one |
| 101 |
|
| 102 |
- name: Query a vlan pool with UUID |
| 103 |
cisco.mso.ndo_vlan_pool: |
| 104 |
host: mso_host |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
template: ansible_tenant_template |
| 108 |
vlan_pool_uuid: '{{ create.current.uuid }}' |
| 109 |
state: query |
| 110 |
register: query_with_uuid |
| 111 |
|
| 112 |
- name: Query all vlan pools in the template |
| 113 |
cisco.mso.ndo_vlan_pool: |
| 114 |
host: mso_host |
| 115 |
username: admin |
| 116 |
password: SomeSecretPassword |
| 117 |
template: ansible_tenant_template |
| 118 |
state: query |
| 119 |
register: query_all |
| 120 |
|
| 121 |
- name: Delete a vlan pool |
| 122 |
cisco.mso.ndo_vlan_pool: |
| 123 |
host: mso_host |
| 124 |
username: admin |
| 125 |
password: SomeSecretPassword |
| 126 |
template: ansible_tenant_template |
| 127 |
vlan_pool: ansible_test_vlan_pool |
| 128 |
state: absent |
| 129 |
""" |
| 130 |
|
| 131 |
RETURN = r""" |
| 132 |
""" |
| 133 |
|
| 134 |
|
| 135 |
import copy |
| 136 |
from ansible.module_utils.basic import AnsibleModule |
| 137 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 138 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
argument_spec = mso_argument_spec() |
| 143 |
argument_spec.update( |
| 144 |
template=dict(type="str", required=True), |
| 145 |
vlan_pool=dict(type="str", aliases=["name"]), |
| 146 |
vlan_pool_uuid=dict(type="str", aliases=["uuid"]), |
| 147 |
description=dict(type="str"), |
| 148 |
vlan_ranges=dict( |
| 149 |
type="list", |
| 150 |
elements="dict", |
| 151 |
options=dict( |
| 152 |
from_vlan=dict(type="int", required=True, aliases=["from"]), |
| 153 |
to_vlan=dict(type="int", required=True, aliases=["to"]), |
| 154 |
), |
| 155 |
), |
| 156 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 157 |
) |
| 158 |
|
| 159 |
module = AnsibleModule( |
| 160 |
argument_spec=argument_spec, |
| 161 |
supports_check_mode=True, |
| 162 |
required_if=[ |
| 163 |
["state", "absent", ["vlan_pool"]], |
| 164 |
["state", "present", ["vlan_pool"]], |
| 165 |
], |
| 166 |
) |
| 167 |
|
| 168 |
mso = MSOModule(module) |
| 169 |
|
| 170 |
template = module.params.get("template") |
| 171 |
vlan_pool = module.params.get("vlan_pool") |
| 172 |
vlan_pool_uuid = module.params.get("vlan_pool_uuid") |
| 173 |
vlan_ranges = get_vlan_ranges_payload(module.params.get("vlan_ranges")) if module.params.get("vlan_ranges") else [] |
| 174 |
description = module.params.get("description") |
| 175 |
state = module.params.get("state") |
| 176 |
|
| 177 |
ops = [] |
| 178 |
match = None |
| 179 |
|
| 180 |
mso_template = MSOTemplate(mso, "fabric_policy", template) |
| 181 |
mso_template.validate_template("fabricPolicy") |
| 182 |
|
| 183 |
path = "/fabricPolicyTemplate/template/vlanPools" |
| 184 |
match = get_fabric_policy_vlan_pool(mso_template, vlan_pool_uuid, vlan_pool) |
| 185 |
|
| 186 |
if vlan_pool_uuid or vlan_pool: |
| 187 |
if match: |
| 188 |
match.details["vlan_ranges"] = match.details["encapBlocks"] |
| 189 |
mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object |
| 190 |
elif match: |
| 191 |
mso.existing = [vlan_pool.update({"vlan_ranges": vlan_pool["encapBlocks"]}) for vlan_pool in match] # Query all objects |
| 192 |
|
| 193 |
if state == "present": |
| 194 |
err_message_min_vlan_ranges = "At least one vlan range is required when state is present." |
| 195 |
|
| 196 |
if match: |
| 197 |
if module.params.get("vlan_ranges") is not None and len(vlan_ranges) == 0: |
| 198 |
mso.fail_json(msg=err_message_min_vlan_ranges) |
| 199 |
|
| 200 |
if vlan_pool and match.details.get("name") != vlan_pool: |
| 201 |
ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=vlan_pool)) |
| 202 |
match.details["name"] = vlan_pool |
| 203 |
|
| 204 |
if description is not None and match.details.get("description") != description: |
| 205 |
ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description)) |
| 206 |
match.details["description"] = description |
| 207 |
|
| 208 |
if module.params.get("vlan_ranges") is not None and match.details.get("encapBlocks") != vlan_ranges: |
| 209 |
ops.append(dict(op="replace", path="{0}/{1}/encapBlocks".format(path, match.index), value=vlan_ranges)) |
| 210 |
match.details["vlan_ranges"] = vlan_ranges |
| 211 |
match.details["encapBlocks"] = vlan_ranges |
| 212 |
|
| 213 |
mso.sanitize(match.details) |
| 214 |
|
| 215 |
else: |
| 216 |
if not vlan_ranges: |
| 217 |
mso.fail_json(msg=err_message_min_vlan_ranges) |
| 218 |
|
| 219 |
payload = {"name": vlan_pool, "allocMode": "static", "encapBlocks": vlan_ranges} |
| 220 |
if description: |
| 221 |
payload["description"] = description |
| 222 |
|
| 223 |
ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload))) |
| 224 |
|
| 225 |
payload["vlan_ranges"] = vlan_ranges |
| 226 |
mso.sanitize(payload) |
| 227 |
mso.existing = mso.proposed |
| 228 |
|
| 229 |
elif state == "absent": |
| 230 |
if match: |
| 231 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index))) |
| 232 |
mso.existing = {} |
| 233 |
|
| 234 |
if not module.check_mode and ops: |
| 235 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 236 |
match = get_fabric_policy_vlan_pool(mso_template, vlan_pool_uuid, vlan_pool) |
| 237 |
if match: |
| 238 |
match.details["vlan_ranges"] = match.details["encapBlocks"] |
| 239 |
mso.existing = match.details # When the state is present |
| 240 |
else: |
| 241 |
mso.existing = {} # When the state is absent |
| 242 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 243 |
mso.existing = mso.proposed if state == "present" else {} |
| 244 |
|
| 245 |
mso.exit_json() |
| 246 |
|
| 247 |
|
| 248 |
def get_vlan_ranges_payload(vlan_ranges): |
| 249 |
payload = [] |
| 250 |
for vlan_range in vlan_ranges: |
| 251 |
vlan_range_payload = { |
| 252 |
"range": { |
| 253 |
"from": vlan_range.get("from_vlan"), |
| 254 |
"to": vlan_range.get("to_vlan"), |
| 255 |
"allocMode": "static", |
| 256 |
} |
| 257 |
} |
| 258 |
payload.append(vlan_range_payload) |
| 259 |
return payload |
| 260 |
|
| 261 |
|
| 262 |
def get_fabric_policy_vlan_pool(mso_template, uuid=None, name=None, fail_module=False): |
| 263 |
""" |
| 264 |
Get the Fabric Policy VLAN Pool by UUID or Name. |
| 265 |
:param uuid: UUID of the Fabric Policy VLAN Pool to search for -> Str |
| 266 |
:param name: Name of the Fabric Policy VLAN Pool to search for -> Str |
| 267 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 268 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 269 |
When the UUID | Name is existing in the search list -> Dict |
| 270 |
When the UUID | Name is not existing in the search list -> None |
| 271 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 272 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 273 |
""" |
| 274 |
match = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("vlanPools", []) |
| 275 |
if uuid or name: # Query a specific object |
| 276 |
return mso_template.get_object_by_key_value_pairs("VLAN Pool", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module) |
| 277 |
return match # Query all objects |
| 278 |
|
| 279 |
|
| 280 |
if __name__ == "__main__": |
| 281 |
main() |