| 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_template |
| 17 |
short_description: Manage Templates on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage Templates 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 |
type: str |
| 28 |
aliases: [ name ] |
| 29 |
template_id: |
| 30 |
description: |
| 31 |
- The id of the template. |
| 32 |
- This parameter is required when the O(template) needs to be updated. |
| 33 |
type: str |
| 34 |
aliases: [ id ] |
| 35 |
template_type: |
| 36 |
description: |
| 37 |
- The type of the template. |
| 38 |
- The O(template_type=application) is only intended for retrieving the Application template using O(template_id) and does not support template creation. |
| 39 |
type: str |
| 40 |
aliases: [ type ] |
| 41 |
choices: |
| 42 |
- application |
| 43 |
- tenant |
| 44 |
- l3out |
| 45 |
- fabric_policy |
| 46 |
- fabric_resource |
| 47 |
- monitoring_tenant |
| 48 |
- monitoring_access |
| 49 |
- service_device |
| 50 |
tenant: |
| 51 |
description: |
| 52 |
- The name of the tenant attached to the template. |
| 53 |
- Required when O(type=tenant), O(type=l3out), O(type=monitoring_tenant) or O(type=service_device). |
| 54 |
type: str |
| 55 |
sites: |
| 56 |
description: |
| 57 |
- The list of sites attached to the template. |
| 58 |
- Required when O(type=l3out), O(type=monitoring_tenant) or O(type=monitoring_access). |
| 59 |
- Set to an empty list O(sites=[]) to remove all sites attached from the template. |
| 60 |
- Set to null O(sites=null) or do not provide O(sites) to avoid making changes to sites attached to the template. |
| 61 |
aliases: [ site ] |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
suboptions: |
| 65 |
name: |
| 66 |
description: |
| 67 |
- The name of the site attached to the template. |
| 68 |
type: str |
| 69 |
state: |
| 70 |
description: |
| 71 |
- Use C(absent) for removing. |
| 72 |
- Use C(query) for listing an object or multiple objects. |
| 73 |
- Use C(present) for creating or updating. |
| 74 |
type: str |
| 75 |
choices: [ absent, query, present ] |
| 76 |
default: query |
| 77 |
extends_documentation_fragment: cisco.mso.modules |
| 78 |
""" |
| 79 |
|
| 80 |
EXAMPLES = r""" |
| 81 |
- name: Create a new tenant policy template |
| 82 |
cisco.mso.ndo_template: |
| 83 |
host: mso_host |
| 84 |
username: admin |
| 85 |
password: SomeSecretPassword |
| 86 |
template: ansible_tenant_policy |
| 87 |
template_type: tenant |
| 88 |
tenant: ansible_test_tenant |
| 89 |
sites: |
| 90 |
- name: ansible_test_site |
| 91 |
state: present |
| 92 |
|
| 93 |
- name: Create a new l3out policy template |
| 94 |
cisco.mso.ndo_template: |
| 95 |
host: mso_host |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
template: ansible_l3out_policy_template |
| 99 |
template_type: l3out |
| 100 |
tenant: ansible_test_tenant |
| 101 |
sites: |
| 102 |
- name: ansible_test_site |
| 103 |
state: present |
| 104 |
|
| 105 |
- name: Query a tenant policy template |
| 106 |
cisco.mso.ndo_template: |
| 107 |
host: mso_host |
| 108 |
username: admin |
| 109 |
password: SomeSecretPassword |
| 110 |
template: ansible_tenant_policy |
| 111 |
template_type: tenant |
| 112 |
state: query |
| 113 |
register: query_new_tenant_policy_template |
| 114 |
|
| 115 |
- name: Query all tenant policy templates |
| 116 |
cisco.mso.ndo_template: |
| 117 |
host: mso_host |
| 118 |
username: admin |
| 119 |
password: SomeSecretPassword |
| 120 |
template_type: tenant |
| 121 |
state: query |
| 122 |
register: query_all |
| 123 |
|
| 124 |
- name: Query all templates |
| 125 |
cisco.mso.ndo_template: |
| 126 |
host: mso_host |
| 127 |
username: admin |
| 128 |
password: SomeSecretPassword |
| 129 |
state: query |
| 130 |
register: query_all |
| 131 |
|
| 132 |
- name: Delete a tenant policy template |
| 133 |
cisco.mso.ndo_template: |
| 134 |
host: mso_host |
| 135 |
username: admin |
| 136 |
password: SomeSecretPassword |
| 137 |
template: ansible_tenant_policy |
| 138 |
template_type: tenant |
| 139 |
state: absent |
| 140 |
""" |
| 141 |
|
| 142 |
RETURN = r""" |
| 143 |
""" |
| 144 |
|
| 145 |
import copy |
| 146 |
from ansible.module_utils.basic import AnsibleModule |
| 147 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 148 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 149 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import TEMPLATE_TYPES |
| 150 |
|
| 151 |
|
| 152 |
def main(): |
| 153 |
argument_spec = mso_argument_spec() |
| 154 |
argument_spec.update( |
| 155 |
template=dict(type="str", aliases=["name"]), |
| 156 |
template_id=dict(type="str", aliases=["id"]), |
| 157 |
template_type=dict(type="str", aliases=["type"], choices=list(TEMPLATE_TYPES)), |
| 158 |
tenant=dict(type="str"), |
| 159 |
sites=dict(type="list", elements="dict", aliases=["site"], options=dict(name=dict(type="str"))), |
| 160 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 161 |
) |
| 162 |
|
| 163 |
module = AnsibleModule( |
| 164 |
argument_spec=argument_spec, |
| 165 |
supports_check_mode=True, |
| 166 |
required_if=[ |
| 167 |
["state", "absent", ["template", "template_type"]], |
| 168 |
["state", "present", ["template", "template_type"]], |
| 169 |
], |
| 170 |
) |
| 171 |
|
| 172 |
mso = MSOModule(module) |
| 173 |
|
| 174 |
template = module.params.get("template") |
| 175 |
template_id = module.params.get("template_id") |
| 176 |
template_type = module.params.get("template_type") |
| 177 |
tenant_id = mso.lookup_tenant(module.params.get("tenant")) |
| 178 |
site_ids = [mso.lookup_site(site.get("name")) for site in module.params.get("sites", [])] if module.params.get("sites") else [] |
| 179 |
state = module.params.get("state") |
| 180 |
|
| 181 |
if state != "query" and template_type == "application": |
| 182 |
mso.fail_json(msg="The template_type: application is only intended for retrieving the Application template.") |
| 183 |
|
| 184 |
mso_template = MSOTemplate(mso, template_type, template, template_id) |
| 185 |
|
| 186 |
mso.existing = mso.previous = copy.deepcopy(mso_template.template) |
| 187 |
|
| 188 |
if state == "present": |
| 189 |
if tenant_id and not TEMPLATE_TYPES[template_type]["tenant"]: |
| 190 |
mso.fail_json(msg="Tenant cannot be attached to template of type {0}.".format(template_type)) |
| 191 |
|
| 192 |
if not tenant_id and TEMPLATE_TYPES[template_type]["tenant"]: |
| 193 |
mso.fail_json(msg="Tenant must be provided for template of type {0}.".format(template_type)) |
| 194 |
|
| 195 |
if not site_ids and TEMPLATE_TYPES[template_type]["site_amount"] == 1: |
| 196 |
mso.fail_json(msg="Site must be provided for template of type {0}.".format(template_type)) |
| 197 |
|
| 198 |
if len(site_ids) > 1 and TEMPLATE_TYPES[template_type]["site_amount"] == 1: |
| 199 |
mso.fail_json(msg="Only one site can be attached to template of type {0}.".format(template_type)) |
| 200 |
|
| 201 |
if mso_template.template: |
| 202 |
if mso_template.template.get("templateType") != TEMPLATE_TYPES[template_type]["template_type"]: |
| 203 |
mso.fail_json(msg="Template type cannot be changed.") |
| 204 |
|
| 205 |
if TEMPLATE_TYPES[template_type]["tenant"] and changed( |
| 206 |
mso_template.template, template_type, tenant_id, "tenantId" if TEMPLATE_TYPES[template_type] != "monitoring_acces" else "tenant" |
| 207 |
): |
| 208 |
mso.fail_json(msg="Tenant cannot be changed.") |
| 209 |
|
| 210 |
if TEMPLATE_TYPES[template_type]["site_amount"] == 1 and changed(mso_template.template, template_type, site_ids[0], "siteId"): |
| 211 |
mso.fail_json(msg="Site cannot be changed.") |
| 212 |
|
| 213 |
ops = [] |
| 214 |
if mso_template.template.get("displayName") != template: |
| 215 |
ops.append(dict(op="replace", path="/displayName", value=template)) |
| 216 |
# Changing in existing object to set correctly in proposed/current output |
| 217 |
mso_template.template["displayName"] = template |
| 218 |
|
| 219 |
# When more then 1 site can be provided the sites can be added and/or removed with PATCH operations |
| 220 |
# This is done to avoid config loss within sites during a single replace operation on the sites container |
| 221 |
if TEMPLATE_TYPES[template_type]["site_amount"] > 1 and module.params.get("sites") is not None: |
| 222 |
append_site_config_to_ops(ops, TEMPLATE_TYPES[template_type]["template_type_container"], mso_template.template, site_ids) |
| 223 |
|
| 224 |
mso.sanitize(mso_template.template) |
| 225 |
|
| 226 |
if not module.check_mode and ops: |
| 227 |
mso.existing = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 228 |
|
| 229 |
else: |
| 230 |
payload = { |
| 231 |
"displayName": template, |
| 232 |
"templateType": TEMPLATE_TYPES[template_type]["template_type"], |
| 233 |
TEMPLATE_TYPES[template_type]["template_type_container"]: {}, |
| 234 |
} |
| 235 |
|
| 236 |
# Set template specific payload in functions to increase readability due to the amount of different templates with small differences |
| 237 |
if template_type == "tenant": |
| 238 |
set_tenant_template_payload(payload, template_type, tenant_id, site_ids) |
| 239 |
elif template_type == "l3out": |
| 240 |
set_l3out_template_payload(payload, template_type, tenant_id, site_ids[0]) |
| 241 |
elif template_type == "fabric_policy": |
| 242 |
set_fabric_policy_template_payload(payload, template_type, site_ids) |
| 243 |
elif template_type == "fabric_resource": |
| 244 |
set_fabric_resource_template_payload(payload, template_type, site_ids) |
| 245 |
elif template_type == "monitoring_tenant": |
| 246 |
set_monitoring_tenant_template_payload(payload, template_type, tenant_id, site_ids) |
| 247 |
elif template_type == "monitoring_access": |
| 248 |
set_monitoring_acces_template_payload(payload, template_type, site_ids) |
| 249 |
elif template_type == "service_device": |
| 250 |
set_service_device_template_payload(payload, template_type, tenant_id, site_ids) |
| 251 |
|
| 252 |
if not module.check_mode: |
| 253 |
mso.existing = mso.request(mso_template.templates_path, method="POST", data=payload) |
| 254 |
payload["templateId"] = mso.existing.get("templateId") |
| 255 |
|
| 256 |
mso.sanitize(payload) |
| 257 |
|
| 258 |
if module.check_mode: |
| 259 |
mso.existing = mso.proposed |
| 260 |
|
| 261 |
elif state == "absent": |
| 262 |
if mso.previous and not module.check_mode: |
| 263 |
mso.request(mso_template.template_path, method="DELETE") |
| 264 |
mso.existing = {} |
| 265 |
|
| 266 |
mso.exit_json() |
| 267 |
|
| 268 |
|
| 269 |
def set_tenant_template_payload(payload, template_type, tenant_id, site_ids): |
| 270 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"template": {"tenantId": tenant_id}}) |
| 271 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 272 |
|
| 273 |
|
| 274 |
def set_l3out_template_payload(payload, template_type, tenant_id, site_id): |
| 275 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"tenantId": tenant_id}) |
| 276 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"siteId": site_id}) |
| 277 |
|
| 278 |
|
| 279 |
def set_fabric_policy_template_payload(payload, template_type, site_ids): |
| 280 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 281 |
|
| 282 |
|
| 283 |
def set_fabric_resource_template_payload(payload, template_type, site_ids): |
| 284 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 285 |
|
| 286 |
|
| 287 |
def set_monitoring_tenant_template_payload(payload, template_type, tenant_id, site_ids): |
| 288 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"template": {"mtType": "tenant", "tenant": tenant_id}}) |
| 289 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 290 |
|
| 291 |
|
| 292 |
def set_monitoring_acces_template_payload(payload, template_type, site_ids): |
| 293 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"template": {"mtType": "access"}}) |
| 294 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 295 |
|
| 296 |
|
| 297 |
def set_service_device_template_payload(payload, template_type, tenant_id, site_ids): |
| 298 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"template": {"tenantId": tenant_id}}) |
| 299 |
payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) |
| 300 |
|
| 301 |
|
| 302 |
def changed(config, template_type, value, key): |
| 303 |
if TEMPLATE_TYPES[template_type]["template_container"]: |
| 304 |
config_value = config.get(TEMPLATE_TYPES[template_type]["template_type_container"], {}).get("template", {}).get(key) |
| 305 |
else: |
| 306 |
config_value = config.get(TEMPLATE_TYPES[template_type]["template_type_container"], {}).get(key) |
| 307 |
return config_value != value |
| 308 |
|
| 309 |
|
| 310 |
def append_site_config_to_ops(ops, template_type_container, config, site_ids): |
| 311 |
template_container = config.get(template_type_container, {}) |
| 312 |
|
| 313 |
existing_site_ids = [site.get("siteId") for site in template_container.get("sites", [])] |
| 314 |
|
| 315 |
index_list = [index for index, site_id in enumerate(existing_site_ids) if site_id not in site_ids] |
| 316 |
|
| 317 |
if index_list: |
| 318 |
# Sort in reverse to remove from the end of the list first, this way the indexes are not shifting during removal operations |
| 319 |
index_list.sort(reverse=True) |
| 320 |
for index in index_list: |
| 321 |
ops.append(dict(op="remove", path="/{0}/sites/{1}".format(template_type_container, index))) |
| 322 |
# Changing in existing object to set correctly in proposed/current output |
| 323 |
template_container["sites"].pop(index) |
| 324 |
|
| 325 |
for site_id in [site_id for site_id in site_ids if site_id not in existing_site_ids]: |
| 326 |
payload = {"siteId": site_id} |
| 327 |
ops.append(dict(op="add", path="/{0}/sites/-".format(template_type_container), value=payload)) |
| 328 |
# Changing in existing object to set correctly in proposed/current output |
| 329 |
if template_container.get("sites") is None: |
| 330 |
template_container["sites"] = [] |
| 331 |
template_container["sites"].append(payload) |
| 332 |
|
| 333 |
|
| 334 |
if __name__ == "__main__": |
| 335 |
main() |