| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Shreyas Srish (@shrsr) <[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_service_device_cluster |
| 17 |
version_added: "2.11.0" |
| 18 |
short_description: Manage Service Device Clusters on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Service Device Clusters on Cisco Nexus Dashboard Orchestrator (NDO). |
| 21 |
- This module is supported on ND v3.2 (NDO v4.4) and later. |
| 22 |
author: |
| 23 |
- Shreyas Srish (@shrsr) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a service device template. |
| 29 |
- This parameter or O(template_id) is required. |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- The ID of the service device template. |
| 34 |
- This parameter or O(template) is required. |
| 35 |
type: str |
| 36 |
uuid: |
| 37 |
description: |
| 38 |
- The UUID of the Service Device Cluster. |
| 39 |
type: str |
| 40 |
aliases: [ service_device_cluster_uuid ] |
| 41 |
name: |
| 42 |
description: |
| 43 |
- The name of the Service Device Cluster. |
| 44 |
type: str |
| 45 |
aliases: [ service_device_cluster ] |
| 46 |
description: |
| 47 |
description: |
| 48 |
- The description of the Service Device Cluster. |
| 49 |
type: str |
| 50 |
device_mode: |
| 51 |
description: |
| 52 |
- Specifies the operational mode of the device. |
| 53 |
type: str |
| 54 |
choices: [ layer1, layer2, layer3 ] |
| 55 |
device_type: |
| 56 |
description: |
| 57 |
- Defines the type of device being configured. |
| 58 |
type: str |
| 59 |
choices: [ firewall, load_balancer, other ] |
| 60 |
interface_properties: |
| 61 |
description: |
| 62 |
- A list containing interface configuration. |
| 63 |
- The old O(interface_properties) will be replaced with the new O(interface_properties) during an update. |
| 64 |
type: list |
| 65 |
elements: dict |
| 66 |
suboptions: |
| 67 |
name: |
| 68 |
description: |
| 69 |
- The name of the interface. |
| 70 |
type: str |
| 71 |
required: true |
| 72 |
bd: |
| 73 |
description: |
| 74 |
- bd configuration details. |
| 75 |
- This parameter or O(interface_properties.bd_uuid) or O(interface_properties.external_epg_uuid) or O(interface_properties.external_epg) is required. |
| 76 |
type: dict |
| 77 |
suboptions: |
| 78 |
name: |
| 79 |
description: |
| 80 |
- The name associated with the bd. |
| 81 |
type: str |
| 82 |
required: true |
| 83 |
template: |
| 84 |
description: |
| 85 |
- Template associated with the bd. |
| 86 |
- This parameter or O(interface_properties.bd.template_id) is required. |
| 87 |
type: str |
| 88 |
template_id: |
| 89 |
description: |
| 90 |
- ID of the template associated with the bd. |
| 91 |
- This parameter or O(interface_properties.bd.template) is required. |
| 92 |
type: str |
| 93 |
schema: |
| 94 |
description: |
| 95 |
- Schema associated with the bd. |
| 96 |
- This parameter or O(interface_properties.bd.schema_id) is required. |
| 97 |
type: str |
| 98 |
schema_id: |
| 99 |
description: |
| 100 |
- ID of the schema associated with the bd. |
| 101 |
- This parameter or O(interface_properties.bd.schema) is required. |
| 102 |
type: str |
| 103 |
bd_uuid: |
| 104 |
description: |
| 105 |
- UUID of the bd. |
| 106 |
- This parameter or O(interface_properties.bd) or O(interface_properties.external_epg_uuid) or O(interface_properties.external_epg) is required. |
| 107 |
type: str |
| 108 |
external_epg: |
| 109 |
description: |
| 110 |
- external_epg configuration details. |
| 111 |
- This parameter or O(interface_properties.bd) or O(interface_properties.external_epg_uuid) or O(interface_properties.bd_uuid) is required. |
| 112 |
type: dict |
| 113 |
suboptions: |
| 114 |
name: |
| 115 |
description: |
| 116 |
- The name associated with the external_epg. |
| 117 |
type: str |
| 118 |
required: true |
| 119 |
template: |
| 120 |
description: |
| 121 |
- Template associated with the external_epg. |
| 122 |
- This parameter or O(interface_properties.external_epg.template_id) is required. |
| 123 |
type: str |
| 124 |
template_id: |
| 125 |
description: |
| 126 |
- ID of the template associated with the external_epg. |
| 127 |
- This parameter or O(interface_properties.external_epg.template) is required. |
| 128 |
type: str |
| 129 |
schema: |
| 130 |
description: |
| 131 |
- Schema associated with the external_epg. |
| 132 |
- This parameter or O(interface_properties.external_epg.schema_id) is required. |
| 133 |
type: str |
| 134 |
schema_id: |
| 135 |
description: |
| 136 |
- ID of the schema associated with the external_epg. |
| 137 |
- This parameter or O(interface_properties.external_epg.schema) is required. |
| 138 |
type: str |
| 139 |
external_epg_uuid: |
| 140 |
description: |
| 141 |
- UUID of the external_epg. |
| 142 |
- This parameter or O(interface_properties.bd) or O(interface_properties.external_epg) or O(interface_properties.bd_uuid) is required. |
| 143 |
type: str |
| 144 |
ipsla_monitoring_policy: |
| 145 |
description: |
| 146 |
- IPSLA monitoring policy configuration. |
| 147 |
type: dict |
| 148 |
suboptions: |
| 149 |
name: |
| 150 |
description: |
| 151 |
- Name of the IPSLA monitoring policy. |
| 152 |
type: str |
| 153 |
template: |
| 154 |
description: |
| 155 |
- Template for the IPSLA monitoring policy. |
| 156 |
- This parameter or O(interface_properties.ipsla_monitoring_policy.template_id) is required. |
| 157 |
type: str |
| 158 |
template_id: |
| 159 |
description: |
| 160 |
- ID of the template for the IPSLA monitoring policy. |
| 161 |
- This parameter or O(interface_properties.ipsla_monitoring_policy.template) is required. |
| 162 |
type: str |
| 163 |
ipsla_monitoring_policy_uuid: |
| 164 |
description: |
| 165 |
- UUID of the IP SLA monitoring policy. |
| 166 |
type: str |
| 167 |
qos_policy: |
| 168 |
description: |
| 169 |
- Quality of Service (QoS) policy configuration. |
| 170 |
type: dict |
| 171 |
suboptions: |
| 172 |
name: |
| 173 |
description: |
| 174 |
- Name of the QoS policy. |
| 175 |
type: str |
| 176 |
template: |
| 177 |
description: |
| 178 |
- Template for the QoS policy. |
| 179 |
- This parameter or O(interface_properties.qos_policy.template_id) is required. |
| 180 |
type: str |
| 181 |
template_id: |
| 182 |
description: |
| 183 |
- ID of the template for the QoS policy. |
| 184 |
- This parameter or O(interface_properties.qos_policy) is required. |
| 185 |
type: str |
| 186 |
qos_policy_uuid: |
| 187 |
description: |
| 188 |
- UUID of the QoS policy. |
| 189 |
type: str |
| 190 |
preferred_group: |
| 191 |
description: |
| 192 |
- Whether the interface belongs to a preferred group. |
| 193 |
- If this parameter is unspecified, it defaults to False. |
| 194 |
type: bool |
| 195 |
rewrite_source_mac: |
| 196 |
description: |
| 197 |
- Whether to rewrite the source MAC address. |
| 198 |
- If this parameter is unspecified, it defaults to False. |
| 199 |
type: bool |
| 200 |
anycast: |
| 201 |
description: |
| 202 |
- Indicates if anycast is enabled. |
| 203 |
- If this parameter is unspecified, it defaults to False. |
| 204 |
type: bool |
| 205 |
config_static_mac: |
| 206 |
description: |
| 207 |
- Indicates if static MAC configuration is enabled. |
| 208 |
- If this parameter is unspecified, it defaults to False. |
| 209 |
type: bool |
| 210 |
aliases: [ static_mac_configuration ] |
| 211 |
is_backup_redirect_ip: |
| 212 |
description: |
| 213 |
- Indicates if it is a backup redirect IP. |
| 214 |
- If this parameter is unspecified, it defaults to False. |
| 215 |
type: bool |
| 216 |
load_balance_hashing: |
| 217 |
description: |
| 218 |
- Load balancing hashing method. |
| 219 |
- If this parameter is unspecified, it defaults to C(source_destination_and_protocol). |
| 220 |
type: str |
| 221 |
choices: ["source_destination_and_protocol", "source_ip", "destination_ip"] |
| 222 |
pod_aware_redirection: |
| 223 |
description: |
| 224 |
- Indicates if pod-aware redirection is enabled. |
| 225 |
- If this parameter is unspecified, it defaults to False. |
| 226 |
type: bool |
| 227 |
resilient_hashing: |
| 228 |
description: |
| 229 |
- Indicates if resilient hashing is enabled. |
| 230 |
- If this parameter is unspecified, it defaults to False. |
| 231 |
type: bool |
| 232 |
tag_based_sorting: |
| 233 |
description: |
| 234 |
- Indicates if tag-based sorting is enabled. |
| 235 |
- If this parameter is unspecified, it defaults to False. |
| 236 |
type: bool |
| 237 |
min_threshold: |
| 238 |
description: |
| 239 |
- Minimum threshold value for redirect. |
| 240 |
- If this parameter is unspecified, it defaults to 0. |
| 241 |
- This value must be between 0 and 100. |
| 242 |
- This value cannot be greater than or equal to maximum threshold. |
| 243 |
type: int |
| 244 |
max_threshold: |
| 245 |
description: |
| 246 |
- Maximum threshold value for redirect. |
| 247 |
- If this parameter is unspecified, it defaults to 0. |
| 248 |
- This value must be between 0 and 100. |
| 249 |
type: int |
| 250 |
threshold_down_action: |
| 251 |
description: |
| 252 |
- Action to take when the threshold is down. |
| 253 |
type: str |
| 254 |
choices: [ permit, deny, bypass ] |
| 255 |
state: |
| 256 |
description: |
| 257 |
- Use C(absent) for removing. |
| 258 |
- Use C(query) for listing an object or multiple objects. |
| 259 |
- Use C(present) for creating or updating. |
| 260 |
type: str |
| 261 |
choices: [ absent, query, present ] |
| 262 |
default: query |
| 263 |
notes: |
| 264 |
- The O(template) must exist before using this module in your playbook. |
| 265 |
Use M(cisco.mso.ndo_template) to create the service device template. |
| 266 |
seealso: |
| 267 |
- module: cisco.mso.ndo_template |
| 268 |
extends_documentation_fragment: cisco.mso.modules |
| 269 |
""" |
| 270 |
|
| 271 |
EXAMPLES = r""" |
| 272 |
- name: Create a service device cluster with one arm |
| 273 |
cisco.mso.ndo_service_device_cluster: |
| 274 |
host: mso_host |
| 275 |
username: admin |
| 276 |
password: SomeSecretPassword |
| 277 |
template: ansible_device_template |
| 278 |
name: device1 |
| 279 |
device_mode: layer3 |
| 280 |
device_type: firewall |
| 281 |
interface_properties: |
| 282 |
- name: interface1 |
| 283 |
external_epg: |
| 284 |
name: ansible_test_epg |
| 285 |
template: ansible_template |
| 286 |
schema: ansible_test |
| 287 |
ipsla_monitoring_policy: |
| 288 |
name: ansible_test_ipsla_monitoring_policy |
| 289 |
template: ansible_tenant_template |
| 290 |
qos_policy: |
| 291 |
name: ansible_custom_qos_policy |
| 292 |
template: ansible_tenant_template |
| 293 |
preferred_group: true |
| 294 |
rewrite_source_mac: false |
| 295 |
config_static_mac: false |
| 296 |
is_backup_redirect_ip: true |
| 297 |
load_balance_hashing: source_ip |
| 298 |
pod_aware_redirection: false |
| 299 |
resilient_hashing: true |
| 300 |
tag_based_sorting: false |
| 301 |
min_threshold: 10 |
| 302 |
max_threshold: 100 |
| 303 |
threshold_down_action: permit |
| 304 |
state: present |
| 305 |
register: add_device1 |
| 306 |
|
| 307 |
- name: Update the service device cluster to advanced |
| 308 |
cisco.mso.ndo_service_device_cluster: |
| 309 |
host: mso_host |
| 310 |
username: admin |
| 311 |
password: SomeSecretPassword |
| 312 |
template: ansible_device_template |
| 313 |
name: device1 |
| 314 |
device_mode: layer3 |
| 315 |
device_type: firewall |
| 316 |
interface_properties: |
| 317 |
- name: interface1 |
| 318 |
external_epg:: |
| 319 |
name: ansible_test_epg |
| 320 |
template: ansible_template |
| 321 |
schema: ansible_test |
| 322 |
ipsla_monitoring_policy: |
| 323 |
name: ansible_test_ipsla_monitoring_policy |
| 324 |
template: ansible_tenant_template |
| 325 |
qos_policy: |
| 326 |
name: ansible_custom_qos_policy |
| 327 |
template: ansible_tenant_template |
| 328 |
preferred_group: true |
| 329 |
rewrite_source_mac: false |
| 330 |
config_static_mac: false |
| 331 |
is_backup_redirect_ip: true |
| 332 |
load_balance_hashing: source_ip |
| 333 |
pod_aware_redirection: false |
| 334 |
resilient_hashing: true |
| 335 |
tag_based_sorting: false |
| 336 |
min_threshold: 10 |
| 337 |
max_threshold: 100 |
| 338 |
threshold_down_action: permit |
| 339 |
- name: interface2 |
| 340 |
bd_uuid: '{{ ansible_test_bd_query.current.uuid }}' |
| 341 |
ipsla_monitoring_policy_uuid: '{{ ipsla_monitoring_policy.current.uuid }}' |
| 342 |
qos_policy: |
| 343 |
name: ansible_custom_qos_policy |
| 344 |
template_id: '{{ ansible_test_policy.current.templateId }}' |
| 345 |
preferred_group: true |
| 346 |
rewrite_source_mac: false |
| 347 |
config_static_mac: false |
| 348 |
is_backup_redirect_ip: true |
| 349 |
load_balance_hashing: source_ip |
| 350 |
pod_aware_redirection: false |
| 351 |
resilient_hashing: true |
| 352 |
tag_based_sorting: false |
| 353 |
min_threshold: 10 |
| 354 |
max_threshold: 100 |
| 355 |
threshold_down_action: permit |
| 356 |
- name: interface3 |
| 357 |
bd: |
| 358 |
name: ansible_test_bd |
| 359 |
template: ansible_template |
| 360 |
schema_id: '{{ ansible_test_bd.current.vrfRef.schemaId }}' |
| 361 |
qos_policy: |
| 362 |
name: ansible_custom_qos_policy |
| 363 |
template_id: '{{ ansible_test_policy.current.templateId }}' |
| 364 |
anycast: true |
| 365 |
rewrite_source_mac: false |
| 366 |
config_static_mac: false |
| 367 |
is_backup_redirect_ip: true |
| 368 |
load_balance_hashing: destination_ip |
| 369 |
pod_aware_redirection: false |
| 370 |
resilient_hashing: true |
| 371 |
tag_based_sorting: false |
| 372 |
min_threshold: 10 |
| 373 |
max_threshold: 100 |
| 374 |
threshold_down_action: deny |
| 375 |
state: present |
| 376 |
|
| 377 |
- name: Query the service device cluster using name |
| 378 |
cisco.mso.ndo_service_device_cluster: |
| 379 |
host: mso_host |
| 380 |
username: admin |
| 381 |
password: SomeSecretPassword |
| 382 |
template: ansible_device_template |
| 383 |
name: device1 |
| 384 |
state: query |
| 385 |
register: query_device |
| 386 |
|
| 387 |
- name: Query the service device cluster using UUID |
| 388 |
cisco.mso.ndo_service_device_cluster: |
| 389 |
host: mso_host |
| 390 |
username: admin |
| 391 |
password: SomeSecretPassword |
| 392 |
template: ansible_device_template |
| 393 |
uuid: '{{ add_device1.current.uuid }}' |
| 394 |
state: query |
| 395 |
register: query_device_uuid |
| 396 |
|
| 397 |
- name: Query all the device clusters |
| 398 |
cisco.mso.ndo_service_device_cluster: |
| 399 |
host: mso_host |
| 400 |
username: admin |
| 401 |
password: SomeSecretPassword |
| 402 |
template: ansible_device_template |
| 403 |
state: query |
| 404 |
register: query_all |
| 405 |
|
| 406 |
- name: Remove the service device cluster |
| 407 |
cisco.mso.ndo_service_device_cluster: |
| 408 |
host: mso_host |
| 409 |
username: admin |
| 410 |
password: SomeSecretPassword |
| 411 |
template: ansible_device_template |
| 412 |
name: device1 |
| 413 |
state: absent |
| 414 |
""" |
| 415 |
|
| 416 |
from ansible.module_utils.basic import AnsibleModule |
| 417 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 418 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 419 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 420 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, snake_to_camel |
| 421 |
from ansible_collections.cisco.mso.plugins.module_utils.schemas import MSOSchemas |
| 422 |
import copy |
| 423 |
|
| 424 |
|
| 425 |
def main(): |
| 426 |
argument_spec = mso_argument_spec() |
| 427 |
argument_spec.update( |
| 428 |
template=dict(type="str"), |
| 429 |
template_id=dict(type="str"), |
| 430 |
name=dict(type="str", aliases=["service_device_cluster"]), |
| 431 |
uuid=dict(type="str", aliases=["service_device_cluster_uuid"]), |
| 432 |
description=dict(type="str"), |
| 433 |
device_mode=dict(type="str", choices=["layer1", "layer2", "layer3"]), |
| 434 |
device_type=dict(type="str", choices=["firewall", "load_balancer", "other"]), |
| 435 |
interface_properties=dict( |
| 436 |
type="list", |
| 437 |
elements="dict", |
| 438 |
options=dict( |
| 439 |
name=dict(type="str", required=True), |
| 440 |
bd=dict( |
| 441 |
type="dict", |
| 442 |
options=dict( |
| 443 |
name=dict(type="str", required=True), |
| 444 |
template=dict(type="str"), |
| 445 |
template_id=dict(type="str"), |
| 446 |
schema=dict(type="str"), |
| 447 |
schema_id=dict(type="str"), |
| 448 |
), |
| 449 |
required_one_of=[ |
| 450 |
["schema", "schema_id"], |
| 451 |
["template", "template_id"], |
| 452 |
], |
| 453 |
mutually_exclusive=[ |
| 454 |
["schema", "schema_id"], |
| 455 |
["template", "template_id"], |
| 456 |
], |
| 457 |
), |
| 458 |
bd_uuid=dict(type="str"), |
| 459 |
external_epg=dict( |
| 460 |
type="dict", |
| 461 |
options=dict( |
| 462 |
name=dict(type="str", required=True), |
| 463 |
template=dict(type="str"), |
| 464 |
template_id=dict(type="str"), |
| 465 |
schema=dict(type="str"), |
| 466 |
schema_id=dict(type="str"), |
| 467 |
), |
| 468 |
required_one_of=[ |
| 469 |
["schema", "schema_id"], |
| 470 |
["template", "template_id"], |
| 471 |
], |
| 472 |
mutually_exclusive=[ |
| 473 |
["schema", "schema_id"], |
| 474 |
["template", "template_id"], |
| 475 |
], |
| 476 |
), |
| 477 |
external_epg_uuid=dict(type="str"), |
| 478 |
ipsla_monitoring_policy=dict( |
| 479 |
type="dict", |
| 480 |
options=dict( |
| 481 |
name=dict(type="str"), |
| 482 |
template=dict(type="str"), |
| 483 |
template_id=dict(type="str"), |
| 484 |
), |
| 485 |
required_by={ |
| 486 |
"template": "name", |
| 487 |
"template_id": "name", |
| 488 |
}, |
| 489 |
mutually_exclusive=[ |
| 490 |
["template", "template_id"], |
| 491 |
], |
| 492 |
), |
| 493 |
ipsla_monitoring_policy_uuid=dict(type="str"), |
| 494 |
qos_policy=dict( |
| 495 |
type="dict", |
| 496 |
options=dict( |
| 497 |
name=dict(type="str"), |
| 498 |
template=dict(type="str"), |
| 499 |
template_id=dict(type="str"), |
| 500 |
), |
| 501 |
required_by={ |
| 502 |
"template": "name", |
| 503 |
"template_id": "name", |
| 504 |
}, |
| 505 |
mutually_exclusive=[ |
| 506 |
["template", "template_id"], |
| 507 |
], |
| 508 |
), |
| 509 |
qos_policy_uuid=dict(type="str"), |
| 510 |
preferred_group=dict(type="bool"), |
| 511 |
rewrite_source_mac=dict(type="bool"), |
| 512 |
anycast=dict(type="bool"), |
| 513 |
config_static_mac=dict(type="bool", aliases=["static_mac_configuration"]), |
| 514 |
is_backup_redirect_ip=dict(type="bool"), |
| 515 |
load_balance_hashing=dict(type="str", choices=["source_destination_and_protocol", "source_ip", "destination_ip"]), |
| 516 |
pod_aware_redirection=dict(type="bool"), |
| 517 |
resilient_hashing=dict(type="bool"), |
| 518 |
tag_based_sorting=dict(type="bool"), |
| 519 |
min_threshold=dict(type="int"), |
| 520 |
max_threshold=dict(type="int"), |
| 521 |
threshold_down_action=dict(type="str", choices=["permit", "deny", "bypass"]), |
| 522 |
), |
| 523 |
required_one_of=[["bd", "bd_uuid", "external_epg", "external_epg_uuid"]], |
| 524 |
mutually_exclusive=[ |
| 525 |
["bd", "bd_uuid", "external_epg", "external_epg_uuid"], |
| 526 |
["ipsla_monitoring_policy", "ipsla_monitoring_policy_uuid"], |
| 527 |
["qos_policy", "qos_policy_uuid"], |
| 528 |
], |
| 529 |
), |
| 530 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 531 |
) |
| 532 |
|
| 533 |
module = AnsibleModule( |
| 534 |
argument_spec=argument_spec, |
| 535 |
supports_check_mode=True, |
| 536 |
required_if=[ |
| 537 |
["state", "present", ["name", "uuid"], True], |
| 538 |
["state", "absent", ["name", "uuid"], True], |
| 539 |
["state", "present", ["device_mode", "device_type", "interface_properties"]], |
| 540 |
], |
| 541 |
required_one_of=[["template", "template_id"]], |
| 542 |
mutually_exclusive=[["template", "template_id"]], |
| 543 |
) |
| 544 |
|
| 545 |
mso = MSOModule(module) |
| 546 |
|
| 547 |
template = module.params.get("template") |
| 548 |
template_id = module.params.get("template_id") |
| 549 |
name = module.params.get("name") |
| 550 |
uuid = module.params.get("uuid") |
| 551 |
description = module.params.get("description") |
| 552 |
device_type = module.params.get("device_type") |
| 553 |
device_mode = module.params.get("device_mode") |
| 554 |
state = module.params.get("state") |
| 555 |
|
| 556 |
ops = [] |
| 557 |
match = None |
| 558 |
device_path = None |
| 559 |
|
| 560 |
reference_dict = { |
| 561 |
"qos": { |
| 562 |
"name": "qosPolicyName", |
| 563 |
"reference": "qosPolicyRef", |
| 564 |
"type": "qos", |
| 565 |
"template": "qosPolicyTemplateName", |
| 566 |
"templateId": "qosPolicyTemplateId", |
| 567 |
}, |
| 568 |
"ipsla": { |
| 569 |
"name": "ipslaMonitoringPolicyName", |
| 570 |
"reference": "ipslaMonitoringRef", |
| 571 |
"type": "ipslaMonitoringPolicy", |
| 572 |
"template": "ipslaMonitoringPolicyTemplateName", |
| 573 |
"templateId": "ipslaMonitoringPolicyTemplateId", |
| 574 |
}, |
| 575 |
"l3out": { |
| 576 |
"name": "externalEpgName", |
| 577 |
"reference": "externalEpgRef", |
| 578 |
"template": "externalEpgTemplateName", |
| 579 |
"templateId": "externalEpgTemplateId", |
| 580 |
"schema": "externalEpgSchemaName", |
| 581 |
"schemaId": "externalEpgSchemaId", |
| 582 |
"type": "externalEpg", |
| 583 |
}, |
| 584 |
"bd": { |
| 585 |
"name": "bdName", |
| 586 |
"reference": "bdRef", |
| 587 |
"template": "bdTemplateName", |
| 588 |
"templateId": "bdTemplateId", |
| 589 |
"schema": "bdSchemaName", |
| 590 |
"schemaId": "bdSchemaId", |
| 591 |
"type": "bd", |
| 592 |
}, |
| 593 |
} |
| 594 |
|
| 595 |
mso_template = MSOTemplate(mso, "service_device", template, template_id) |
| 596 |
mso_template.validate_template("serviceDevice") |
| 597 |
if module.params.get("interface_properties") is not None: |
| 598 |
interface_properties = get_interfaces_payload(mso, mso_template, module.params.get("interface_properties"), reference_dict) |
| 599 |
object_description = "Service Device Cluster" |
| 600 |
path = "/deviceTemplate/template/devices" |
| 601 |
|
| 602 |
existing_devices = mso_template.template.get("deviceTemplate", {}).get("template", {}).get("devices") or [] |
| 603 |
|
| 604 |
if name or uuid: |
| 605 |
match = mso_template.get_object_by_key_value_pairs( |
| 606 |
object_description, |
| 607 |
existing_devices, |
| 608 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 609 |
) |
| 610 |
if match: |
| 611 |
device_path = "{0}/{1}".format(path, match.index) |
| 612 |
mso_template.update_config_with_template_and_references(match.details, reference_dict) |
| 613 |
mso.existing = mso.previous = copy.deepcopy(match.details) |
| 614 |
else: |
| 615 |
mso.existing = mso.previous = [mso_template.update_config_with_template_and_references(device, reference_dict) for device in existing_devices] |
| 616 |
|
| 617 |
if state == "present": |
| 618 |
mso_values = dict( |
| 619 |
name=name, |
| 620 |
description=description, |
| 621 |
deviceLocation="onPremise", |
| 622 |
deviceMode=device_mode, |
| 623 |
deviceType=snake_to_camel(device_type), |
| 624 |
connectivityMode="advanced" if len(interface_properties) >= 3 else ("oneArm" if len(interface_properties) == 1 else "twoArm"), |
| 625 |
interfaces=interface_properties, |
| 626 |
) |
| 627 |
|
| 628 |
if match: |
| 629 |
append_update_ops_data(ops, match.details, device_path, mso_values) |
| 630 |
mso.sanitize(match.details, collate=True) |
| 631 |
else: |
| 632 |
mso.sanitize(mso_values) |
| 633 |
ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent)) |
| 634 |
|
| 635 |
elif state == "absent": |
| 636 |
if match: |
| 637 |
ops.append(dict(op="remove", path=device_path)) |
| 638 |
|
| 639 |
if not module.check_mode and ops: |
| 640 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 641 |
devices = response.get("deviceTemplate", {}).get("template", {}).get("devices") or [] |
| 642 |
match = mso_template.get_object_by_key_value_pairs(object_description, devices, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]) |
| 643 |
if match: |
| 644 |
mso_template.update_config_with_template_and_references(match.details, reference_dict) |
| 645 |
mso.existing = match.details |
| 646 |
else: |
| 647 |
mso.existing = {} |
| 648 |
elif module.check_mode and state != "query": |
| 649 |
mso_template.update_config_with_template_and_references(mso.proposed, reference_dict) |
| 650 |
mso.existing = mso.proposed if state == "present" else {} |
| 651 |
|
| 652 |
mso.exit_json() |
| 653 |
|
| 654 |
|
| 655 |
def get_interfaces_payload(mso, mso_template, interfaces, reference_dict): |
| 656 |
|
| 657 |
schema = MSOSchemas(mso) |
| 658 |
templates = MSOTemplates(mso) |
| 659 |
|
| 660 |
payload = [] |
| 661 |
for interface in interfaces: |
| 662 |
mso.check_template_when_name_is_provided(interface.get("ipsla_monitoring_policy")) |
| 663 |
mso.check_template_when_name_is_provided(interface.get("qos_policy")) |
| 664 |
|
| 665 |
interface_payload = { |
| 666 |
"name": interface.get("name"), |
| 667 |
"redirect": True, |
| 668 |
"isAdvancedIntfConfig": True, |
| 669 |
"ipslaMonitoringRef": templates.get_object_uuid_from_template( |
| 670 |
"tenant", "ipslaMonitoringPolicies", interface.get("ipsla_monitoring_policy_uuid"), interface.get("ipsla_monitoring_policy") |
| 671 |
), |
| 672 |
"advancedIntfConfig": { |
| 673 |
"rewriteSourceMac": interface.get("rewrite_source_mac"), |
| 674 |
"anycast": interface.get("anycast"), |
| 675 |
"configStaticMac": interface.get("config_static_mac"), |
| 676 |
"isBackupRedirectIP": interface.get("is_backup_redirect_ip"), |
| 677 |
"loadBalanceHashing": snake_to_camel(interface.get("load_balance_hashing"), ["ip"]), |
| 678 |
"podAwareRedirection": interface.get("pod_aware_redirection"), |
| 679 |
"preferredGroup": interface.get("preferred_group"), |
| 680 |
"resilientHashing": interface.get("resilient_hashing"), |
| 681 |
"qosPolicyRef": templates.get_object_uuid_from_template( |
| 682 |
"tenant", "qosPolicies", interface.get("qos_policy_uuid"), interface.get("qos_policy") |
| 683 |
), |
| 684 |
"tag": interface.get("tag_based_sorting"), |
| 685 |
"thresholdForRedirect": { |
| 686 |
"maxThreshold": interface.get("max_threshold"), |
| 687 |
"minThreshold": interface.get("min_threshold"), |
| 688 |
"thresholdDownAction": interface.get("threshold_down_action"), |
| 689 |
}, |
| 690 |
}, |
| 691 |
} |
| 692 |
interface_payload["deviceInterfaceType"] = "l3out" |
| 693 |
interface_type = "external_epg" |
| 694 |
if interface.get("bd_uuid") or interface.get("bd"): |
| 695 |
interface_payload["deviceInterfaceType"] = interface_type = "bd" |
| 696 |
interface_uuid = interface.get("bd_uuid") or interface.get("external_epg_uuid") |
| 697 |
if interface_uuid is None: |
| 698 |
existing_schema = schema.get_template_from_schema( |
| 699 |
interface[interface_type].get("schema"), |
| 700 |
interface[interface_type].get("schema_id"), |
| 701 |
interface[interface_type].get("template"), |
| 702 |
interface[interface_type].get("template_id"), |
| 703 |
) |
| 704 |
if interface_type == "bd": |
| 705 |
existing_schema.set_template_bd(interface["bd"].get("name"), fail_module=True) |
| 706 |
interface_uuid = existing_schema.schema_objects.get("template_bd").details.get("uuid") |
| 707 |
else: |
| 708 |
existing_schema.set_template_external_epg(interface["external_epg"].get("name"), fail_module=True) |
| 709 |
interface_uuid = existing_schema.schema_objects.get("template_external_epg").details.get("uuid") |
| 710 |
interface_payload[reference_dict[interface_payload["deviceInterfaceType"]].get("reference")] = interface_uuid |
| 711 |
if interface_payload.get("ipslaMonitoringRef"): |
| 712 |
interface_payload["advancedIntfConfig"]["advancedTrackingOptions"] = True |
| 713 |
if interface_payload.get("advancedIntfConfig", {}).get("thresholdForRedirect", {}).get("thresholdDownAction"): |
| 714 |
interface_payload["advancedIntfConfig"]["thresholdForRedirectDestination"] = True |
| 715 |
payload.append(interface_payload) |
| 716 |
return payload |
| 717 |
|
| 718 |
|
| 719 |
if __name__ == "__main__": |
| 720 |
main() |