| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_rpm |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Install patch or feature rpms on Cisco NX-OS devices. |
| 29 |
description: |
| 30 |
- Install software maintenance upgrade (smu) RPMS and 3rd party RPMS on Cisco NX-OS |
| 31 |
devices. |
| 32 |
version_added: 1.0.0 |
| 33 |
author: Sai Chintalapudi (@saichint) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 7.0(3)I2(5), 7.0(3)I4(6), 7.0(3)I5(3), 7.0(3)I6(1), 7.0(3)I7(3) |
| 36 |
- Unsupported for Cisco MDS |
| 37 |
- For patches, the minimum platform version needed is 7.0(3)I2(5) |
| 38 |
- For feature rpms, the minimum platform version needed is 7.0(3)I6(1) |
| 39 |
- The module manages the entire RPM lifecycle (Add, activate, commit, deactivate, |
| 40 |
remove) |
| 41 |
- For reload patches, this module is NOT idempotent until the patch is committed. |
| 42 |
options: |
| 43 |
pkg: |
| 44 |
description: |
| 45 |
- Name of the RPM package. |
| 46 |
type: str |
| 47 |
file_system: |
| 48 |
description: |
| 49 |
- The remote file system of the device. If omitted, devices that support a file_system |
| 50 |
parameter will use their default values. |
| 51 |
default: bootflash |
| 52 |
type: str |
| 53 |
aggregate: |
| 54 |
description: |
| 55 |
- List of RPM/patch definitions. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
suboptions: |
| 59 |
pkg: |
| 60 |
description: |
| 61 |
- Name of the RPM package. |
| 62 |
required: True |
| 63 |
type: str |
| 64 |
file_system: |
| 65 |
description: |
| 66 |
- The remote file system of the device. If omitted, devices that support a file_system |
| 67 |
parameter will use their default values. |
| 68 |
type: str |
| 69 |
state: |
| 70 |
description: |
| 71 |
- If the state is present, the rpm will be installed, If the state is absent, |
| 72 |
it will be removed. |
| 73 |
choices: |
| 74 |
- present |
| 75 |
- absent |
| 76 |
type: str |
| 77 |
state: |
| 78 |
description: |
| 79 |
- If the state is present, the rpm will be installed, If the state is absent, |
| 80 |
it will be removed. |
| 81 |
default: present |
| 82 |
choices: |
| 83 |
- present |
| 84 |
- absent |
| 85 |
type: str |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = """ |
| 89 |
- cisco.nxos.nxos_rpm: |
| 90 |
pkg: nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000.rpm |
| 91 |
""" |
| 92 |
|
| 93 |
RETURN = """ |
| 94 |
commands: |
| 95 |
description: commands sent to the device |
| 96 |
returned: always |
| 97 |
type: list |
| 98 |
sample: ["install add bootflash:nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000.rpm forced", |
| 99 |
"install activate nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000 forced", |
| 100 |
"install commit nxos.sample-n9k_ALL-1.0.0-7.0.3.I7.3.lib32_n9000"] |
| 101 |
""" |
| 102 |
|
| 103 |
|
| 104 |
import time |
| 105 |
|
| 106 |
from copy import deepcopy |
| 107 |
|
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( |
| 110 |
remove_default_spec, |
| 111 |
) |
| 112 |
|
| 113 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 114 |
load_config, |
| 115 |
run_commands, |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def execute_show_command(command, module): |
| 120 |
iteration = 0 |
| 121 |
cmds = [{"command": command, "output": "text"}] |
| 122 |
|
| 123 |
while iteration < 10: |
| 124 |
body = run_commands(module, cmds)[0] |
| 125 |
if body: |
| 126 |
return body |
| 127 |
else: |
| 128 |
time.sleep(2) |
| 129 |
iteration += 1 |
| 130 |
|
| 131 |
|
| 132 |
def remote_file_exists(module, dst, file_system): |
| 133 |
command = "dir {0}:/{1}".format(file_system, dst) |
| 134 |
body = execute_show_command(command, module) |
| 135 |
if "No such file" in body: |
| 136 |
return False |
| 137 |
return True |
| 138 |
|
| 139 |
|
| 140 |
def config_cmd_operation(module, cmd): |
| 141 |
iteration = 0 |
| 142 |
while iteration < 10: |
| 143 |
msg = load_config(module, [cmd], True) |
| 144 |
if msg: |
| 145 |
if ( |
| 146 |
"another install operation is in progress" in msg[0].lower() |
| 147 |
or "failed" in msg[0].lower() |
| 148 |
): |
| 149 |
time.sleep(2) |
| 150 |
iteration += 1 |
| 151 |
else: |
| 152 |
return |
| 153 |
else: |
| 154 |
return |
| 155 |
|
| 156 |
|
| 157 |
def validate_operation(module, show_cmd, cfg_cmd, pkg, pkg_not_present): |
| 158 |
iteration = 0 |
| 159 |
while iteration < 10: |
| 160 |
body = execute_show_command(show_cmd, module) |
| 161 |
if pkg_not_present: |
| 162 |
if pkg not in body: |
| 163 |
return |
| 164 |
else: |
| 165 |
if pkg in body: |
| 166 |
return |
| 167 |
time.sleep(2) |
| 168 |
iteration += 1 |
| 169 |
|
| 170 |
err = 'Operation "{0}" Failed'.format(cfg_cmd) |
| 171 |
module.fail_json(msg=err) |
| 172 |
|
| 173 |
|
| 174 |
def add_operation(module, show_cmd, file_system, full_pkg, pkg): |
| 175 |
cmd = "install add {0}:{1}".format(file_system, full_pkg) |
| 176 |
config_cmd_operation(module, cmd) |
| 177 |
validate_operation(module, show_cmd, cmd, pkg, False) |
| 178 |
return cmd |
| 179 |
|
| 180 |
|
| 181 |
def activate_operation(module, show_cmd, pkg): |
| 182 |
cmd = "install activate {0} forced".format(pkg) |
| 183 |
config_cmd_operation(module, cmd) |
| 184 |
validate_operation(module, show_cmd, cmd, pkg, False) |
| 185 |
return cmd |
| 186 |
|
| 187 |
|
| 188 |
def activate_reload(module, pkg, flag): |
| 189 |
iteration = 0 |
| 190 |
if flag: |
| 191 |
cmd = "install activate {0} forced".format(pkg) |
| 192 |
else: |
| 193 |
cmd = "install deactivate {0} forced".format(pkg) |
| 194 |
opts = {"ignore_timeout": True} |
| 195 |
while iteration < 10: |
| 196 |
msg = load_config(module, [cmd], True, opts) |
| 197 |
if msg: |
| 198 |
if isinstance(msg[0], int): |
| 199 |
if msg[0] == -32603: |
| 200 |
return cmd |
| 201 |
elif isinstance(msg[0], str): |
| 202 |
if "socket is closed" in msg[0].lower(): |
| 203 |
return cmd |
| 204 |
if ( |
| 205 |
"another install operation is in progress" in msg[0].lower() |
| 206 |
or "failed" in msg[0].lower() |
| 207 |
): |
| 208 |
time.sleep(2) |
| 209 |
iteration += 1 |
| 210 |
|
| 211 |
|
| 212 |
def commit_operation(module, show_cmd, pkg, flag): |
| 213 |
cmd = "install commit {0}".format(pkg) |
| 214 |
config_cmd_operation(module, cmd) |
| 215 |
validate_operation(module, show_cmd, cmd, pkg, flag) |
| 216 |
return cmd |
| 217 |
|
| 218 |
|
| 219 |
def deactivate_operation(module, show_cmd, pkg, flag): |
| 220 |
cmd = "install deactivate {0} forced".format(pkg) |
| 221 |
config_cmd_operation(module, cmd) |
| 222 |
validate_operation(module, show_cmd, cmd, pkg, flag) |
| 223 |
return cmd |
| 224 |
|
| 225 |
|
| 226 |
def terminal_operation(module, config): |
| 227 |
if config: |
| 228 |
cmd = "terminal dont-ask" |
| 229 |
else: |
| 230 |
cmd = "no terminal dont-ask" |
| 231 |
config_cmd_operation(module, cmd) |
| 232 |
return cmd |
| 233 |
|
| 234 |
|
| 235 |
def remove_operation(module, show_cmd, pkg): |
| 236 |
commands = [] |
| 237 |
commands.append(terminal_operation(module, True)) |
| 238 |
cmd = "install remove {0} forced".format(pkg) |
| 239 |
config_cmd_operation(module, cmd) |
| 240 |
validate_operation(module, show_cmd, cmd, pkg, True) |
| 241 |
commands.append(cmd) |
| 242 |
commands.append(terminal_operation(module, False)) |
| 243 |
return commands |
| 244 |
|
| 245 |
|
| 246 |
def install_remove_rpm(module, full_pkg, file_system, state): |
| 247 |
commands = [] |
| 248 |
reload_patch = False |
| 249 |
|
| 250 |
splitted_pkg = full_pkg.split(".") |
| 251 |
pkg = ".".join(splitted_pkg[0:-1]) |
| 252 |
|
| 253 |
show_inactive = "show install inactive" |
| 254 |
show_active = "show install active" |
| 255 |
show_commit = "show install committed" |
| 256 |
show_patches = "show install patches" |
| 257 |
show_pkg_info = "show install pkg-info {0}".format(pkg) |
| 258 |
|
| 259 |
if state == "present": |
| 260 |
inactive_body = execute_show_command(show_inactive, module) |
| 261 |
active_body = execute_show_command(show_active, module) |
| 262 |
|
| 263 |
if pkg not in inactive_body and pkg not in active_body: |
| 264 |
commands.append(add_operation(module, show_inactive, file_system, full_pkg, pkg)) |
| 265 |
|
| 266 |
patch_type_body = execute_show_command(show_pkg_info, module) |
| 267 |
if patch_type_body and "Patch Type : reload" in patch_type_body: |
| 268 |
# This is reload smu/patch rpm |
| 269 |
reload_patch = True |
| 270 |
|
| 271 |
if pkg not in active_body: |
| 272 |
if reload_patch: |
| 273 |
commands.append(activate_reload(module, pkg, True)) |
| 274 |
return commands |
| 275 |
else: |
| 276 |
commands.append(activate_operation(module, show_active, pkg)) |
| 277 |
|
| 278 |
commit_body = execute_show_command(show_commit, module) |
| 279 |
if pkg not in commit_body: |
| 280 |
patch_body = execute_show_command(show_patches, module) |
| 281 |
if pkg in patch_body: |
| 282 |
# This is smu/patch rpm |
| 283 |
commands.append(commit_operation(module, show_commit, pkg, False)) |
| 284 |
else: |
| 285 |
err = 'Operation "install activate {0} forced" Failed'.format(pkg) |
| 286 |
module.fail_json(msg=err) |
| 287 |
|
| 288 |
else: |
| 289 |
commit_body = execute_show_command(show_commit, module) |
| 290 |
active_body = execute_show_command(show_active, module) |
| 291 |
|
| 292 |
patch_type_body = execute_show_command(show_pkg_info, module) |
| 293 |
if patch_type_body and "Patch Type : reload" in patch_type_body: |
| 294 |
# This is reload smu/patch rpm |
| 295 |
reload_patch = True |
| 296 |
|
| 297 |
if pkg in commit_body and pkg in active_body: |
| 298 |
if reload_patch: |
| 299 |
commands.append(activate_reload(module, pkg, False)) |
| 300 |
return commands |
| 301 |
else: |
| 302 |
commands.append(deactivate_operation(module, show_active, pkg, True)) |
| 303 |
commit_body = execute_show_command(show_commit, module) |
| 304 |
if pkg in commit_body: |
| 305 |
# This is smu/patch rpm |
| 306 |
commands.append(commit_operation(module, show_commit, pkg, True)) |
| 307 |
commands.extend(remove_operation(module, show_inactive, pkg)) |
| 308 |
|
| 309 |
elif pkg in commit_body: |
| 310 |
# This is smu/patch rpm |
| 311 |
commands.append(commit_operation(module, show_commit, pkg, True)) |
| 312 |
commands.extend(remove_operation(module, show_inactive, pkg)) |
| 313 |
|
| 314 |
elif pkg in active_body: |
| 315 |
# This is smu/patch rpm |
| 316 |
if reload_patch: |
| 317 |
commands.append(activate_reload(module, pkg, False)) |
| 318 |
return commands |
| 319 |
else: |
| 320 |
commands.append(deactivate_operation(module, show_inactive, pkg, False)) |
| 321 |
commands.extend(remove_operation(module, show_inactive, pkg)) |
| 322 |
|
| 323 |
else: |
| 324 |
inactive_body = execute_show_command(show_inactive, module) |
| 325 |
if pkg in inactive_body: |
| 326 |
commands.extend(remove_operation(module, show_inactive, pkg)) |
| 327 |
|
| 328 |
return commands |
| 329 |
|
| 330 |
|
| 331 |
def main(): |
| 332 |
element_spec = dict( |
| 333 |
pkg=dict(type="str"), |
| 334 |
file_system=dict(type="str", default="bootflash"), |
| 335 |
state=dict(choices=["absent", "present"], default="present"), |
| 336 |
) |
| 337 |
|
| 338 |
aggregate_spec = deepcopy(element_spec) |
| 339 |
aggregate_spec["pkg"] = dict(required=True) |
| 340 |
|
| 341 |
# remove default in aggregate spec, to handle common arguments |
| 342 |
remove_default_spec(aggregate_spec) |
| 343 |
|
| 344 |
argument_spec = dict(aggregate=dict(type="list", elements="dict", options=aggregate_spec)) |
| 345 |
|
| 346 |
argument_spec.update(element_spec) |
| 347 |
required_one_of = [["pkg", "aggregate"]] |
| 348 |
mutually_exclusive = [["pkg", "aggregate"]] |
| 349 |
|
| 350 |
module = AnsibleModule( |
| 351 |
argument_spec=argument_spec, |
| 352 |
required_one_of=required_one_of, |
| 353 |
mutually_exclusive=mutually_exclusive, |
| 354 |
supports_check_mode=False, |
| 355 |
) |
| 356 |
|
| 357 |
warnings = list() |
| 358 |
results = {"changed": False, "commands": [], "warnings": warnings} |
| 359 |
|
| 360 |
aggregate = module.params.get("aggregate") |
| 361 |
objects = [] |
| 362 |
if aggregate: |
| 363 |
for item in aggregate: |
| 364 |
for key in item: |
| 365 |
if item.get(key) is None: |
| 366 |
item[key] = module.params[key] |
| 367 |
|
| 368 |
d = item.copy() |
| 369 |
objects.append(d) |
| 370 |
else: |
| 371 |
objects.append( |
| 372 |
{ |
| 373 |
"pkg": module.params["pkg"], |
| 374 |
"file_system": module.params["file_system"], |
| 375 |
"state": module.params["state"], |
| 376 |
}, |
| 377 |
) |
| 378 |
|
| 379 |
for obj in objects: |
| 380 |
if obj["state"] == "present": |
| 381 |
remote_exists = remote_file_exists(module, obj["pkg"], file_system=obj["file_system"]) |
| 382 |
|
| 383 |
if not remote_exists: |
| 384 |
module.fail_json(msg="The requested package doesn't exist on the device") |
| 385 |
|
| 386 |
cmds = install_remove_rpm(module, obj["pkg"], obj["file_system"], obj["state"]) |
| 387 |
|
| 388 |
if cmds: |
| 389 |
results["changed"] = True |
| 390 |
results["commands"].extend(cmds) |
| 391 |
|
| 392 |
module.exit_json(**results) |
| 393 |
|
| 394 |
|
| 395 |
if __name__ == "__main__": |
| 396 |
main() |