| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_maintenance_policy |
| 16 |
short_description: Manage firmware maintenance policies (maint:MaintP) |
| 17 |
description: |
| 18 |
- Manage maintenance policies that defines behavior during an ACI upgrade. |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name for the maintenance policy. |
| 23 |
type: str |
| 24 |
aliases: [ maintenance_policy ] |
| 25 |
run_mode: |
| 26 |
description: |
| 27 |
- Whether the system pauses on error or just continues through it. |
| 28 |
- The APIC defaults to C(pauseOnlyOnFailures) when unset during creation. |
| 29 |
type: str |
| 30 |
choices: [ pause_always_between_sets, pause_only_on_failures, pause_never, pauseOnlyOnFailures, pauseNever ] |
| 31 |
aliases: [ runmode ] |
| 32 |
graceful: |
| 33 |
description: |
| 34 |
- Whether the system will bring down the nodes gracefully during an upgrade, which reduces traffic lost. |
| 35 |
- The APIC defaults to C(false) when unset during creation. |
| 36 |
type: bool |
| 37 |
scheduler: |
| 38 |
description: |
| 39 |
- The name of scheduler that is applied to the policy. |
| 40 |
type: str |
| 41 |
admin_state: |
| 42 |
description: |
| 43 |
- The administrative state of the executable policies. |
| 44 |
- Will trigger an immediate upgrade for nodes if C(admin_state) is set to triggered. |
| 45 |
- The APIC defaults to C(untriggered) when unset during creation. |
| 46 |
type: str |
| 47 |
choices: [ triggered, untriggered ] |
| 48 |
aliases: [ adminst ] |
| 49 |
download_state: |
| 50 |
description: |
| 51 |
- The download state of the executable policies. |
| 52 |
- The APIC defaults to C(untriggered) when unset during creation. |
| 53 |
type: str |
| 54 |
choices: [ triggered, untriggered ] |
| 55 |
notify_condition: |
| 56 |
description: |
| 57 |
- Specifies under what pause condition will admin be notified via email/text as configured. |
| 58 |
- This notification mechanism is independent of events/faults. |
| 59 |
- The APIC defaults to C(notifyOnlyOnFailures) when unset during creation. |
| 60 |
type: str |
| 61 |
choices: [ notify_always_between_sets, notify_never, notify_only_on_failures ] |
| 62 |
smu_operation: |
| 63 |
description: |
| 64 |
- Specifies that the upgrade is a Software Maintenance Upgrade (SMU) patch operation. |
| 65 |
type: str |
| 66 |
choices: [ smu_install, smu_uninstall ] |
| 67 |
smu_operation_flags: |
| 68 |
description: |
| 69 |
- Specifies the Software Maintenance Upgrade (SMU) patch operation flags |
| 70 |
- Indicates if node should be reloaded immediately or skip auto reload on SMU Install/Uninstall. |
| 71 |
type: str |
| 72 |
choices: [ smu_reload_immediate, smu_reload_skip ] |
| 73 |
sr_upgrade: |
| 74 |
description: |
| 75 |
- Specifies that the upgrade is a Silent Roll (SR) package upgrade. |
| 76 |
type: bool |
| 77 |
aliases: [ silent_roll_upgrade ] |
| 78 |
sr_version: |
| 79 |
description: |
| 80 |
- The target firmware version of the Silent Roll (SR) package upgrade to install. |
| 81 |
type: str |
| 82 |
aliases: [ silent_roll_version ] |
| 83 |
version: |
| 84 |
description: |
| 85 |
- The target firmware version to install. |
| 86 |
type: str |
| 87 |
version_check_override: |
| 88 |
description: |
| 89 |
- The version check override. |
| 90 |
- This is a directive to ignore the version check for the next install. |
| 91 |
- The version check, which occurs during a maintenance window, checks to see if the desired version matches the running version. |
| 92 |
- If the versions do not match, the install is performed. If the versions do match, the install is not performed. |
| 93 |
- The version check override is a one-time override that performs the install whether or not the versions match. |
| 94 |
- The APIC defaults to C(untriggered) when unset during creation. |
| 95 |
type: str |
| 96 |
choices: [ trigger, trigger_immediate, triggered, untriggered ] |
| 97 |
ignore_compat: |
| 98 |
description: |
| 99 |
- To check whether compatibility checks should be ignored |
| 100 |
- The APIC defaults to C(false) when unset during creation. |
| 101 |
type: bool |
| 102 |
aliases: [ ignoreCompat ] |
| 103 |
description: |
| 104 |
description: |
| 105 |
- Description for the maintenance policy. |
| 106 |
type: str |
| 107 |
aliases: [ descr ] |
| 108 |
state: |
| 109 |
description: |
| 110 |
- Use C(present) or C(absent) for adding or removing. |
| 111 |
- Use C(query) for listing an object or multiple objects. |
| 112 |
type: str |
| 113 |
choices: [ absent, present, query ] |
| 114 |
default: present |
| 115 |
name_alias: |
| 116 |
description: |
| 117 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 118 |
type: str |
| 119 |
extends_documentation_fragment: |
| 120 |
- cisco.aci.aci |
| 121 |
- cisco.aci.annotation |
| 122 |
|
| 123 |
notes: |
| 124 |
- A scheduler is required for this module, which could have been created using the M(cisco.aci.aci_fabric_scheduler) module or via the UI. |
| 125 |
seealso: |
| 126 |
- module: cisco.aci.aci_fabric_scheduler |
| 127 |
- name: APIC Management Information Model reference |
| 128 |
description: More information about the internal APIC class B(maint:MaintP). |
| 129 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 130 |
author: |
| 131 |
- Steven Gerhart (@sgerhart) |
| 132 |
- Gaspard Micol (@gmicol) |
| 133 |
""" |
| 134 |
|
| 135 |
EXAMPLES = r""" |
| 136 |
- name: Create a maintenance policy |
| 137 |
cisco.aci.aci_maintenance_policy: |
| 138 |
host: apic |
| 139 |
username: admin |
| 140 |
password: SomeSecretPassword |
| 141 |
name: my_maintenance_policy |
| 142 |
scheduler: simpleScheduler |
| 143 |
state: present |
| 144 |
delegate_to: localhost |
| 145 |
|
| 146 |
- name: Delete a maintenance policy |
| 147 |
cisco.aci.aci_maintenance_policy: |
| 148 |
host: apic |
| 149 |
username: admin |
| 150 |
password: SomeSecretPassword |
| 151 |
name: my_maintenance_policy |
| 152 |
state: absent |
| 153 |
delegate_to: localhost |
| 154 |
|
| 155 |
- name: Query all maintenance policies |
| 156 |
cisco.aci.aci_maintenance_policy: |
| 157 |
host: apic |
| 158 |
username: admin |
| 159 |
password: SomeSecretPassword |
| 160 |
state: query |
| 161 |
delegate_to: localhost |
| 162 |
register: query_result |
| 163 |
|
| 164 |
- name: Query a specific maintenance policy |
| 165 |
cisco.aci.aci_maintenance_policy: |
| 166 |
host: apic |
| 167 |
username: admin |
| 168 |
password: SomeSecretPassword |
| 169 |
name: my_maintenance_policy |
| 170 |
state: query |
| 171 |
delegate_to: localhost |
| 172 |
register: query_result |
| 173 |
""" |
| 174 |
|
| 175 |
RETURN = r""" |
| 176 |
current: |
| 177 |
description: The existing configuration from the APIC after the module has finished |
| 178 |
returned: success |
| 179 |
type: list |
| 180 |
sample: |
| 181 |
[ |
| 182 |
{ |
| 183 |
"fvTenant": { |
| 184 |
"attributes": { |
| 185 |
"descr": "Production environment", |
| 186 |
"dn": "uni/tn-production", |
| 187 |
"name": "production", |
| 188 |
"nameAlias": "", |
| 189 |
"ownerKey": "", |
| 190 |
"ownerTag": "" |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
] |
| 195 |
error: |
| 196 |
description: The error information as returned from the APIC |
| 197 |
returned: failure |
| 198 |
type: dict |
| 199 |
sample: |
| 200 |
{ |
| 201 |
"code": "122", |
| 202 |
"text": "unknown managed object class foo" |
| 203 |
} |
| 204 |
raw: |
| 205 |
description: The raw output returned by the APIC REST API (xml or json) |
| 206 |
returned: parse error |
| 207 |
type: str |
| 208 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 209 |
sent: |
| 210 |
description: The actual/minimal configuration pushed to the APIC |
| 211 |
returned: info |
| 212 |
type: list |
| 213 |
sample: |
| 214 |
{ |
| 215 |
"fvTenant": { |
| 216 |
"attributes": { |
| 217 |
"descr": "Production environment" |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
previous: |
| 222 |
description: The original configuration from the APIC before the module has started |
| 223 |
returned: info |
| 224 |
type: list |
| 225 |
sample: |
| 226 |
[ |
| 227 |
{ |
| 228 |
"fvTenant": { |
| 229 |
"attributes": { |
| 230 |
"descr": "Production", |
| 231 |
"dn": "uni/tn-production", |
| 232 |
"name": "production", |
| 233 |
"nameAlias": "", |
| 234 |
"ownerKey": "", |
| 235 |
"ownerTag": "" |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
] |
| 240 |
proposed: |
| 241 |
description: The assembled configuration from the user-provided parameters |
| 242 |
returned: info |
| 243 |
type: dict |
| 244 |
sample: |
| 245 |
{ |
| 246 |
"fvTenant": { |
| 247 |
"attributes": { |
| 248 |
"descr": "Production environment", |
| 249 |
"name": "production" |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
filter_string: |
| 254 |
description: The filter string used for the request |
| 255 |
returned: failure or debug |
| 256 |
type: str |
| 257 |
sample: ?rsp-prop-include=config-only |
| 258 |
method: |
| 259 |
description: The HTTP method used for the request to the APIC |
| 260 |
returned: failure or debug |
| 261 |
type: str |
| 262 |
sample: POST |
| 263 |
response: |
| 264 |
description: The HTTP response from the APIC |
| 265 |
returned: failure or debug |
| 266 |
type: str |
| 267 |
sample: OK (30 bytes) |
| 268 |
status: |
| 269 |
description: The HTTP status from the APIC |
| 270 |
returned: failure or debug |
| 271 |
type: int |
| 272 |
sample: 200 |
| 273 |
url: |
| 274 |
description: The HTTP url used for the request to the APIC |
| 275 |
returned: failure or debug |
| 276 |
type: str |
| 277 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 278 |
""" |
| 279 |
|
| 280 |
|
| 281 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 282 |
from ansible.module_utils.basic import AnsibleModule |
| 283 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import ( |
| 284 |
MATCH_RUN_MODE_MAPPING, |
| 285 |
MATCH_NOTIFY_CONDITION_MAPPING, |
| 286 |
MATCH_SMU_OPERATION_MAPPING, |
| 287 |
MATCH_SMU_OPERATION_FLAGS_MAPPING, |
| 288 |
MATCH_TRIGGER_MAPPING, |
| 289 |
) |
| 290 |
|
| 291 |
|
| 292 |
def main(): |
| 293 |
list_run_mode_choices = list(MATCH_RUN_MODE_MAPPING.keys()) |
| 294 |
list_run_mode_choices.extend(["pauseOnlyOnFailures", "pauseNever"]) |
| 295 |
argument_spec = aci_argument_spec() |
| 296 |
argument_spec.update(aci_annotation_spec()) |
| 297 |
argument_spec.update( |
| 298 |
name=dict(type="str", aliases=["maintenance_policy"]), # Not required for querying all objects |
| 299 |
run_mode=dict(type="str", choices=list_run_mode_choices, aliases=["runmode"]), |
| 300 |
graceful=dict(type="bool"), |
| 301 |
scheduler=dict(type="str"), |
| 302 |
ignore_compat=dict(type="bool", aliases=["ignoreCompat"]), |
| 303 |
admin_state=dict(type="str", choices=list(MATCH_TRIGGER_MAPPING.keys())[2:], aliases=["adminst"]), |
| 304 |
download_state=dict(type="str", choices=list(MATCH_TRIGGER_MAPPING.keys())[2:]), |
| 305 |
notify_condition=dict(type="str", choices=list(MATCH_NOTIFY_CONDITION_MAPPING.keys())), |
| 306 |
smu_operation=dict(type="str", choices=list(MATCH_SMU_OPERATION_MAPPING.keys())), |
| 307 |
smu_operation_flags=dict(type="str", choices=list(MATCH_SMU_OPERATION_FLAGS_MAPPING.keys())), |
| 308 |
sr_upgrade=dict(type="bool", aliases=["silent_roll_upgrade"]), |
| 309 |
sr_version=dict(type="str", aliases=["silent_roll_version"]), |
| 310 |
version=dict(type="str"), |
| 311 |
version_check_override=dict(type="str", choices=list(MATCH_TRIGGER_MAPPING.keys())), |
| 312 |
description=dict(type="str", aliases=["descr"]), |
| 313 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 314 |
name_alias=dict(type="str"), |
| 315 |
) |
| 316 |
|
| 317 |
module = AnsibleModule( |
| 318 |
argument_spec=argument_spec, |
| 319 |
supports_check_mode=True, |
| 320 |
required_if=[ |
| 321 |
["state", "absent", ["name"]], |
| 322 |
["state", "present", ["name", "scheduler"]], |
| 323 |
], |
| 324 |
) |
| 325 |
|
| 326 |
aci = ACIModule(module) |
| 327 |
|
| 328 |
state = module.params.get("state") |
| 329 |
name = module.params.get("name") |
| 330 |
run_mode = module.params.get("run_mode") |
| 331 |
graceful = aci.boolean(module.params.get("graceful"), "yes", "no") |
| 332 |
scheduler = module.params.get("scheduler") |
| 333 |
admin_state = module.params.get("admin_state") |
| 334 |
download_state = module.params.get("download_state") |
| 335 |
notify_condition = MATCH_NOTIFY_CONDITION_MAPPING.get(module.params.get("notify_condition")) |
| 336 |
smu_operation = MATCH_SMU_OPERATION_MAPPING.get(module.params.get("smu_operation")) |
| 337 |
smu_operation_flags = MATCH_SMU_OPERATION_FLAGS_MAPPING.get(module.params.get("smu_operation_flags")) |
| 338 |
sr_version = module.params.get("sr_version") |
| 339 |
sr_upgrade = module.params.get("sr_upgrade") |
| 340 |
version = module.params.get("version") |
| 341 |
version_check_override = MATCH_TRIGGER_MAPPING.get(module.params.get("version_check_override")) |
| 342 |
ignore_compat = aci.boolean(module.params.get("ignore_compat")) |
| 343 |
description = module.params.get("description") |
| 344 |
name_alias = module.params.get("name_alias") |
| 345 |
|
| 346 |
if run_mode not in ["pauseOnlyOnFailures", "pauseNever"]: |
| 347 |
run_mode = MATCH_RUN_MODE_MAPPING.get(run_mode) |
| 348 |
|
| 349 |
aci.construct_url( |
| 350 |
root_class=dict( |
| 351 |
aci_class="maintMaintP", |
| 352 |
aci_rn="fabric/maintpol-{0}".format(name), |
| 353 |
target_filter={"name": name}, |
| 354 |
module_object=name, |
| 355 |
), |
| 356 |
child_classes=["maintRsPolScheduler"], |
| 357 |
) |
| 358 |
|
| 359 |
aci.get_existing() |
| 360 |
|
| 361 |
if state == "present": |
| 362 |
aci.payload( |
| 363 |
aci_class="maintMaintP", |
| 364 |
class_config=dict( |
| 365 |
name=name, |
| 366 |
descr=description, |
| 367 |
runMode=run_mode, |
| 368 |
graceful=graceful, |
| 369 |
adminSt=admin_state, |
| 370 |
downloadSt=download_state, |
| 371 |
notifCond=notify_condition, |
| 372 |
smuOperation=smu_operation, |
| 373 |
smuOperationFlags=smu_operation_flags, |
| 374 |
srUpgrade=sr_upgrade, |
| 375 |
srVersion=sr_version, |
| 376 |
version=version, |
| 377 |
versionCheckOverride=version_check_override, |
| 378 |
ignoreCompat=ignore_compat, |
| 379 |
nameAlias=name_alias, |
| 380 |
), |
| 381 |
child_configs=[ |
| 382 |
dict( |
| 383 |
maintRsPolScheduler=dict( |
| 384 |
attributes=dict( |
| 385 |
tnTrigSchedPName=scheduler, |
| 386 |
), |
| 387 |
), |
| 388 |
), |
| 389 |
], |
| 390 |
) |
| 391 |
|
| 392 |
aci.get_diff(aci_class="maintMaintP") |
| 393 |
|
| 394 |
aci.post_config() |
| 395 |
|
| 396 |
elif state == "absent": |
| 397 |
aci.delete_config() |
| 398 |
|
| 399 |
aci.exit_json() |
| 400 |
|
| 401 |
|
| 402 |
if __name__ == "__main__": |
| 403 |
main() |