| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Sabari Jaganathan (@sajagana) <[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_node_setting |
| 17 |
short_description: Manage Fabric Policy Node Settings on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage Fabric Policy Node Settings on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Sabari Jaganathan (@sajagana) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a Fabric Policy template. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
aliases: [ fabric_template ] |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The name of the Fabric Policy Node Settings. |
| 34 |
type: str |
| 35 |
aliases: [ node_setting ] |
| 36 |
uuid: |
| 37 |
description: |
| 38 |
- The UUID of the Fabric Policy Node Settings. |
| 39 |
- This parameter is required when the O(name) needs to be updated. |
| 40 |
type: str |
| 41 |
aliases: [ node_setting_uuid ] |
| 42 |
description: |
| 43 |
description: |
| 44 |
- The description of the Fabric Policy Node Settings. |
| 45 |
type: str |
| 46 |
synce: |
| 47 |
description: |
| 48 |
- The Synchronous Ethernet (SyncE) Interface Policy of the Fabric Policy Node Settings. |
| 49 |
type: dict |
| 50 |
suboptions: |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Use C(enabled) to configure the SyncE Interface Policy. |
| 54 |
- Both O(synce.admin_state) and O(synce.quality_level) are required when the state is C(enabled). |
| 55 |
- Use C(disabled) to remove the SyncE Interface Policy. |
| 56 |
type: str |
| 57 |
choices: [ enabled, disabled ] |
| 58 |
admin_state: |
| 59 |
description: |
| 60 |
- The administrative state of the SyncE Interface Policy. |
| 61 |
type: str |
| 62 |
choices: [ enabled, disabled ] |
| 63 |
quality_level: |
| 64 |
description: |
| 65 |
- The quality level option of the SyncE Interface Policy. |
| 66 |
type: str |
| 67 |
choices: [ option_1, option_2_generation_1, option_2_generation_2 ] |
| 68 |
ptp: |
| 69 |
description: |
| 70 |
- The Precision Time Protocol (PTP) of the Fabric Policy Node Settings. |
| 71 |
type: dict |
| 72 |
suboptions: |
| 73 |
state: |
| 74 |
description: |
| 75 |
- Use C(enabled) to configure the PTP Settings. |
| 76 |
- Both O(ptp.node_domain) and O(ptp.priority_2) are required when the state is C(enabled). |
| 77 |
- Use C(disabled) to remove the PTP Settings. |
| 78 |
type: str |
| 79 |
choices: [ enabled, disabled ] |
| 80 |
node_domain: |
| 81 |
description: |
| 82 |
- The node domain number of the PTP Settings. |
| 83 |
- The value must be between 24 and 43. |
| 84 |
type: int |
| 85 |
priority_2: |
| 86 |
description: |
| 87 |
- The value that is used when advertising this clock. |
| 88 |
- The value must be between 0 and 255, lower values are prioritized. |
| 89 |
- The PTP priority 1 is set to a fixed value of 128. |
| 90 |
type: int |
| 91 |
state: |
| 92 |
description: |
| 93 |
- Use C(absent) for removing. |
| 94 |
- Use C(query) for listing an object or multiple objects. |
| 95 |
- Use C(present) for creating or updating. |
| 96 |
type: str |
| 97 |
choices: [ absent, query, present ] |
| 98 |
default: query |
| 99 |
notes: |
| 100 |
- The O(template) must exist before using this module in your playbook. |
| 101 |
Use M(cisco.mso.ndo_template) to create the Fabric Policy template. |
| 102 |
seealso: |
| 103 |
- module: cisco.mso.ndo_template |
| 104 |
extends_documentation_fragment: cisco.mso.modules |
| 105 |
""" |
| 106 |
|
| 107 |
EXAMPLES = r""" |
| 108 |
- name: Create a new Fabric Policy Node Setting |
| 109 |
cisco.mso.ndo_node_setting: |
| 110 |
host: mso_host |
| 111 |
username: admin |
| 112 |
password: SomeSecretPassword |
| 113 |
template: fabric_template |
| 114 |
name: ns1 |
| 115 |
state: present |
| 116 |
register: create_ns1 |
| 117 |
|
| 118 |
- name: Add SyncE config to the existing Fabric Policy Node Setting using Name |
| 119 |
cisco.mso.ndo_node_setting: |
| 120 |
host: mso_host |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
template: fabric_template |
| 124 |
name: ns1 |
| 125 |
synce: |
| 126 |
admin_state: enabled |
| 127 |
quality_level: option_2_generation_1 |
| 128 |
state: present |
| 129 |
|
| 130 |
- name: Add PTP config to the existing Fabric Policy Node Setting using UUID |
| 131 |
cisco.mso.ndo_node_setting: |
| 132 |
host: mso_host |
| 133 |
username: admin |
| 134 |
password: SomeSecretPassword |
| 135 |
template: fabric_template |
| 136 |
uuid: "{{ create_ns1.current.uuid }}" |
| 137 |
name: ns1_updated |
| 138 |
ptp: |
| 139 |
node_domain: 25 |
| 140 |
priority_2: 100 |
| 141 |
state: present |
| 142 |
|
| 143 |
- name: Query an existing Fabric Policy Node Setting using UUID |
| 144 |
cisco.mso.ndo_node_setting: |
| 145 |
host: mso_host |
| 146 |
username: admin |
| 147 |
password: SomeSecretPassword |
| 148 |
template: fabric_template |
| 149 |
uuid: "{{ create_ns1.current.uuid }}" |
| 150 |
state: query |
| 151 |
register: query_with_uuid |
| 152 |
|
| 153 |
- name: Query an existing Fabric Policy Node Setting using Name |
| 154 |
cisco.mso.ndo_node_setting: |
| 155 |
host: mso_host |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
template: fabric_template |
| 159 |
name: ns1_updated |
| 160 |
state: query |
| 161 |
register: query_with_name |
| 162 |
|
| 163 |
- name: Query all Fabric Policy Node Setting |
| 164 |
cisco.mso.ndo_node_setting: |
| 165 |
host: mso_host |
| 166 |
username: admin |
| 167 |
password: SomeSecretPassword |
| 168 |
template: fabric_template |
| 169 |
state: query |
| 170 |
register: query_all |
| 171 |
|
| 172 |
- name: Delete an existing Fabric Policy Node Setting using UUID |
| 173 |
cisco.mso.ndo_node_setting: |
| 174 |
host: mso_host |
| 175 |
username: admin |
| 176 |
password: SomeSecretPassword |
| 177 |
template: fabric_template |
| 178 |
uuid: "{{ create_ns1.current.uuid }}" |
| 179 |
state: absent |
| 180 |
|
| 181 |
- name: Delete an existing Fabric Policy Node Setting using Name |
| 182 |
cisco.mso.ndo_node_setting: |
| 183 |
host: mso_host |
| 184 |
username: admin |
| 185 |
password: SomeSecretPassword |
| 186 |
template: fabric_template |
| 187 |
name: ns1_updated |
| 188 |
state: absent |
| 189 |
""" |
| 190 |
|
| 191 |
RETURN = r""" |
| 192 |
""" |
| 193 |
|
| 194 |
|
| 195 |
import copy |
| 196 |
from ansible.module_utils.basic import AnsibleModule |
| 197 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 198 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 199 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import SYNC_E_QUALITY_LEVEL_OPTION |
| 200 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 201 |
|
| 202 |
|
| 203 |
def main(): |
| 204 |
argument_spec = mso_argument_spec() |
| 205 |
argument_spec.update( |
| 206 |
template=dict(type="str", required=True, aliases=["fabric_template"]), |
| 207 |
name=dict(type="str", aliases=["node_setting"]), |
| 208 |
uuid=dict(type="str", aliases=["node_setting_uuid"]), |
| 209 |
description=dict(type="str"), |
| 210 |
synce=dict( |
| 211 |
type="dict", |
| 212 |
options=dict( |
| 213 |
state=dict(type="str", choices=["enabled", "disabled"]), |
| 214 |
admin_state=dict(type="str", choices=["enabled", "disabled"]), |
| 215 |
quality_level=dict(type="str", choices=list(SYNC_E_QUALITY_LEVEL_OPTION)), |
| 216 |
), |
| 217 |
required_if=[["state", "enabled", ["admin_state", "quality_level"]]], |
| 218 |
), |
| 219 |
ptp=dict( |
| 220 |
type="dict", |
| 221 |
options=dict( |
| 222 |
state=dict(type="str", choices=["enabled", "disabled"]), |
| 223 |
node_domain=dict(type="int"), |
| 224 |
priority_2=dict(type="int"), |
| 225 |
), |
| 226 |
required_if=[["state", "enabled", ["node_domain", "priority_2"]]], |
| 227 |
), |
| 228 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 229 |
) |
| 230 |
|
| 231 |
module = AnsibleModule( |
| 232 |
argument_spec=argument_spec, |
| 233 |
supports_check_mode=True, |
| 234 |
required_if=[ |
| 235 |
["state", "absent", ["name", "uuid"], True], |
| 236 |
["state", "present", ["name", "uuid"], True], |
| 237 |
], |
| 238 |
) |
| 239 |
|
| 240 |
mso = MSOModule(module) |
| 241 |
|
| 242 |
template = module.params.get("template") |
| 243 |
name = module.params.get("name") |
| 244 |
uuid = module.params.get("uuid") |
| 245 |
description = module.params.get("description") |
| 246 |
synce = module.params.get("synce") |
| 247 |
ptp = module.params.get("ptp") |
| 248 |
state = module.params.get("state") |
| 249 |
|
| 250 |
mso_template = MSOTemplate(mso, "fabric_policy", template) |
| 251 |
mso_template.validate_template("fabricPolicy") |
| 252 |
|
| 253 |
node_setting = mso_template.get_node_settings_object(uuid, name) |
| 254 |
|
| 255 |
if (uuid or name) and node_setting: |
| 256 |
mso.existing = mso.previous = copy.deepcopy(node_setting.details) # Query a specific object |
| 257 |
elif node_setting: |
| 258 |
mso.existing = node_setting # Query all objects |
| 259 |
|
| 260 |
if state != "query": |
| 261 |
node_setting_path = "/fabricPolicyTemplate/template/nodePolicyGroups/{0}".format(node_setting.index if node_setting else "-") |
| 262 |
|
| 263 |
ops = [] |
| 264 |
if state == "present": |
| 265 |
if mso.existing: |
| 266 |
proposed_payload = copy.deepcopy(mso.existing) |
| 267 |
mso_values = dict() |
| 268 |
mso_values_remove = list() |
| 269 |
|
| 270 |
mso_values["name"] = name |
| 271 |
mso_values["description"] = description |
| 272 |
|
| 273 |
if synce is not None: |
| 274 |
if synce.get("state") == "disabled" and proposed_payload.get("synce"): |
| 275 |
mso_values_remove.append("synce") |
| 276 |
else: |
| 277 |
mso_values["synce"] = dict() |
| 278 |
mso_values[("synce", "adminState")] = synce.get("admin_state") |
| 279 |
mso_values[("synce", "qlOption")] = SYNC_E_QUALITY_LEVEL_OPTION.get(synce.get("quality_level")) |
| 280 |
|
| 281 |
if ptp is not None: |
| 282 |
if ptp.get("state") == "disabled" and proposed_payload.get("ptp"): |
| 283 |
mso_values_remove.append("ptp") |
| 284 |
else: |
| 285 |
mso_values["ptp"] = dict() |
| 286 |
|
| 287 |
# Add the Priority 1 fixed value 128 to the PTP settings during initialization |
| 288 |
mso_values[("ptp", "prio1")] = 128 |
| 289 |
mso_values[("ptp", "domain")] = ptp.get("node_domain") |
| 290 |
mso_values[("ptp", "prio2")] = ptp.get("priority_2") |
| 291 |
|
| 292 |
append_update_ops_data(ops, proposed_payload, node_setting_path, mso_values, mso_values_remove) |
| 293 |
mso.sanitize(proposed_payload) |
| 294 |
else: |
| 295 |
mso_values = dict(name=name) |
| 296 |
|
| 297 |
if description: |
| 298 |
mso_values["description"] = description |
| 299 |
|
| 300 |
if synce is not None: |
| 301 |
mso_values["synce"] = dict(adminState=synce.get("admin_state"), qlOption=SYNC_E_QUALITY_LEVEL_OPTION.get(synce.get("quality_level"))) |
| 302 |
|
| 303 |
if ptp is not None: |
| 304 |
mso_values["ptp"] = dict( |
| 305 |
domain=ptp.get("node_domain"), |
| 306 |
prio2=ptp.get("priority_2"), |
| 307 |
prio1=128, |
| 308 |
) |
| 309 |
|
| 310 |
ops.append(dict(op="add", path=node_setting_path, value=copy.deepcopy(mso_values))) |
| 311 |
|
| 312 |
mso.sanitize(mso_values) |
| 313 |
elif state == "absent": |
| 314 |
if mso.existing: |
| 315 |
ops.append(dict(op="remove", path=node_setting_path)) |
| 316 |
|
| 317 |
if not module.check_mode and ops: |
| 318 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 319 |
node_setting = mso_template.get_node_settings_object(uuid, name) |
| 320 |
if node_setting: |
| 321 |
mso.existing = node_setting.details # When the state is present |
| 322 |
else: |
| 323 |
mso.existing = {} # When the state is absent |
| 324 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 325 |
mso.existing = mso.proposed if state == "present" else {} |
| 326 |
|
| 327 |
mso.exit_json() |
| 328 |
|
| 329 |
|
| 330 |
if __name__ == "__main__": |
| 331 |
main() |