| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Akini Ross (@akinross) <[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_pod_settings |
| 17 |
version_added: "2.11.0" |
| 18 |
short_description: Manage Pod Settings on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Pod Settings on Cisco Nexus Dashboard Orchestrator (NDO). |
| 21 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 22 |
author: |
| 23 |
- Akini Ross (@akinross) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a Fabric Policy template. |
| 29 |
- This parameter or O(template_id) is required. |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- The ID of the template. |
| 34 |
- The template must be a Fabric Policy template. |
| 35 |
- This parameter or O(template) is required. |
| 36 |
type: str |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name of the Pod Settings. |
| 40 |
type: str |
| 41 |
aliases: [ pod_settings ] |
| 42 |
uuid: |
| 43 |
description: |
| 44 |
- The UUID of the Pod Settings. |
| 45 |
- This parameter is required when the O(name) needs to be updated. |
| 46 |
type: str |
| 47 |
aliases: [ pod_settings_uuid ] |
| 48 |
description: |
| 49 |
description: |
| 50 |
- The description of the Pod Settings. |
| 51 |
type: str |
| 52 |
macsec_policy: |
| 53 |
description: |
| 54 |
- The MACsec Policy to be used. |
| 55 |
- This parameter is mutually exclusive with O(macsec_policy_uuid). |
| 56 |
- Providing an empty dictionary O(macsec_policy={}) will remove the MACsec Policy from the Pod Settings. |
| 57 |
type: dict |
| 58 |
suboptions: |
| 59 |
name: |
| 60 |
description: |
| 61 |
- The name of the MACsec Policy to be used. |
| 62 |
type: str |
| 63 |
macsec_policy_uuid: |
| 64 |
description: |
| 65 |
- The UUID of the MACsec Policy to be used. |
| 66 |
- Providing an empty string O(macsec_policy_uuid="") will remove the MACsec Policy from the Pod Settings. |
| 67 |
- This parameter is mutually exclusive with O(macsec_policy). |
| 68 |
type: str |
| 69 |
ntp_policy: |
| 70 |
description: |
| 71 |
- The NTP Policy to be used. |
| 72 |
- This parameter is mutually exclusive with O(ntp_policy_uuid). |
| 73 |
- Providing an empty dictionary O(ntp_policy={}) will remove the NTP Policy from the Pod Settings. |
| 74 |
type: dict |
| 75 |
suboptions: |
| 76 |
name: |
| 77 |
description: |
| 78 |
- The name of the NTP Policy to be used. |
| 79 |
type: str |
| 80 |
ntp_policy_uuid: |
| 81 |
description: |
| 82 |
- The UUID of the NTP Policy to be used. |
| 83 |
- Providing an empty string O(ntp_policy_uuid="") will remove the NTP Policy from the Pod Settings. |
| 84 |
- This parameter is mutually exclusive with O(ntp_policy). |
| 85 |
type: str |
| 86 |
state: |
| 87 |
description: |
| 88 |
- Use C(absent) for removing. |
| 89 |
- Use C(query) for listing an object or multiple objects. |
| 90 |
- Use C(present) for creating or updating. |
| 91 |
type: str |
| 92 |
choices: [ absent, query, present ] |
| 93 |
default: query |
| 94 |
notes: |
| 95 |
- The O(template) must exist before using this module in your playbook. |
| 96 |
Use M(cisco.mso.ndo_template) to create the Fabric Policy template. |
| 97 |
- The O(macsec_policy) or O(macsec_policy_uuid) must exist before using it with this module in your playbook. |
| 98 |
Use M(cisco.mso.ndo_macsec_policy) to create the MACsec Policy. |
| 99 |
- The O(ntp_policy) or O(ntp_policy_uuid) must exist before using it with this module in your playbook. |
| 100 |
Use M(cisco.mso.ndo_ntp_policy) to create the NTP Policy. |
| 101 |
seealso: |
| 102 |
- module: cisco.mso.ndo_template |
| 103 |
- module: cisco.mso.ndo_macsec_policy |
| 104 |
- module: cisco.mso.ndo_ntp_policy |
| 105 |
extends_documentation_fragment: cisco.mso.modules |
| 106 |
""" |
| 107 |
|
| 108 |
EXAMPLES = r""" |
| 109 |
- name: Create Pod Settings |
| 110 |
cisco.mso.ndo_pod_settings: |
| 111 |
host: mso_host |
| 112 |
username: admin |
| 113 |
password: SomeSecretPassword |
| 114 |
template: ansible_test |
| 115 |
name: ansible_pod_settings |
| 116 |
state: present |
| 117 |
register: create_pod_settings |
| 118 |
|
| 119 |
- name: Update Pod Settings |
| 120 |
cisco.mso.ndo_pod_settings: |
| 121 |
host: mso_host |
| 122 |
username: admin |
| 123 |
password: SomeSecretPassword |
| 124 |
template: ansible_test |
| 125 |
name: ansible_pod_settings |
| 126 |
description: Updated Pod Settings |
| 127 |
macsec_policy: |
| 128 |
name: ansible_macsec_policy |
| 129 |
ntp_policy: |
| 130 |
name: ansible_ntp_policy |
| 131 |
state: present |
| 132 |
|
| 133 |
- name: Update the Pod Settings using UUID |
| 134 |
cisco.mso.ndo_pod_settings: |
| 135 |
host: mso_host |
| 136 |
username: admin |
| 137 |
password: SomeSecretPassword |
| 138 |
template: ansible_test |
| 139 |
uuid: "{{ create_pod_settings.current.uuid }}" |
| 140 |
macsec_policy_uuid: "{{ create_macsec_policy.current.uuid }}" |
| 141 |
ntp_policy_uuid: "{{ create_ntp_policy.current.uuid }}" |
| 142 |
state: present |
| 143 |
|
| 144 |
- name: Query the Pod Settings using name |
| 145 |
cisco.mso.ndo_pod_settings: |
| 146 |
host: mso_host |
| 147 |
username: admin |
| 148 |
password: SomeSecretPassword |
| 149 |
template: ansible_test |
| 150 |
name: ansible_pod_settings |
| 151 |
state: query |
| 152 |
register: query_with_name |
| 153 |
|
| 154 |
- name: Query the Pod Settings using UUID |
| 155 |
cisco.mso.ndo_pod_settings: |
| 156 |
host: mso_host |
| 157 |
username: admin |
| 158 |
password: SomeSecretPassword |
| 159 |
template: ansible_test |
| 160 |
uuid: "{{ create_pod_settings.current.uuid }}" |
| 161 |
state: query |
| 162 |
register: query_with_uuid |
| 163 |
|
| 164 |
- name: Query all Pod Settings in a template |
| 165 |
cisco.mso.ndo_pod_settings: |
| 166 |
host: mso_host |
| 167 |
username: admin |
| 168 |
password: SomeSecretPassword |
| 169 |
template: ansible_test |
| 170 |
state: query |
| 171 |
register: query_all_objects |
| 172 |
|
| 173 |
- name: Delete the Pod Settings using Name |
| 174 |
cisco.mso.ndo_pod_settings: |
| 175 |
host: mso_host |
| 176 |
username: admin |
| 177 |
password: SomeSecretPassword |
| 178 |
template: ansible_test |
| 179 |
name: ansible_pod_settings |
| 180 |
state: absent |
| 181 |
|
| 182 |
- name: Delete the Pod Settings using UUID |
| 183 |
cisco.mso.ndo_pod_settings: |
| 184 |
host: mso_host |
| 185 |
username: admin |
| 186 |
password: SomeSecretPassword |
| 187 |
template_id: ansible_test |
| 188 |
uuid: "{{ create_pod_settings.current.uuid }}" |
| 189 |
state: absent |
| 190 |
""" |
| 191 |
|
| 192 |
RETURN = r""" |
| 193 |
""" |
| 194 |
|
| 195 |
from ansible.module_utils.basic import AnsibleModule |
| 196 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 197 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 198 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, check_if_all_elements_are_none |
| 199 |
import copy |
| 200 |
|
| 201 |
|
| 202 |
def main(): |
| 203 |
argument_spec = mso_argument_spec() |
| 204 |
argument_spec.update( |
| 205 |
template=dict(type="str"), |
| 206 |
template_id=dict(type="str"), |
| 207 |
name=dict(type="str", aliases=["pod_settings"]), |
| 208 |
uuid=dict(type="str", aliases=["pod_settings_uuid"]), |
| 209 |
description=dict(type="str"), |
| 210 |
macsec_policy=dict( |
| 211 |
type="dict", |
| 212 |
options=dict( |
| 213 |
name=dict(type="str"), |
| 214 |
), |
| 215 |
), |
| 216 |
macsec_policy_uuid=dict(type="str"), |
| 217 |
ntp_policy=dict( |
| 218 |
type="dict", |
| 219 |
options=dict( |
| 220 |
name=dict(type="str"), |
| 221 |
), |
| 222 |
), |
| 223 |
ntp_policy_uuid=dict(type="str"), |
| 224 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 225 |
) |
| 226 |
|
| 227 |
module = AnsibleModule( |
| 228 |
argument_spec=argument_spec, |
| 229 |
supports_check_mode=True, |
| 230 |
mutually_exclusive=[ |
| 231 |
("template", "template_id"), |
| 232 |
("macsec_policy", "macsec_policy_uuid"), |
| 233 |
("ntp_policy", "ntp_policy_uuid"), |
| 234 |
], |
| 235 |
required_if=[ |
| 236 |
["state", "absent", ["name", "uuid"], True], |
| 237 |
["state", "present", ["name", "uuid"], True], |
| 238 |
], |
| 239 |
required_one_of=[ |
| 240 |
["template", "template_id"], |
| 241 |
], |
| 242 |
) |
| 243 |
|
| 244 |
mso = MSOModule(module) |
| 245 |
|
| 246 |
template_name = module.params.get("template") |
| 247 |
template_id = module.params.get("template_id") |
| 248 |
name = module.params.get("name") |
| 249 |
uuid = module.params.get("uuid") |
| 250 |
description = module.params.get("description") |
| 251 |
macsec_policy = module.params.get("macsec_policy") |
| 252 |
if macsec_policy is not None and check_if_all_elements_are_none(macsec_policy.values()): |
| 253 |
macsec_policy = {} |
| 254 |
macsec_policy_uuid = module.params.get("macsec_policy_uuid") |
| 255 |
ntp_policy = module.params.get("ntp_policy") |
| 256 |
if ntp_policy is not None and check_if_all_elements_are_none(ntp_policy.values()): |
| 257 |
ntp_policy = {} |
| 258 |
ntp_policy_uuid = module.params.get("ntp_policy_uuid") |
| 259 |
state = module.params.get("state") |
| 260 |
|
| 261 |
mso_template = MSOTemplate(mso, "fabric_policy", template_name, template_id) |
| 262 |
mso_template.validate_template("fabricPolicy") |
| 263 |
object_description = "Pod Settings" |
| 264 |
|
| 265 |
match = mso_template.get_pod_settings_object(uuid, name) |
| 266 |
|
| 267 |
if (uuid or name) and match: |
| 268 |
set_pod_settings_policy_names(mso_template, match.details) |
| 269 |
mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object |
| 270 |
elif match: |
| 271 |
mso.existing = [set_pod_settings_policy_names(mso_template, obj) for obj in match] # Query all objects |
| 272 |
|
| 273 |
pod_settings_path = "/fabricPolicyTemplate/template/podPolicyGroups/{0}".format(match.index if match else "-") |
| 274 |
|
| 275 |
ops = [] |
| 276 |
|
| 277 |
if state == "present": |
| 278 |
|
| 279 |
mso_values = { |
| 280 |
"name": name, |
| 281 |
"description": description, |
| 282 |
} |
| 283 |
|
| 284 |
if macsec_policy or macsec_policy_uuid: |
| 285 |
mso_values["fabricMACsec"] = mso_template.get_macsec_policy_object( |
| 286 |
macsec_policy_uuid, |
| 287 |
macsec_policy.get("name") if macsec_policy else macsec_policy, |
| 288 |
fail_module=True, |
| 289 |
).details.get("uuid") |
| 290 |
if ntp_policy or ntp_policy_uuid: |
| 291 |
mso_values["ntp"] = mso_template.get_ntp_policy_object( |
| 292 |
ntp_policy_uuid, |
| 293 |
ntp_policy.get("name") if ntp_policy else ntp_policy, |
| 294 |
fail_module=True, |
| 295 |
).details.get("uuid") |
| 296 |
|
| 297 |
if match: |
| 298 |
remove_data = [] |
| 299 |
unwanted = [] |
| 300 |
if macsec_policy == {} or macsec_policy_uuid == "": |
| 301 |
remove_data.append("fabricMACsec") |
| 302 |
unwanted.extend(["fabricMACsec", "fabricMACsecName"]) |
| 303 |
if ntp_policy == {} or ntp_policy_uuid == "": |
| 304 |
remove_data.append("ntp") |
| 305 |
unwanted.extend(["ntp", "ntpName"]) |
| 306 |
|
| 307 |
append_update_ops_data(ops, match.details, pod_settings_path, mso_values, remove_data) |
| 308 |
mso.sanitize(mso_values, collate=True, unwanted=unwanted) |
| 309 |
set_pod_settings_policy_names(mso_template, mso.proposed) |
| 310 |
else: |
| 311 |
mso.sanitize(mso_values) |
| 312 |
ops.append(dict(op="add", path=pod_settings_path, value=mso.sent)) |
| 313 |
|
| 314 |
elif state == "absent": |
| 315 |
if match: |
| 316 |
ops.append(dict(op="remove", path=pod_settings_path)) |
| 317 |
|
| 318 |
if not module.check_mode and ops: |
| 319 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 320 |
match = mso_template.get_pod_settings_object(uuid, name, search_object=response) |
| 321 |
if match: |
| 322 |
set_pod_settings_policy_names(mso_template, match.details) |
| 323 |
mso.existing = match.details # When the state is present |
| 324 |
else: |
| 325 |
mso.existing = {} # When the state is absent |
| 326 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 327 |
if mso.proposed: |
| 328 |
set_pod_settings_policy_names(mso_template, mso.proposed) |
| 329 |
mso.existing = mso.proposed if state == "present" else {} |
| 330 |
mso.exit_json() |
| 331 |
|
| 332 |
|
| 333 |
def set_pod_settings_policy_names(mso_template, pod_settings): |
| 334 |
pod_settings["templateName"] = mso_template.template_name |
| 335 |
pod_settings["templateId"] = mso_template.template_id |
| 336 |
if pod_settings.get("fabricMACsec"): |
| 337 |
pod_settings["fabricMACsecName"] = mso_template.get_macsec_policy_object(uuid=pod_settings["fabricMACsec"]).details.get("name") |
| 338 |
if pod_settings.get("ntp"): |
| 339 |
pod_settings["ntpName"] = mso_template.get_ntp_policy_object(uuid=pod_settings["ntp"]).details.get("name") |
| 340 |
return pod_settings |
| 341 |
|
| 342 |
|
| 343 |
if __name__ == "__main__": |
| 344 |
main() |