| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Samita Bhattacharjee (@samiib) <[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_mcp_global_policy |
| 17 |
short_description: Manage the MCP Global Policy in a Fabric Policy Template on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage the MisCabling Protocol (MCP) Global Policy in a Fabric Policy Template on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- There can only be a single MCP Global Policy in a Fabric Policy Template. |
| 21 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 22 |
author: |
| 23 |
- Samita Bhattacharjee (@samiib) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a Fabric Policy template. |
| 29 |
type: str |
| 30 |
aliases: [ fabric_template ] |
| 31 |
required: true |
| 32 |
name: |
| 33 |
description: |
| 34 |
- The name of the MCP Global Policy. |
| 35 |
type: str |
| 36 |
aliases: [ mcp_global_policy ] |
| 37 |
uuid: |
| 38 |
description: |
| 39 |
- The UUID of the MCP Global Policy. |
| 40 |
type: str |
| 41 |
aliases: [ mcp_global_policy_uuid ] |
| 42 |
description: |
| 43 |
description: |
| 44 |
- The description of the MCP Global Policy. |
| 45 |
- Providing an empty string will remove the O(description="") from the MCP Global Policy. |
| 46 |
type: str |
| 47 |
admin_state: |
| 48 |
description: |
| 49 |
- The administrative state of the MCP Global Policy. |
| 50 |
- Defaults to C(enabled) when unset during creation. |
| 51 |
type: str |
| 52 |
choices: [ enabled, disabled ] |
| 53 |
key: |
| 54 |
description: |
| 55 |
- The key of the MCP Global Policy. |
| 56 |
type: str |
| 57 |
per_vlan: |
| 58 |
description: |
| 59 |
- When enabled MCP will send packets on a per End Point Group (EPG) basis. |
| 60 |
- If disabled, the packets will only be sent on untagged EPGs which allows detecting loops in the native VLAN only. |
| 61 |
- Defaults to C(disabled) when unset during creation. |
| 62 |
type: str |
| 63 |
choices: [ enabled, disabled ] |
| 64 |
aliases: [ per_epg, mcp_pdu_per_vlan ] |
| 65 |
loop_detection_factor: |
| 66 |
description: |
| 67 |
- The amount of MCP packets that will be received before port disable loop protection action takes place. |
| 68 |
- Defaults to 3 when unset during creation. |
| 69 |
- The value must be between 1 and 255. |
| 70 |
type: int |
| 71 |
aliases: [ loop_factor, loop_detection_mult_factor ] |
| 72 |
port_disable: |
| 73 |
description: |
| 74 |
- Enable or disable port disabling when MCP packets are received. |
| 75 |
- Defaults to C(enabled) when unset during creation. |
| 76 |
type: str |
| 77 |
choices: [ enabled, disabled ] |
| 78 |
aliases: [ port_disable_protection ] |
| 79 |
initial_delay_time: |
| 80 |
description: |
| 81 |
- The MCP initial delay time in seconds. |
| 82 |
- Defaults to 180 when unset during creation. |
| 83 |
- The value must be between 0 and 1800. |
| 84 |
type: int |
| 85 |
aliases: [ initial_delay ] |
| 86 |
transmission_frequency_sec: |
| 87 |
description: |
| 88 |
- The MCP transmission frequency in seconds. |
| 89 |
- Defaults to 2 when unset during creation. |
| 90 |
- The value must be between 0 and 300. |
| 91 |
type: int |
| 92 |
aliases: [ tx_freq ] |
| 93 |
transmission_frequency_msec: |
| 94 |
description: |
| 95 |
- The MCP transmission frequency in milliseconds. |
| 96 |
- Defaults to 0 when unset during creation. |
| 97 |
- The value must be between 0 and 999. |
| 98 |
type: int |
| 99 |
aliases: [ tx_freq_ms ] |
| 100 |
state: |
| 101 |
description: |
| 102 |
- Use C(absent) for removing. |
| 103 |
- Use C(query) for listing an object or multiple objects. |
| 104 |
- Use C(present) for creating or updating. |
| 105 |
type: str |
| 106 |
choices: [ absent, query, present ] |
| 107 |
default: query |
| 108 |
notes: |
| 109 |
- The O(template) must exist before using this module in your playbook. |
| 110 |
Use M(cisco.mso.ndo_template) to create the Fabric Policy template. |
| 111 |
- Attempts to create any additional MCP Global Policies will only update the existing |
| 112 |
object in the Fabric Policy template. |
| 113 |
seealso: |
| 114 |
- module: cisco.mso.ndo_template |
| 115 |
extends_documentation_fragment: cisco.mso.modules |
| 116 |
""" |
| 117 |
|
| 118 |
EXAMPLES = r""" |
| 119 |
- name: Create the MCP Global Policy object |
| 120 |
cisco.mso.ndo_mcp_global_policy: |
| 121 |
host: mso_host |
| 122 |
username: admin |
| 123 |
password: SomeSecretPassword |
| 124 |
template: fabric_template |
| 125 |
name: example_mcp_global_policy |
| 126 |
state: present |
| 127 |
register: mcp_global_policy_new |
| 128 |
|
| 129 |
- name: Create the MCP Global Policy object with all attributes |
| 130 |
cisco.mso.ndo_mcp_global_policy: |
| 131 |
host: mso_host |
| 132 |
username: admin |
| 133 |
password: SomeSecretPassword |
| 134 |
template: fabric_template |
| 135 |
name: example_mcp_global_policy |
| 136 |
description: A Global MCP Policy |
| 137 |
key: cisco |
| 138 |
admin_state: enabled |
| 139 |
per_vlan: enabled |
| 140 |
loop_detection_factor: 3 |
| 141 |
port_disable: enabled |
| 142 |
initial_delay_time: 180 |
| 143 |
transmission_frequency_sec: 2 |
| 144 |
transmission_frequency_msec: 10 |
| 145 |
state: present |
| 146 |
register: mcp_global_policy_all |
| 147 |
|
| 148 |
- name: Update the MCP Global Policy object with UUID |
| 149 |
cisco.mso.ndo_mcp_global_policy: |
| 150 |
host: mso_host |
| 151 |
username: admin |
| 152 |
password: SomeSecretPassword |
| 153 |
template: fabric_template |
| 154 |
name: mcp_global_policy_update |
| 155 |
uuid: "{{ mcp_global_policy_all.current.uuid }}" |
| 156 |
state: present |
| 157 |
|
| 158 |
- name: Query the MCP Global Policy object with name |
| 159 |
cisco.mso.ndo_mcp_global_policy: |
| 160 |
host: mso_host |
| 161 |
username: admin |
| 162 |
password: SomeSecretPassword |
| 163 |
template: fabric_template |
| 164 |
name: example_mcp_global_policy |
| 165 |
state: query |
| 166 |
register: query_name |
| 167 |
|
| 168 |
- name: Query the MCP Global Policy object with UUID |
| 169 |
cisco.mso.ndo_mcp_global_policy: |
| 170 |
host: mso_host |
| 171 |
username: admin |
| 172 |
password: SomeSecretPassword |
| 173 |
template: fabric_template |
| 174 |
uuid: "{{ mcp_global_policy_all.current.uuid }}" |
| 175 |
state: query |
| 176 |
register: query_uuid |
| 177 |
|
| 178 |
- name: Query the MCP Global Policy object in a Fabric Template |
| 179 |
cisco.mso.ndo_mcp_global_policy: |
| 180 |
host: mso_host |
| 181 |
username: admin |
| 182 |
password: SomeSecretPassword |
| 183 |
template: fabric_template |
| 184 |
state: query |
| 185 |
register: query_all |
| 186 |
|
| 187 |
- name: Delete the MCP Global Policy object with name |
| 188 |
cisco.mso.ndo_mcp_global_policy: |
| 189 |
host: mso_host |
| 190 |
username: admin |
| 191 |
password: SomeSecretPassword |
| 192 |
template: fabric_template |
| 193 |
name: example_mcp_global_policy |
| 194 |
state: absent |
| 195 |
|
| 196 |
- name: Delete the MCP Global Policy object with UUID |
| 197 |
cisco.mso.ndo_mcp_global_policy: |
| 198 |
host: mso_host |
| 199 |
username: admin |
| 200 |
password: SomeSecretPassword |
| 201 |
template: fabric_template |
| 202 |
uuid: "{{ mcp_global_policy_all.current.uuid }}" |
| 203 |
state: absent |
| 204 |
""" |
| 205 |
|
| 206 |
RETURN = r""" |
| 207 |
""" |
| 208 |
|
| 209 |
import copy |
| 210 |
from ansible.module_utils.basic import AnsibleModule |
| 211 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 212 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP |
| 213 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 214 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
argument_spec = mso_argument_spec() |
| 219 |
argument_spec.update( |
| 220 |
template=dict(type="str", required=True, aliases=["fabric_template"]), |
| 221 |
name=dict(type="str", aliases=["mcp_global_policy"]), |
| 222 |
uuid=dict(type="str", aliases=["mcp_global_policy_uuid"]), |
| 223 |
description=dict(type="str"), |
| 224 |
admin_state=dict(type="str", choices=["enabled", "disabled"]), |
| 225 |
key=dict(type="str", no_log=False), |
| 226 |
per_vlan=dict( |
| 227 |
type="str", |
| 228 |
aliases=["per_epg", "mcp_pdu_per_vlan"], |
| 229 |
choices=["enabled", "disabled"], |
| 230 |
), |
| 231 |
loop_detection_factor=dict(type="int", aliases=["loop_factor", "loop_detection_mult_factor"]), |
| 232 |
port_disable=dict( |
| 233 |
type="str", |
| 234 |
aliases=["port_disable_protection"], |
| 235 |
choices=["enabled", "disabled"], |
| 236 |
), |
| 237 |
initial_delay_time=dict(type="int", aliases=["initial_delay"]), |
| 238 |
transmission_frequency_sec=dict(type="int", aliases=["tx_freq"]), |
| 239 |
transmission_frequency_msec=dict(type="int", aliases=["tx_freq_ms"]), |
| 240 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 241 |
) |
| 242 |
|
| 243 |
# Enforcing that a user must specify a name or uuid when |
| 244 |
# adding, updating or removing even though there is only one policy per template. |
| 245 |
module = AnsibleModule( |
| 246 |
argument_spec=argument_spec, |
| 247 |
supports_check_mode=True, |
| 248 |
required_if=[ |
| 249 |
["state", "absent", ["name", "uuid"], True], |
| 250 |
["state", "present", ["name", "uuid"], True], |
| 251 |
], |
| 252 |
) |
| 253 |
|
| 254 |
mso = MSOModule(module) |
| 255 |
|
| 256 |
name = module.params.get("name") |
| 257 |
uuid = module.params.get("uuid") |
| 258 |
state = module.params.get("state") |
| 259 |
per_vlan = module.params.get("per_vlan") |
| 260 |
port_disable = module.params.get("port_disable") |
| 261 |
|
| 262 |
mso_values = { |
| 263 |
"name": name, |
| 264 |
"description": module.params.get("description"), |
| 265 |
"adminState": module.params.get("admin_state"), |
| 266 |
"key": module.params.get("key"), |
| 267 |
"enablePduPerVlan": ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(per_vlan), |
| 268 |
"loopDetectMultFactor": module.params.get("loop_detection_factor"), |
| 269 |
"protectPortDisable": ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(port_disable), |
| 270 |
"initialDelayTime": module.params.get("initial_delay_time"), |
| 271 |
"txFreq": module.params.get("transmission_frequency_sec"), |
| 272 |
"txFreqMsec": module.params.get("transmission_frequency_msec"), |
| 273 |
} |
| 274 |
|
| 275 |
ops = [] |
| 276 |
|
| 277 |
mso_template = MSOTemplate(mso, "fabric_policy", module.params.get("template")) |
| 278 |
mso_template.validate_template("fabricPolicy") |
| 279 |
|
| 280 |
object_description = "MCP Global Policy" |
| 281 |
object_base_path = "/fabricPolicyTemplate/template/mcpGlobalPolicy" |
| 282 |
|
| 283 |
existing_mcp_global_policy = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("mcpGlobalPolicy", {}) |
| 284 |
|
| 285 |
if state in ["query", "absent"] and existing_mcp_global_policy == {}: |
| 286 |
mso.exit_json() |
| 287 |
|
| 288 |
elif state == "query" and not (name or uuid): |
| 289 |
mso.existing = [existing_mcp_global_policy] |
| 290 |
|
| 291 |
elif existing_mcp_global_policy and (name or uuid): |
| 292 |
mso.existing = mso.previous = copy.deepcopy(existing_mcp_global_policy) |
| 293 |
if uuid and mso.existing.get("uuid") != uuid: |
| 294 |
mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid)) |
| 295 |
|
| 296 |
if state == "present": |
| 297 |
if mso.existing: |
| 298 |
proposed_payload = copy.deepcopy(mso.existing) |
| 299 |
append_update_ops_data(ops, proposed_payload, object_base_path, mso_values) |
| 300 |
mso.sanitize(proposed_payload, collate=True) |
| 301 |
else: |
| 302 |
if uuid: |
| 303 |
mso.fail_json(msg="{0} cannot be created with a UUID".format(object_description)) |
| 304 |
mso.sanitize(mso_values) |
| 305 |
ops.append(dict(op="add", path=object_base_path, value=mso.sent)) |
| 306 |
|
| 307 |
mso.existing = mso.proposed |
| 308 |
|
| 309 |
elif state == "absent": |
| 310 |
if mso.existing: |
| 311 |
ops.append(dict(op="remove", path=object_base_path)) |
| 312 |
|
| 313 |
if not module.check_mode and ops: |
| 314 |
response_object = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 315 |
mso.existing = response_object.get("fabricPolicyTemplate", {}).get("template", {}).get("mcpGlobalPolicy", {}) |
| 316 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 317 |
mso.existing = mso.proposed if state == "present" else {} |
| 318 |
|
| 319 |
mso.exit_json() |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__": |
| 323 |
main() |