| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]> |
| 5 |
# Copyright: (c) 2025, Samita Bhattacharjee (@samiib) <[email protected]> |
| 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_deploy |
| 17 |
version_added: "2.13.0" |
| 18 |
short_description: Deploy templates to sites on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Deploy templates to sites. |
| 21 |
- Prior to deploy or redeploy a schema validation is executed. |
| 22 |
- When schema validation fails, M(cisco.mso.ndo_template_deploy) fails and deploy or redeploy will not be executed. |
| 23 |
- Only supports NDO v3.7 and higher |
| 24 |
author: |
| 25 |
- Akini Ross (@akinross) |
| 26 |
- Samita Bhattacharjee (@samiib) |
| 27 |
options: |
| 28 |
schema: |
| 29 |
description: |
| 30 |
- The name of the schema. |
| 31 |
type: str |
| 32 |
schema_id: |
| 33 |
description: |
| 34 |
- The ID of the schema. |
| 35 |
type: str |
| 36 |
template: |
| 37 |
description: |
| 38 |
- The name of the template. |
| 39 |
- This parameter or O(template_id) is required. |
| 40 |
type: str |
| 41 |
template_id: |
| 42 |
description: |
| 43 |
- The ID of the template. |
| 44 |
- This parameter or O(template) is required. |
| 45 |
type: str |
| 46 |
template_type: |
| 47 |
description: |
| 48 |
- The type of the template. |
| 49 |
- O(template_type) is ignored when O(template_id) is provided. |
| 50 |
type: str |
| 51 |
aliases: [ type ] |
| 52 |
default: application |
| 53 |
choices: [ tenant, l3out, application, fabric_policy, fabric_resource, monitoring_tenant, monitoring_access, service_device ] |
| 54 |
sites: |
| 55 |
description: |
| 56 |
- The list of site names where the template will be undeployed. |
| 57 |
- This option is required when O(state=undeploy). |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
state: |
| 61 |
description: |
| 62 |
- Use C(deploy) to deploy a template. |
| 63 |
- Use C(redeploy) to redeploy a template. |
| 64 |
- Use C(undeploy) to undeploy a template from specified O(sites). |
| 65 |
- Use C(undeploy_all) to undeploy a template from all sites. |
| 66 |
- Use C(query) to get deployment status. |
| 67 |
type: str |
| 68 |
choices: [ deploy, redeploy, undeploy, undeploy_all, query ] |
| 69 |
default: deploy |
| 70 |
notes: |
| 71 |
- The O(template) must exist before using this module in your playbook. |
| 72 |
Use M(cisco.mso.ndo_template) to create non-schema templates. |
| 73 |
Use M(cisco.mso.mso_schema_template) to create schema/application templates. |
| 74 |
seealso: |
| 75 |
- module: cisco.mso.mso_schema_site |
| 76 |
- module: cisco.mso.mso_schema_template |
| 77 |
- module: cisco.mso.ndo_template |
| 78 |
extends_documentation_fragment: cisco.mso.modules |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Deploy a schema template |
| 83 |
cisco.mso.ndo_template_deploy: |
| 84 |
host: mso_host |
| 85 |
username: admin |
| 86 |
password: SomeSecretPassword |
| 87 |
schema: Schema 1 |
| 88 |
template: Template 1 |
| 89 |
state: deploy |
| 90 |
|
| 91 |
- name: Deploy a fabric policy template |
| 92 |
cisco.mso.ndo_template_deploy: |
| 93 |
host: mso_host |
| 94 |
username: admin |
| 95 |
password: SomeSecretPassword |
| 96 |
template: Fabric Policy 1 |
| 97 |
type: fabric_policy |
| 98 |
state: deploy |
| 99 |
|
| 100 |
- name: Redeploy a schema template |
| 101 |
cisco.mso.ndo_template_deploy: |
| 102 |
host: mso_host |
| 103 |
username: admin |
| 104 |
password: SomeSecretPassword |
| 105 |
schema: Schema 1 |
| 106 |
template: Template 1 |
| 107 |
state: redeploy |
| 108 |
|
| 109 |
- name: Redeploy a fabric policy template using template id |
| 110 |
cisco.mso.ndo_template_deploy: |
| 111 |
host: mso_host |
| 112 |
username: admin |
| 113 |
password: SomeSecretPassword |
| 114 |
template_id: '{{ fabric_policy_1.current.templateId }}' |
| 115 |
state: deploy |
| 116 |
|
| 117 |
- name: Undeploy a schema template |
| 118 |
cisco.mso.ndo_template_deploy: |
| 119 |
host: mso_host |
| 120 |
username: admin |
| 121 |
password: SomeSecretPassword |
| 122 |
schema: Schema 1 |
| 123 |
template: Template 1 |
| 124 |
sites: [Site1, Site2] |
| 125 |
state: undeploy |
| 126 |
|
| 127 |
- name: Undeploy a fabric policy template from all sites |
| 128 |
cisco.mso.ndo_template_deploy: |
| 129 |
host: mso_host |
| 130 |
username: admin |
| 131 |
password: SomeSecretPassword |
| 132 |
template: Fabric Policy 1 |
| 133 |
type: fabric_policy |
| 134 |
state: undeploy_all |
| 135 |
|
| 136 |
- name: Undeploy a fabric policy template from one site |
| 137 |
cisco.mso.ndo_template_deploy: |
| 138 |
host: mso_host |
| 139 |
username: admin |
| 140 |
password: SomeSecretPassword |
| 141 |
template: Fabric Policy 1 |
| 142 |
type: fabric_policy |
| 143 |
sites: [Site1] |
| 144 |
state: undeploy |
| 145 |
|
| 146 |
- name: Query a schema template deploy status |
| 147 |
cisco.mso.ndo_template_deploy: |
| 148 |
host: mso_host |
| 149 |
username: admin |
| 150 |
password: SomeSecretPassword |
| 151 |
schema: Schema 1 |
| 152 |
template: Template 1 |
| 153 |
state: query |
| 154 |
register: query_result |
| 155 |
|
| 156 |
- name: Query a fabric policy template deploy status |
| 157 |
cisco.mso.ndo_template_deploy: |
| 158 |
host: mso_host |
| 159 |
username: admin |
| 160 |
password: SomeSecretPassword |
| 161 |
template: Fabric Policy 1 |
| 162 |
type: fabric_policy |
| 163 |
state: query |
| 164 |
register: query_result |
| 165 |
""" |
| 166 |
|
| 167 |
RETURN = r""" |
| 168 |
""" |
| 169 |
|
| 170 |
from ansible.module_utils.basic import AnsibleModule |
| 171 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import ( |
| 172 |
MSOModule, |
| 173 |
mso_argument_spec, |
| 174 |
) |
| 175 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 176 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import TEMPLATE_TYPES |
| 177 |
|
| 178 |
|
| 179 |
def main(): |
| 180 |
argument_spec = mso_argument_spec() |
| 181 |
argument_spec.update( |
| 182 |
schema=dict(type="str"), |
| 183 |
schema_id=dict(type="str"), |
| 184 |
template=dict(type="str"), |
| 185 |
template_id=dict(type="str"), |
| 186 |
template_type=dict(type="str", choices=list(TEMPLATE_TYPES), default="application", aliases=["type"]), |
| 187 |
sites=dict(type="list", elements="str"), |
| 188 |
state=dict( |
| 189 |
type="str", |
| 190 |
default="deploy", |
| 191 |
choices=["deploy", "redeploy", "undeploy", "undeploy_all", "query"], |
| 192 |
), |
| 193 |
) |
| 194 |
|
| 195 |
module = AnsibleModule( |
| 196 |
argument_spec=argument_spec, |
| 197 |
supports_check_mode=True, |
| 198 |
required_if=[ |
| 199 |
["state", "undeploy", ["sites"]], |
| 200 |
], |
| 201 |
mutually_exclusive=[ |
| 202 |
("template", "template_id"), |
| 203 |
("schema", "schema_id"), |
| 204 |
], |
| 205 |
required_one_of=[["template", "template_id"]], |
| 206 |
) |
| 207 |
|
| 208 |
schema = module.params.get("schema") |
| 209 |
schema_id = module.params.get("schema_id") |
| 210 |
template = module.params.get("template") |
| 211 |
template_id = module.params.get("template_id") |
| 212 |
template_type = module.params.get("template_type") |
| 213 |
sites = module.params.get("sites") |
| 214 |
state = module.params.get("state") |
| 215 |
|
| 216 |
mso = MSOModule(module) |
| 217 |
|
| 218 |
if template_id is not None: |
| 219 |
template_type = None |
| 220 |
elif template_type == "application": |
| 221 |
template = template.replace(" ", "") |
| 222 |
|
| 223 |
mso_template = MSOTemplate(mso, template_type, template, template_id, schema, schema_id) |
| 224 |
|
| 225 |
if template is None: |
| 226 |
template = mso_template.template_name |
| 227 |
if template_id is None: |
| 228 |
template_id = mso_template.template_id |
| 229 |
if template_type is None: |
| 230 |
template_type = mso_template.template_type |
| 231 |
else: |
| 232 |
template_type = TEMPLATE_TYPES[template_type]["template_type"] |
| 233 |
|
| 234 |
mso_template.validate_template(template_type) |
| 235 |
|
| 236 |
is_application = template_type == "application" |
| 237 |
if is_application: |
| 238 |
if schema_id is None and schema is None: |
| 239 |
schema_id = mso_template.schema_id |
| 240 |
elif schema is not None and schema_id is None: |
| 241 |
schema_id = mso.lookup_schema(schema) |
| 242 |
|
| 243 |
path = None |
| 244 |
if state == "query": |
| 245 |
if is_application: |
| 246 |
path = "status/schema/{0}/template/{1}".format(schema_id, template) |
| 247 |
elif mso_template.deploy_task_id: |
| 248 |
path = "task/{0}".format(mso_template.deploy_task_id) |
| 249 |
method = "GET" |
| 250 |
payload = None |
| 251 |
else: |
| 252 |
path = "task" |
| 253 |
method = "POST" |
| 254 |
is_undeploy = state == "undeploy" or state == "undeploy_all" |
| 255 |
if is_application: |
| 256 |
payload = dict(schemaId=schema_id, templateName=template) |
| 257 |
else: |
| 258 |
payload = dict(templateId=template_id) |
| 259 |
if state == "deploy": |
| 260 |
payload.update(isRedeploy=False) |
| 261 |
elif state == "redeploy": |
| 262 |
payload.update(isRedeploy=True) |
| 263 |
elif is_undeploy: |
| 264 |
payload.update(undeploy=get_site_ids(mso, mso_template, sites, state)) |
| 265 |
if is_application and not is_undeploy: |
| 266 |
mso.validate_schema(schema_id) |
| 267 |
if not module.check_mode: |
| 268 |
mso.existing = mso.request(path, method=method, data=payload) if path else {} |
| 269 |
mso.exit_json() |
| 270 |
|
| 271 |
|
| 272 |
def get_site_ids(mso, mso_template, sites, state): |
| 273 |
if state == "undeploy_all": |
| 274 |
return mso_template.deployed_site_ids |
| 275 |
return [site.get("siteId") for site in mso.lookup_sites(sites)] if sites else [] |
| 276 |
|
| 277 |
|
| 278 |
if __name__ == "__main__": |
| 279 |
main() |