| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Gaspard Micol (@gmicol) <[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_ntp_policy |
| 17 |
short_description: Manage NTP Policies in Fabric Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage NTP (NTP) Policies in Fabric Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Gaspard Micol (@gmicol) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the Fabric Policy template. |
| 27 |
- This parameter or O(template_id) is required. |
| 28 |
type: str |
| 29 |
aliases: [ fabric_policy_template ] |
| 30 |
template_id: |
| 31 |
description: |
| 32 |
- The ID of the Fabric Policy template. |
| 33 |
- This parameter or O(template) is required. |
| 34 |
type: str |
| 35 |
aliases: [ fabric_policy_template_id ] |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name of the NTP Policy. |
| 39 |
type: str |
| 40 |
aliases: [ ntp_policy ] |
| 41 |
uuid: |
| 42 |
description: |
| 43 |
- The UUID of the NTP Policy. |
| 44 |
- This parameter is required when the NTP Policy O(name) needs to be updated. |
| 45 |
aliases: [ ntp_policy_uuid ] |
| 46 |
type: str |
| 47 |
description: |
| 48 |
description: |
| 49 |
- The description of the NTP Policy. |
| 50 |
- Providing an empty string will remove the O(description="") from the NTP Policy. |
| 51 |
type: str |
| 52 |
ntp_keys: |
| 53 |
description: |
| 54 |
- The list of NTP client authentication keys. |
| 55 |
- Providing a new list of O(ntp_keys) will completely replace an existing one from the NTP Policy. |
| 56 |
- Providing an empty list will remove the O(ntp_keys=[]) from the NTP Policy. |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
suboptions: |
| 60 |
id: |
| 61 |
description: |
| 62 |
- The key's ID. |
| 63 |
- The value must be between 1 and 65535. |
| 64 |
type: int |
| 65 |
required: true |
| 66 |
aliases: [ key_id ] |
| 67 |
key: |
| 68 |
description: |
| 69 |
- The key. |
| 70 |
- Up to 40 alphanumerical characters. |
| 71 |
type: str |
| 72 |
authentication_type: |
| 73 |
description: |
| 74 |
- The type of authentication. |
| 75 |
- The default value is O(ntp_keys.authentication_type=md5). |
| 76 |
type: str |
| 77 |
choices: [ md5, sha1 ] |
| 78 |
trusted: |
| 79 |
description: |
| 80 |
- Set the NTP client authentication key to trusted. |
| 81 |
type: bool |
| 82 |
ntp_providers: |
| 83 |
description: |
| 84 |
- The list of NTP providers. |
| 85 |
- Providing a new list of O(ntp_providers) will completely replace an existing one from the NTP Policy. |
| 86 |
- Providing an empty list will remove the O(ntp_providers=[]) from the NTP Policy. |
| 87 |
type: list |
| 88 |
elements: dict |
| 89 |
suboptions: |
| 90 |
host: |
| 91 |
description: |
| 92 |
- The hostname or IP address of the NTP provider. |
| 93 |
type: str |
| 94 |
required: true |
| 95 |
minimum_poll_interval: |
| 96 |
description: |
| 97 |
- The minimum polling interval value. |
| 98 |
- The default value is O(ntp_providers.minimum_poll_interval=4). |
| 99 |
- The value must be between 4 and 16. |
| 100 |
type: int |
| 101 |
aliases: [ min_poll ] |
| 102 |
maximum_poll_interval: |
| 103 |
description: |
| 104 |
- The maximum polling interval value. |
| 105 |
- The default value is O(ntp_providers.maximum_poll_interval=6). |
| 106 |
- The value must be between 4 and 16. |
| 107 |
type: int |
| 108 |
aliases: [ max_poll ] |
| 109 |
management_epg_type: |
| 110 |
description: |
| 111 |
- The type of the management EPG. |
| 112 |
type: str |
| 113 |
required: true |
| 114 |
choices: [ inb, oob ] |
| 115 |
aliases: [ epg_type ] |
| 116 |
management_epg: |
| 117 |
description: |
| 118 |
- The management EPG. |
| 119 |
type: str |
| 120 |
required: true |
| 121 |
aliases: [ epg ] |
| 122 |
preferred: |
| 123 |
description: |
| 124 |
- Set the NTP provider to preferred. |
| 125 |
type: bool |
| 126 |
authentication_key_id: |
| 127 |
description: |
| 128 |
- The NTP authentication key ID. |
| 129 |
- The value must be between 1 and 65535. |
| 130 |
type: int |
| 131 |
aliases: [ key_id ] |
| 132 |
admin_state: |
| 133 |
description: |
| 134 |
- Enable admin state. |
| 135 |
- The default value is O(admin_state=enabled). |
| 136 |
type: str |
| 137 |
choices: [ enabled, disabled ] |
| 138 |
server_state: |
| 139 |
description: |
| 140 |
- Enable server state. |
| 141 |
- The default value is O(server_state=disabled). |
| 142 |
type: str |
| 143 |
choices: [ enabled, disabled ] |
| 144 |
master_mode: |
| 145 |
description: |
| 146 |
- Enable master mode. |
| 147 |
- The default value is O(master_mode=disabled). |
| 148 |
type: str |
| 149 |
choices: [ enabled, disabled ] |
| 150 |
stratum: |
| 151 |
description: |
| 152 |
- The numerical value of the stratum. |
| 153 |
- The default value is O(stratum=4). |
| 154 |
- The value must be between 1 and 14. |
| 155 |
type: int |
| 156 |
authentication_state: |
| 157 |
description: |
| 158 |
- Enable authentication state. |
| 159 |
- The default value is O(authentication_state=disabled). |
| 160 |
type: str |
| 161 |
choices: [ enabled, disabled ] |
| 162 |
state: |
| 163 |
description: |
| 164 |
- Use C(absent) for removing. |
| 165 |
- Use C(query) for listing an object or multiple objects. |
| 166 |
- Use C(present) for creating or updating. |
| 167 |
type: str |
| 168 |
choices: [ absent, query, present ] |
| 169 |
default: query |
| 170 |
notes: |
| 171 |
- The O(template) must exist before using this module in your playbook. |
| 172 |
Use M(cisco.mso.ndo_template) to create the Fabric Policy template. |
| 173 |
seealso: |
| 174 |
- module: cisco.mso.ndo_template |
| 175 |
extends_documentation_fragment: cisco.mso.modules |
| 176 |
""" |
| 177 |
|
| 178 |
EXAMPLES = r""" |
| 179 |
- name: Create a new NTP Policy object |
| 180 |
cisco.mso.ndo_ntp_policy: |
| 181 |
host: mso_host |
| 182 |
username: admin |
| 183 |
password: SomeSecretPassword |
| 184 |
template: fabric_policy_template |
| 185 |
name: ntp_policy_1 |
| 186 |
ntp_keys: |
| 187 |
- id: 1 |
| 188 |
key: my_key |
| 189 |
authentication_type: md5 |
| 190 |
trusted: true |
| 191 |
ntp_providers: |
| 192 |
- host: background |
| 193 |
minimum_poll_interval: 4 |
| 194 |
maximum_poll_interval: 16 |
| 195 |
management_epg_type: oob |
| 196 |
management_epg: default |
| 197 |
preferred: true |
| 198 |
authentication_key_id: 1 |
| 199 |
admin_state: enabled |
| 200 |
server_state: enabled |
| 201 |
master_mode: enabled |
| 202 |
stratum: 4 |
| 203 |
authentication_state: enabled |
| 204 |
state: present |
| 205 |
register: ntp_policy_1 |
| 206 |
|
| 207 |
- name: Update a NTP Policy object name with UUID |
| 208 |
cisco.mso.ndo_ntp_policy: |
| 209 |
host: mso_host |
| 210 |
username: admin |
| 211 |
password: SomeSecretPassword |
| 212 |
template: fabric_policy_template |
| 213 |
name: ntp_policy_2 |
| 214 |
uuid: "{{ ntp_policy_1.current.uuid }}" |
| 215 |
state: present |
| 216 |
|
| 217 |
- name: Query a NTP Policy object with name |
| 218 |
cisco.mso.ndo_ntp_policy: |
| 219 |
host: mso_host |
| 220 |
username: admin |
| 221 |
password: SomeSecretPassword |
| 222 |
template: fabric_policy_template |
| 223 |
name: ntp_policy_2 |
| 224 |
state: query |
| 225 |
register: query_name |
| 226 |
|
| 227 |
- name: Query a NTP Policy object with UUID |
| 228 |
cisco.mso.ndo_ntp_policy: |
| 229 |
host: mso_host |
| 230 |
username: admin |
| 231 |
password: SomeSecretPassword |
| 232 |
template: fabric_policy_template |
| 233 |
uuid: "{{ ntp_policy_1.current.uuid }}" |
| 234 |
state: query |
| 235 |
register: query_uuid |
| 236 |
|
| 237 |
- name: Query all NTP Policy objects in a Fabric Policy Template |
| 238 |
cisco.mso.ndo_ntp_policy: |
| 239 |
host: mso_host |
| 240 |
username: admin |
| 241 |
password: SomeSecretPassword |
| 242 |
template: fabric_policy_template |
| 243 |
state: query |
| 244 |
register: query_all |
| 245 |
|
| 246 |
- name: Delete a NTP Policy object with name |
| 247 |
cisco.mso.ndo_ntp_policy: |
| 248 |
host: mso_host |
| 249 |
username: admin |
| 250 |
password: SomeSecretPassword |
| 251 |
template: fabric_policy_template |
| 252 |
name: ntp_policy_2 |
| 253 |
state: absent |
| 254 |
|
| 255 |
- name: Delete a NTP Policy object with UUID |
| 256 |
cisco.mso.ndo_ntp_policy: |
| 257 |
host: mso_host |
| 258 |
username: admin |
| 259 |
password: SomeSecretPassword |
| 260 |
template: fabric_policy_template |
| 261 |
uuid: "{{ ntp_policy_1.current.uuid }}" |
| 262 |
state: absent |
| 263 |
""" |
| 264 |
|
| 265 |
RETURN = r""" |
| 266 |
""" |
| 267 |
|
| 268 |
import copy |
| 269 |
from ansible.module_utils.basic import AnsibleModule |
| 270 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 271 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 272 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 273 |
|
| 274 |
|
| 275 |
def main(): |
| 276 |
argument_spec = mso_argument_spec() |
| 277 |
argument_spec.update( |
| 278 |
template=dict(type="str", aliases=["fabric_policy_template"]), |
| 279 |
template_id=dict(type="str", aliases=["fabric_policy_template_id"]), |
| 280 |
name=dict(type="str", aliases=["ntp_policy"]), |
| 281 |
uuid=dict(type="str", aliases=["ntp_policy_uuid"]), |
| 282 |
description=dict(type="str"), |
| 283 |
ntp_keys=dict( |
| 284 |
type="list", |
| 285 |
elements="dict", |
| 286 |
options=dict( |
| 287 |
id=dict(type="int", required=True, aliases=["key_id"]), |
| 288 |
key=dict(type="str", no_log=True), |
| 289 |
authentication_type=dict(type="str", choices=["md5", "sha1"]), |
| 290 |
trusted=dict(type="bool"), |
| 291 |
), |
| 292 |
no_log=False, |
| 293 |
), |
| 294 |
ntp_providers=dict( |
| 295 |
type="list", |
| 296 |
elements="dict", |
| 297 |
options=dict( |
| 298 |
host=dict(type="str", required=True), |
| 299 |
minimum_poll_interval=dict(type="int", aliases=["min_poll"]), |
| 300 |
maximum_poll_interval=dict(type="int", aliases=["max_poll"]), |
| 301 |
management_epg_type=dict(type="str", choices=["inb", "oob"], required=True, aliases=["epg_type"]), |
| 302 |
management_epg=dict(type="str", required=True, aliases=["epg"]), |
| 303 |
preferred=dict(type="bool"), |
| 304 |
authentication_key_id=dict(type="int", aliases=["key_id"]), |
| 305 |
), |
| 306 |
), |
| 307 |
admin_state=dict(type="str", choices=["enabled", "disabled"]), |
| 308 |
server_state=dict(type="str", choices=["enabled", "disabled"]), |
| 309 |
master_mode=dict(type="str", choices=["enabled", "disabled"]), |
| 310 |
stratum=dict(type="int"), |
| 311 |
authentication_state=dict(type="str", choices=["enabled", "disabled"]), |
| 312 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 313 |
) |
| 314 |
|
| 315 |
module = AnsibleModule( |
| 316 |
argument_spec=argument_spec, |
| 317 |
supports_check_mode=True, |
| 318 |
required_if=[ |
| 319 |
["state", "absent", ["name", "uuid"], True], |
| 320 |
["state", "present", ["name", "uuid"], True], |
| 321 |
], |
| 322 |
required_one_of=[ |
| 323 |
("template", "template_id"), |
| 324 |
], |
| 325 |
mutually_exclusive=[ |
| 326 |
("template", "template_id"), |
| 327 |
], |
| 328 |
) |
| 329 |
|
| 330 |
mso = MSOModule(module) |
| 331 |
|
| 332 |
template = module.params.get("template") |
| 333 |
template_id = module.params.get("template_id") |
| 334 |
name = module.params.get("name") |
| 335 |
uuid = module.params.get("uuid") |
| 336 |
description = module.params.get("description") |
| 337 |
ntp_keys = module.params.get("ntp_keys") |
| 338 |
if ntp_keys: |
| 339 |
ntp_keys = [ |
| 340 |
{ |
| 341 |
"id": item.get("id"), |
| 342 |
"key": item.get("key"), |
| 343 |
"authType": item.get("authentication_type"), |
| 344 |
"trusted": item.get("trusted"), |
| 345 |
} |
| 346 |
for item in ntp_keys |
| 347 |
] |
| 348 |
ntp_providers = module.params.get("ntp_providers") |
| 349 |
if ntp_providers: |
| 350 |
ntp_providers = [ |
| 351 |
{ |
| 352 |
"host": item.get("host"), |
| 353 |
"minPollInterval": item.get("minimum_poll_interval"), |
| 354 |
"maxPollInterval": item.get("maximum_poll_interval"), |
| 355 |
"mgmtEpgType": item.get("management_epg_type"), |
| 356 |
"mgmtEpgName": item.get("management_epg"), |
| 357 |
"preferred": item.get("preferred"), |
| 358 |
"authKeyID": item.get("authentication_key_id"), |
| 359 |
} |
| 360 |
for item in ntp_providers |
| 361 |
] |
| 362 |
admin_state = module.params.get("admin_state") |
| 363 |
server_state = module.params.get("server_state") |
| 364 |
master_mode = module.params.get("master_mode") |
| 365 |
stratum = module.params.get("stratum") |
| 366 |
authentication_state = module.params.get("authentication_state") |
| 367 |
state = module.params.get("state") |
| 368 |
|
| 369 |
match = None |
| 370 |
ops = [] |
| 371 |
|
| 372 |
template_object = MSOTemplate(mso, "fabric_policy", template, template_id) |
| 373 |
template_object.validate_template("fabricPolicy") |
| 374 |
|
| 375 |
ntp_policies = template_object.template.get("fabricPolicyTemplate", {}).get("template", {}).get("ntpPolicies", []) |
| 376 |
object_description = "NTP Policy" |
| 377 |
|
| 378 |
if state in ["query", "absent"] and ntp_policies == []: |
| 379 |
mso.exit_json() |
| 380 |
elif state == "query" and not (name or uuid): |
| 381 |
mso.existing = ntp_policies |
| 382 |
elif ntp_policies and (name or uuid): |
| 383 |
match = template_object.get_object_by_key_value_pairs(object_description, ntp_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]) |
| 384 |
if match: |
| 385 |
mso.existing = mso.previous = copy.deepcopy(match.details) |
| 386 |
|
| 387 |
if state != "query": |
| 388 |
ntp_policy_attrs_path = "/fabricPolicyTemplate/template/ntpPolicies/{0}".format(match.index if match else "-") |
| 389 |
|
| 390 |
if state == "present": |
| 391 |
if uuid and not mso.existing: |
| 392 |
mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid)) |
| 393 |
|
| 394 |
mso_values = dict( |
| 395 |
name=name, |
| 396 |
description=description, |
| 397 |
ntpKeys=ntp_keys, |
| 398 |
ntpProviders=ntp_providers, |
| 399 |
adminState=admin_state, |
| 400 |
serverState=server_state, |
| 401 |
masterMode=master_mode, |
| 402 |
stratum=stratum, |
| 403 |
authState=authentication_state, |
| 404 |
) |
| 405 |
|
| 406 |
if match: |
| 407 |
append_update_ops_data(ops, match.details, ntp_policy_attrs_path, mso_values) |
| 408 |
mso.sanitize(match.details, collate=True) |
| 409 |
else: |
| 410 |
mso.sanitize(mso_values) |
| 411 |
ops.append(dict(op="add", path=ntp_policy_attrs_path, value=mso.sent)) |
| 412 |
|
| 413 |
elif state == "absent": |
| 414 |
if match: |
| 415 |
ops.append(dict(op="remove", path=ntp_policy_attrs_path)) |
| 416 |
|
| 417 |
if not module.check_mode and ops: |
| 418 |
response_object = mso.request(template_object.template_path, method="PATCH", data=ops) |
| 419 |
ntp_policies = response_object.get("fabricPolicyTemplate", {}).get("template", {}).get("ntpPolicies", []) |
| 420 |
match = template_object.get_object_by_key_value_pairs(object_description, ntp_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]) |
| 421 |
if match: |
| 422 |
mso.existing = match.details # When the state is present |
| 423 |
else: |
| 424 |
mso.existing = {} # When the state is absent |
| 425 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 426 |
mso.existing = mso.proposed if state == "present" else {} |
| 427 |
|
| 428 |
mso.exit_json() |
| 429 |
|
| 430 |
|
| 431 |
if __name__ == "__main__": |
| 432 |
main() |