| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Sabari Jaganathan (@sajagana) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: ndo_schema_template_contract_service_chain |
| 16 |
version_added: "2.11.0" |
| 17 |
short_description: Manage the Schema Template Contract Service Chaining workflow on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage the Schema Template Contract Service Chaining workflow on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.2.3) and later. |
| 21 |
- This module is the recommended approach for Service Chaining over the previous solution using M(cisco.mso.mso_schema_template_contract_service_graph). |
| 22 |
- This module is incompatible with M(cisco.mso.mso_schema_template_contract_service_graph). |
| 23 |
author: |
| 24 |
- Sabari Jaganathan (@sajagana) |
| 25 |
options: |
| 26 |
schema: |
| 27 |
description: |
| 28 |
- The name of the schema. |
| 29 |
- This parameter is mutually exclusive with O(schema_id). |
| 30 |
- This parameter or O(schema_id) is required when O(template) is set. |
| 31 |
type: str |
| 32 |
schema_id: |
| 33 |
description: |
| 34 |
- The ID of the schema. |
| 35 |
- This parameter is mutually exclusive with O(schema). |
| 36 |
- This parameter or O(schema) is required when O(template) is set. |
| 37 |
type: str |
| 38 |
template: |
| 39 |
description: |
| 40 |
- The name of the template. |
| 41 |
- This parameter is mutually exclusive with O(template_id). |
| 42 |
type: str |
| 43 |
template_id: |
| 44 |
description: |
| 45 |
- The ID of the template. |
| 46 |
- This parameter is mutually exclusive with O(template). |
| 47 |
type: str |
| 48 |
contract: |
| 49 |
description: |
| 50 |
- The name of the contract. |
| 51 |
- This parameter is mutually exclusive with O(contract_uuid). |
| 52 |
type: str |
| 53 |
contract_uuid: |
| 54 |
description: |
| 55 |
- The UUID of the contract. |
| 56 |
- This parameter is mutually exclusive with O(contract). |
| 57 |
type: str |
| 58 |
node_filter: |
| 59 |
description: |
| 60 |
- The Filter After First Device option of the contract service chain. |
| 61 |
- This enables traffic filtering to be dynamically applied after the first device in the chain has processed the traffic. |
| 62 |
- Defaults to O(node_filter=allow_all) when unset during creation. |
| 63 |
type: str |
| 64 |
choices: [ allow_all, filters_from_contract ] |
| 65 |
aliases: [ filter_after_first_device ] |
| 66 |
service_nodes: |
| 67 |
description: |
| 68 |
- The list of service nodes for the contract service chain. |
| 69 |
- This parameter is required for creating the contract service chain. |
| 70 |
- Providing a new list of O(service_nodes) will completely replace an existing one from the contract service chain. |
| 71 |
type: list |
| 72 |
elements: dict |
| 73 |
suboptions: |
| 74 |
device_type: |
| 75 |
description: |
| 76 |
- The type of the service device. |
| 77 |
type: str |
| 78 |
choices: [ firewall, load_balancer, other ] |
| 79 |
required: true |
| 80 |
aliases: [ type ] |
| 81 |
uuid: |
| 82 |
description: |
| 83 |
- The UUID of the service device. |
| 84 |
- This parameter is mutually exclusive with O(service_nodes.device). |
| 85 |
- This parameter or O(service_nodes.device) is required. |
| 86 |
type: str |
| 87 |
aliases: [ device_uuid ] |
| 88 |
provider_interface_name: |
| 89 |
description: |
| 90 |
- The name of the service device interface used as the provider interface in the contract service chain. |
| 91 |
type: str |
| 92 |
required: true |
| 93 |
aliases: [ provider_interface, provider ] |
| 94 |
provider_redirect: |
| 95 |
description: |
| 96 |
- The provider redirect option of the contract service chain. |
| 97 |
- Defaults to O(service_nodes.provider_redirect=false) when unset during creation. |
| 98 |
type: bool |
| 99 |
consumer_interface_name: |
| 100 |
description: |
| 101 |
- The name of the service device interface used as the consumer interface in the contract service chain. |
| 102 |
type: str |
| 103 |
required: true |
| 104 |
aliases: [ consumer_interface, consumer ] |
| 105 |
consumer_redirect: |
| 106 |
description: |
| 107 |
- The consumer redirect option of the contract service chain. |
| 108 |
- Defaults to O(service_nodes.consumer_redirect=false) when unset during creation. |
| 109 |
type: bool |
| 110 |
device: |
| 111 |
description: |
| 112 |
- The service device details for the contract service chain. |
| 113 |
- This parameter is mutually exclusive with O(service_nodes.uuid). |
| 114 |
- This parameter or O(service_nodes.uuid) is required. |
| 115 |
type: dict |
| 116 |
suboptions: |
| 117 |
name: |
| 118 |
description: |
| 119 |
- The name of the service device. |
| 120 |
type: str |
| 121 |
required: true |
| 122 |
template: |
| 123 |
description: |
| 124 |
- The template name of the service device. |
| 125 |
- This parameter is mutually exclusive with O(service_nodes.device.template_id). |
| 126 |
- This parameter or O(service_nodes.device.template_id) is required. |
| 127 |
type: str |
| 128 |
template_id: |
| 129 |
description: |
| 130 |
- The template id of the service device. |
| 131 |
- This parameter is mutually exclusive with O(service_nodes.device.template). |
| 132 |
- This parameter or O(service_nodes.device.template) is required. |
| 133 |
type: str |
| 134 |
state: |
| 135 |
description: |
| 136 |
- Use C(present) or C(absent) for adding or removing. |
| 137 |
- Use C(query) for listing an object or multiple objects. |
| 138 |
type: str |
| 139 |
choices: [ absent, present, query ] |
| 140 |
default: present |
| 141 |
seealso: |
| 142 |
- module: cisco.mso.mso_schema_template_contract_filter |
| 143 |
extends_documentation_fragment: cisco.mso.modules |
| 144 |
""" |
| 145 |
|
| 146 |
EXAMPLES = r""" |
| 147 |
- name: Add schema template contract service chain |
| 148 |
cisco.mso.ndo_schema_template_contract_service_chain: |
| 149 |
host: mso_host |
| 150 |
username: admin |
| 151 |
password: SomeSecretPassword |
| 152 |
schema: schema1 |
| 153 |
template: template1 |
| 154 |
contract: contract1 |
| 155 |
node_filter: allow_all |
| 156 |
service_nodes: |
| 157 |
- device_type: load_balancer |
| 158 |
consumer_interface_name: lb_interface1 |
| 159 |
provider_interface_name: lb_interface2 |
| 160 |
consumer_redirect: true |
| 161 |
provider_redirect: true |
| 162 |
device: |
| 163 |
name: load_balancer_device |
| 164 |
template: service_device_template |
| 165 |
state: present |
| 166 |
|
| 167 |
- name: Update schema template contract service chain with multiple nodes |
| 168 |
cisco.mso.ndo_schema_template_contract_service_chain: |
| 169 |
host: mso_host |
| 170 |
username: admin |
| 171 |
password: SomeSecretPassword |
| 172 |
schema: schema1 |
| 173 |
template: template1 |
| 174 |
contract: contract1 |
| 175 |
node_filter: "filters_from_contract" |
| 176 |
service_nodes: |
| 177 |
- device_type: load_balancer |
| 178 |
consumer_interface_name: lb_interface1 |
| 179 |
provider_interface_name: lb_interface2 |
| 180 |
consumer_redirect: true |
| 181 |
provider_redirect: true |
| 182 |
device: |
| 183 |
name: load_balancer_device |
| 184 |
template: service_device_template |
| 185 |
- device_type: firewall |
| 186 |
consumer_interface_name: fw_interface1 |
| 187 |
provider_interface_name: fw_interface2 |
| 188 |
uuid: "{{ service_device.current.uuid }}" |
| 189 |
state: present |
| 190 |
|
| 191 |
- name: Query schema template contract service chain |
| 192 |
cisco.mso.ndo_schema_template_contract_service_chain: |
| 193 |
host: mso_host |
| 194 |
username: admin |
| 195 |
password: SomeSecretPassword |
| 196 |
schema: schema1 |
| 197 |
template: template1 |
| 198 |
contract: contract1 |
| 199 |
state: query |
| 200 |
register: query_service_chain |
| 201 |
|
| 202 |
- name: Delete schema template contract service chain |
| 203 |
cisco.mso.ndo_schema_template_contract_service_chain: |
| 204 |
host: mso_host |
| 205 |
username: admin |
| 206 |
password: SomeSecretPassword |
| 207 |
schema: schema1 |
| 208 |
template: template1 |
| 209 |
contract: contract1 |
| 210 |
state: absent |
| 211 |
""" |
| 212 |
|
| 213 |
RETURN = r""" |
| 214 |
""" |
| 215 |
|
| 216 |
from ansible.module_utils.basic import AnsibleModule |
| 217 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 218 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 219 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 220 |
from ansible_collections.cisco.mso.plugins.module_utils.template import KVPair |
| 221 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import snake_to_camel |
| 222 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import CONTRACT_SERVICE_CHAIN_NODE_FILTER_MAP |
| 223 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import delete_none_values |
| 224 |
import copy |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
argument_spec = mso_argument_spec() |
| 229 |
argument_spec.update( |
| 230 |
schema=dict(type="str"), |
| 231 |
schema_id=dict(type="str"), |
| 232 |
template=dict(type="str"), |
| 233 |
template_id=dict(type="str"), |
| 234 |
contract=dict(type="str"), |
| 235 |
contract_uuid=dict(type="str"), |
| 236 |
node_filter=dict(type="str", choices=list(CONTRACT_SERVICE_CHAIN_NODE_FILTER_MAP), aliases=["filter_after_first_device"]), |
| 237 |
service_nodes=dict( |
| 238 |
type="list", |
| 239 |
elements="dict", |
| 240 |
options=dict( |
| 241 |
device_type=dict(type="str", choices=["firewall", "load_balancer", "other"], required=True, aliases=["type"]), |
| 242 |
uuid=dict(type="str", aliases=["device_uuid"]), |
| 243 |
device=dict( |
| 244 |
type="dict", |
| 245 |
options=dict( |
| 246 |
name=dict(type="str", required=True), |
| 247 |
template=dict(type="str"), |
| 248 |
template_id=dict(type="str"), |
| 249 |
), |
| 250 |
mutually_exclusive=[["template", "template_id"]], |
| 251 |
required_one_of=[["template", "template_id"]], |
| 252 |
), |
| 253 |
provider_interface_name=dict(type="str", required=True, aliases=["provider_interface", "provider"]), |
| 254 |
provider_redirect=dict(type="bool"), |
| 255 |
consumer_interface_name=dict(type="str", required=True, aliases=["consumer_interface", "consumer"]), |
| 256 |
consumer_redirect=dict(type="bool"), |
| 257 |
), |
| 258 |
mutually_exclusive=[["uuid", "device"]], |
| 259 |
required_one_of=[["uuid", "device"]], |
| 260 |
), |
| 261 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 262 |
) |
| 263 |
|
| 264 |
module = AnsibleModule( |
| 265 |
argument_spec=argument_spec, |
| 266 |
supports_check_mode=True, |
| 267 |
required_if=[ |
| 268 |
["state", "present", ["service_nodes"]], |
| 269 |
], |
| 270 |
mutually_exclusive=[["schema", "schema_id"], ["template", "template_id"], ["contract", "contract_uuid"]], |
| 271 |
required_one_of=[["contract", "contract_uuid"], ["template", "template_id"]], |
| 272 |
) |
| 273 |
|
| 274 |
schema = module.params.get("schema") |
| 275 |
schema_id = module.params.get("schema_id") |
| 276 |
template = module.params.get("template") |
| 277 |
template_id = module.params.get("template_id") |
| 278 |
contract = module.params.get("contract") |
| 279 |
contract_uuid = module.params.get("contract_uuid") |
| 280 |
node_filter = CONTRACT_SERVICE_CHAIN_NODE_FILTER_MAP.get(module.params.get("node_filter")) |
| 281 |
service_nodes = module.params.get("service_nodes") |
| 282 |
state = module.params.get("state") |
| 283 |
|
| 284 |
mso = MSOModule(module) |
| 285 |
|
| 286 |
if not template_id and not (schema or schema_id): |
| 287 |
mso.fail_json(msg="The schema or schema_id is required when the template is set.") |
| 288 |
|
| 289 |
mso_templates = MSOTemplates(mso) |
| 290 |
mso_template = MSOTemplate(mso, "application", template, template_id, schema_name=schema, schema_id=schema_id) |
| 291 |
|
| 292 |
template_id = template_id or mso_template.template.get("templateId") |
| 293 |
template = template or mso_template.template.get("appTemplate", {}).get("template", {}).get("name") |
| 294 |
contract_match = mso_template.get_application_template_contract(contract_uuid, contract, fail_module=True) |
| 295 |
service_chain = contract_match.details.get("serviceChaining") if contract_match and contract_match.details.get("serviceChaining") else {} |
| 296 |
|
| 297 |
reference_collections = { |
| 298 |
"serviceDeviceRef": { |
| 299 |
"name": "deviceName", |
| 300 |
"reference": "deviceRef", |
| 301 |
"type": "serviceDevice", |
| 302 |
"template": "deviceTemplateName", |
| 303 |
"templateId": "deviceTemplateId", |
| 304 |
}, |
| 305 |
} |
| 306 |
|
| 307 |
if service_chain: |
| 308 |
mso.existing = mso.previous = mso_template.update_config_with_template_and_references( |
| 309 |
service_chain, reference_collections, set_template=True, use_cache=True |
| 310 |
) # Query a specific object |
| 311 |
|
| 312 |
service_chain_path = "/templates/{0}/contracts/{1}/serviceChaining".format(template, contract_match.details.get("name")) |
| 313 |
|
| 314 |
ops = [] |
| 315 |
|
| 316 |
if state == "present": |
| 317 |
mso_values = dict(nodeFilter=node_filter) |
| 318 |
service_nodes_config = [] |
| 319 |
if service_nodes: |
| 320 |
for index, service_node in enumerate(service_nodes, start=1): |
| 321 |
if not service_node.get("uuid"): |
| 322 |
service_node_template = mso_templates.get_template( |
| 323 |
"service_device", service_node.get("device").get("template"), service_node.get("device").get("template_id") |
| 324 |
) |
| 325 |
service_devices = service_node_template.template.get("deviceTemplate", {}).get("template", {}).get("devices") or [] |
| 326 |
service_node_match = service_node_template.get_object_by_key_value_pairs( |
| 327 |
"Service Device Cluster", |
| 328 |
service_devices, |
| 329 |
[KVPair("name", service_node.get("device").get("name"))], |
| 330 |
fail_module=True, |
| 331 |
) |
| 332 |
service_node["uuid"] = service_node_match.details.get("uuid") |
| 333 |
|
| 334 |
service_nodes_config.append( |
| 335 |
{ |
| 336 |
"index": index, |
| 337 |
"name": "node-{0}".format(index), |
| 338 |
"deviceRef": service_node.get("uuid"), |
| 339 |
"deviceType": snake_to_camel(service_node.get("device_type")), |
| 340 |
"providerConnector": { |
| 341 |
"interfaceName": service_node.get("provider_interface_name"), |
| 342 |
"isRedirect": service_node.get("provider_redirect"), |
| 343 |
}, |
| 344 |
"consumerConnector": { |
| 345 |
"interfaceName": service_node.get("consumer_interface_name"), |
| 346 |
"isRedirect": service_node.get("consumer_redirect"), |
| 347 |
}, |
| 348 |
} |
| 349 |
) |
| 350 |
|
| 351 |
mso_values["serviceNodes"] = service_nodes_config |
| 352 |
mso.sanitize(delete_none_values(mso_values)) |
| 353 |
ops.append(dict(op="replace" if mso.existing else "add", path=service_chain_path, value=mso.sent)) |
| 354 |
|
| 355 |
elif state == "absent" and mso.existing: |
| 356 |
ops.append(dict(op="remove", path=service_chain_path)) |
| 357 |
|
| 358 |
if not module.check_mode and ops: |
| 359 |
mso.request(mso_template.schema_path, method="PATCH", data=ops) |
| 360 |
mso_template = MSOTemplate(mso, "application", None, template_id) |
| 361 |
contract_match = mso_template.get_application_template_contract(contract_uuid, contract, fail_module=True) |
| 362 |
service_chain = contract_match.details.get("serviceChaining") if contract_match and contract_match.details.get("serviceChaining") else {} |
| 363 |
|
| 364 |
if service_chain: |
| 365 |
mso.existing = mso_template.update_config_with_template_and_references( |
| 366 |
service_chain, reference_collections, set_template=True, use_cache=True |
| 367 |
) # When the state is present |
| 368 |
else: |
| 369 |
mso.existing = {} # When the state is absent |
| 370 |
|
| 371 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 372 |
if state == "present": |
| 373 |
mso.existing = mso_template.update_config_with_template_and_references( |
| 374 |
copy.deepcopy(mso.proposed), reference_collections, set_template=True, use_cache=True |
| 375 |
) |
| 376 |
else: |
| 377 |
mso.existing = {} |
| 378 |
|
| 379 |
mso.exit_json() |
| 380 |
|
| 381 |
|
| 382 |
if __name__ == "__main__": |
| 383 |
main() |