| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Patrick Callahan <[email protected]> |
| 4 |
# based on |
| 5 |
# openbsd_pkg |
| 6 |
# Copyright (c) 2013 |
| 7 |
# Patrik Lundin <[email protected]> |
| 8 |
# |
| 9 |
# yum |
| 10 |
# Copyright (c) 2012, Red Hat, Inc |
| 11 |
# Written by Seth Vidal <skvidal at fedoraproject.org> |
| 12 |
# |
| 13 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 14 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
DOCUMENTATION = r""" |
| 19 |
module: zypper |
| 20 |
author: |
| 21 |
- "Patrick Callahan (@dirtyharrycallahan)" |
| 22 |
- "Alexander Gubin (@alxgu)" |
| 23 |
- "Thomas O'Donnell (@andytom)" |
| 24 |
- "Robin Roth (@robinro)" |
| 25 |
- "Andrii Radyk (@AnderEnder)" |
| 26 |
short_description: Manage packages on SUSE and openSUSE |
| 27 |
description: |
| 28 |
- Manage packages on SUSE and openSUSE using the zypper and rpm tools. |
| 29 |
- Also supports transactional updates, by running zypper inside C(/sbin/transactional-update --continue --drop-if-no-change |
| 30 |
--quiet run). |
| 31 |
extends_documentation_fragment: |
| 32 |
- community.general._attributes |
| 33 |
attributes: |
| 34 |
check_mode: |
| 35 |
support: full |
| 36 |
diff_mode: |
| 37 |
support: full |
| 38 |
options: |
| 39 |
name: |
| 40 |
description: |
| 41 |
- Package name V(name) or package specifier or a list of either. |
| 42 |
- Can include a version like V(name=1.0), V(name>3.4) or V(name<=2.7). If a version is given, V(oldpackage) is implied |
| 43 |
and zypper is allowed to update the package within the version range given. |
| 44 |
- You can also pass a URL or a local path to a rpm file. |
| 45 |
- When using O(state=latest), this can be V(*), which updates all installed packages. |
| 46 |
required: true |
| 47 |
aliases: ['pkg'] |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
state: |
| 51 |
description: |
| 52 |
- V(present) makes sure the package is installed. |
| 53 |
- V(latest) makes sure the latest version of the package is installed. |
| 54 |
- V(absent) makes sure the specified package is not installed. |
| 55 |
- V(dist-upgrade) makes sure the latest version of all installed packages from all enabled repositories is installed. |
| 56 |
- When using V(dist-upgrade), O(name) should be V(*). |
| 57 |
choices: [present, latest, absent, dist-upgrade, installed, removed] |
| 58 |
default: "present" |
| 59 |
type: str |
| 60 |
type: |
| 61 |
description: |
| 62 |
- The type of package to be operated on. |
| 63 |
choices: [package, patch, pattern, product, srcpackage, application] |
| 64 |
default: "package" |
| 65 |
type: str |
| 66 |
extra_args_precommand: |
| 67 |
description: |
| 68 |
- Add additional global target options to C(zypper). |
| 69 |
- Options should be supplied in a single line as if given in the command line. |
| 70 |
type: str |
| 71 |
disable_gpg_check: |
| 72 |
description: |
| 73 |
- Whether to disable to GPG signature checking of the package signature being installed. Has an effect only if O(state) |
| 74 |
is V(present) or V(latest). |
| 75 |
default: false |
| 76 |
type: bool |
| 77 |
auto_import_keys: |
| 78 |
description: |
| 79 |
- Whether to automatically import new repository signing keys. Adds C(--gpg-auto-import-keys) option to I(zypper). |
| 80 |
- Is only used when installing. |
| 81 |
default: false |
| 82 |
type: bool |
| 83 |
version_added: 11.3.0 |
| 84 |
disable_recommends: |
| 85 |
description: |
| 86 |
- Corresponds to the C(--no-recommends) option for I(zypper). Default behavior (V(true)) modifies zypper's default behavior; |
| 87 |
V(false) does install recommended packages. |
| 88 |
default: true |
| 89 |
type: bool |
| 90 |
force: |
| 91 |
description: |
| 92 |
- Adds C(--force) option to I(zypper). Allows to downgrade packages and change vendor or architecture. |
| 93 |
default: false |
| 94 |
type: bool |
| 95 |
force_resolution: |
| 96 |
description: |
| 97 |
- Adds C(--force-resolution) option to I(zypper). Allows to (un)install packages with conflicting requirements (resolver |
| 98 |
chooses a solution). |
| 99 |
default: false |
| 100 |
type: bool |
| 101 |
version_added: '0.2.0' |
| 102 |
update_cache: |
| 103 |
description: |
| 104 |
- Run the equivalent of C(zypper refresh) before the operation. Disabled in check mode. |
| 105 |
default: false |
| 106 |
type: bool |
| 107 |
aliases: ["refresh"] |
| 108 |
oldpackage: |
| 109 |
description: |
| 110 |
- Adds C(--oldpackage) option to I(zypper). Allows to downgrade packages with less side-effects than force. This is |
| 111 |
implied as soon as a version is specified as part of the package name. |
| 112 |
default: false |
| 113 |
type: bool |
| 114 |
extra_args: |
| 115 |
description: |
| 116 |
- Add additional options to C(zypper) command. |
| 117 |
- Options should be supplied in a single line as if given in the command line. |
| 118 |
type: str |
| 119 |
allow_vendor_change: |
| 120 |
type: bool |
| 121 |
default: false |
| 122 |
description: |
| 123 |
- Adds C(--allow_vendor_change) option to I(zypper) dist-upgrade command. |
| 124 |
version_added: '0.2.0' |
| 125 |
replacefiles: |
| 126 |
type: bool |
| 127 |
default: false |
| 128 |
description: |
| 129 |
- Adds C(--replacefiles) option to I(zypper) install/update command. |
| 130 |
version_added: '0.2.0' |
| 131 |
clean_deps: |
| 132 |
type: bool |
| 133 |
default: false |
| 134 |
description: |
| 135 |
- Adds C(--clean-deps) option to I(zypper) remove command. |
| 136 |
version_added: '4.6.0' |
| 137 |
simple_errors: |
| 138 |
type: bool |
| 139 |
default: false |
| 140 |
description: |
| 141 |
- When set to V(true), provide a simplified error output (parses only the C(<message>) tag text in the XML output). |
| 142 |
version_added: '10.2.0' |
| 143 |
quiet: |
| 144 |
type: bool |
| 145 |
default: true |
| 146 |
description: |
| 147 |
- Adds C(--quiet) option to I(zypper) install/update command. |
| 148 |
version_added: '10.2.0' |
| 149 |
skip_post_errors: |
| 150 |
type: bool |
| 151 |
default: false |
| 152 |
description: |
| 153 |
- When set to V(true), ignore I(zypper) return code 107 (post install script errors). |
| 154 |
version_added: '10.6.0' |
| 155 |
notes: |
| 156 |
- When used with a C(loop:) each package is processed individually, it is much more efficient to pass the list directly |
| 157 |
to the O(name) option. |
| 158 |
requirements: |
| 159 |
- "zypper >= 1.0 # included in openSUSE >= 11.1 or SUSE Linux Enterprise Server/Desktop >= 11.0" |
| 160 |
- python-xml |
| 161 |
- rpm |
| 162 |
""" |
| 163 |
|
| 164 |
EXAMPLES = r""" |
| 165 |
- name: Install nmap |
| 166 |
community.general.zypper: |
| 167 |
name: nmap |
| 168 |
state: present |
| 169 |
|
| 170 |
- name: Install apache2 with recommended packages |
| 171 |
community.general.zypper: |
| 172 |
name: apache2 |
| 173 |
state: present |
| 174 |
disable_recommends: false |
| 175 |
|
| 176 |
- name: Apply a given patch |
| 177 |
community.general.zypper: |
| 178 |
name: openSUSE-2016-128 |
| 179 |
state: present |
| 180 |
type: patch |
| 181 |
|
| 182 |
- name: Remove the nmap package |
| 183 |
community.general.zypper: |
| 184 |
name: nmap |
| 185 |
state: absent |
| 186 |
|
| 187 |
- name: Install the nginx rpm from a remote repo |
| 188 |
community.general.zypper: |
| 189 |
name: 'http://nginx.org/packages/sles/12/x86_64/RPMS/nginx-1.8.0-1.sles12.ngx.x86_64.rpm' |
| 190 |
state: present |
| 191 |
|
| 192 |
- name: Install local rpm file |
| 193 |
community.general.zypper: |
| 194 |
name: /tmp/fancy-software.rpm |
| 195 |
state: present |
| 196 |
|
| 197 |
- name: Update all packages |
| 198 |
community.general.zypper: |
| 199 |
name: '*' |
| 200 |
state: latest |
| 201 |
|
| 202 |
- name: Install latest packages but dump error messages in a simplified format |
| 203 |
community.general.zypper: |
| 204 |
name: '*' |
| 205 |
state: latest |
| 206 |
simple_errors: true |
| 207 |
quiet: false |
| 208 |
|
| 209 |
- name: Apply all available patches |
| 210 |
community.general.zypper: |
| 211 |
name: '*' |
| 212 |
state: latest |
| 213 |
type: patch |
| 214 |
|
| 215 |
- name: Perform a dist-upgrade with additional arguments |
| 216 |
community.general.zypper: |
| 217 |
name: '*' |
| 218 |
state: dist-upgrade |
| 219 |
allow_vendor_change: true |
| 220 |
extra_args: '--allow-arch-change' |
| 221 |
|
| 222 |
- name: Perform a installation of nmap with the install option replacefiles |
| 223 |
community.general.zypper: |
| 224 |
name: 'nmap' |
| 225 |
state: latest |
| 226 |
replacefiles: true |
| 227 |
|
| 228 |
- name: Refresh repositories and update package openssl |
| 229 |
community.general.zypper: |
| 230 |
name: openssl |
| 231 |
state: present |
| 232 |
update_cache: true |
| 233 |
|
| 234 |
- name: "Install specific version (possible comparisons: <, >, <=, >=, =)" |
| 235 |
community.general.zypper: |
| 236 |
name: 'docker>=1.10' |
| 237 |
state: present |
| 238 |
|
| 239 |
- name: Wait 20 seconds to acquire the lock before failing |
| 240 |
community.general.zypper: |
| 241 |
name: mosh |
| 242 |
state: present |
| 243 |
environment: |
| 244 |
ZYPP_LOCK_TIMEOUT: 20 |
| 245 |
|
| 246 |
- name: Install the package with post-install error without failing |
| 247 |
community.general.zypper: |
| 248 |
name: <package_with_post_install_error> |
| 249 |
state: present |
| 250 |
skip_post_errors: true |
| 251 |
""" |
| 252 |
|
| 253 |
import os.path |
| 254 |
import re |
| 255 |
import xml |
| 256 |
from xml.dom.minidom import parseString as parseXML |
| 257 |
|
| 258 |
# import module snippets |
| 259 |
from ansible.module_utils.basic import AnsibleModule |
| 260 |
|
| 261 |
|
| 262 |
class Package: |
| 263 |
def __init__(self, name, prefix, version): |
| 264 |
self.name = name |
| 265 |
self.prefix = prefix |
| 266 |
self.version = version |
| 267 |
self.shouldinstall = prefix == "+" |
| 268 |
|
| 269 |
def __str__(self): |
| 270 |
return self.prefix + self.name + self.version |
| 271 |
|
| 272 |
|
| 273 |
def split_name_version(name): |
| 274 |
"""splits of the package name and desired version |
| 275 |
|
| 276 |
example formats: |
| 277 |
- docker>=1.10 |
| 278 |
- apache=2.4 |
| 279 |
|
| 280 |
Allowed version specifiers: <, >, <=, >=, = |
| 281 |
Allowed version format: [0-9.-]* |
| 282 |
|
| 283 |
Also allows a prefix indicating remove "-", "~" or install "+" |
| 284 |
""" |
| 285 |
|
| 286 |
prefix = "" |
| 287 |
if name[0] in ["-", "~", "+"]: |
| 288 |
prefix = name[0] |
| 289 |
name = name[1:] |
| 290 |
if prefix == "~": |
| 291 |
prefix = "-" |
| 292 |
|
| 293 |
version_check = re.compile("^(.*?)((?:<|>|<=|>=|=)[0-9.-]*)?$") |
| 294 |
try: |
| 295 |
reres = version_check.match(name) |
| 296 |
name, version = reres.groups() |
| 297 |
if version is None: |
| 298 |
version = "" |
| 299 |
return prefix, name, version |
| 300 |
except Exception: |
| 301 |
return prefix, name, "" |
| 302 |
|
| 303 |
|
| 304 |
def get_want_state(names, remove=False): |
| 305 |
packages = [] |
| 306 |
urls = [] |
| 307 |
for name in names: |
| 308 |
if "://" in name or name.endswith(".rpm"): |
| 309 |
urls.append(name) |
| 310 |
else: |
| 311 |
prefix, pname, version = split_name_version(name) |
| 312 |
if prefix not in ["-", "+"]: |
| 313 |
if remove: |
| 314 |
prefix = "-" |
| 315 |
else: |
| 316 |
prefix = "+" |
| 317 |
packages.append(Package(pname, prefix, version)) |
| 318 |
return packages, urls |
| 319 |
|
| 320 |
|
| 321 |
def get_installed_state(m, packages): |
| 322 |
"get installed state of packages" |
| 323 |
|
| 324 |
cmd = get_cmd(m, "search") |
| 325 |
cmd.extend(["--match-exact", "--details", "--installed-only"]) |
| 326 |
cmd.extend([p.name for p in packages]) |
| 327 |
return parse_zypper_xml(m, cmd, fail_not_found=False)[0] |
| 328 |
|
| 329 |
|
| 330 |
def parse_zypper_xml(m, cmd, fail_not_found=True, packages=None): |
| 331 |
rc, stdout, stderr = m.run_command(cmd, check_rc=False) |
| 332 |
|
| 333 |
try: |
| 334 |
dom = parseXML(stdout) |
| 335 |
except xml.parsers.expat.ExpatError as exc: |
| 336 |
m.fail_json(msg=f"Failed to parse zypper xml output: {exc}", rc=rc, stdout=stdout, stderr=stderr, cmd=cmd) |
| 337 |
|
| 338 |
if rc == 104: |
| 339 |
# exit code 104 is ZYPPER_EXIT_INF_CAP_NOT_FOUND (no packages found) |
| 340 |
if fail_not_found: |
| 341 |
errmsg = dom.getElementsByTagName("message")[-1].childNodes[0].data |
| 342 |
m.fail_json(msg=errmsg, rc=rc, stdout=stdout, stderr=stderr, cmd=cmd) |
| 343 |
else: |
| 344 |
return {}, rc, stdout, stderr |
| 345 |
elif rc in [0, 102, 103, 106, 107]: |
| 346 |
# zypper exit codes |
| 347 |
# 0: success |
| 348 |
# 106: signature verification failed |
| 349 |
# 102: ZYPPER_EXIT_INF_REBOOT_NEEDED - Returned after a successful installation of a patch which requires reboot of computer. |
| 350 |
# 103: zypper was upgraded, run same command again |
| 351 |
# 107: ZYPPER_EXIT_INF_RPM_SCRIPT_FAILED - Some of the packages %post install scripts returned an error, but package is installed. |
| 352 |
if packages is None: |
| 353 |
firstrun = True |
| 354 |
packages = {} |
| 355 |
else: |
| 356 |
firstrun = False |
| 357 |
solvable_list = dom.getElementsByTagName("solvable") |
| 358 |
for solvable in solvable_list: |
| 359 |
name = solvable.getAttribute("name") |
| 360 |
packages[name] = {} |
| 361 |
packages[name]["version"] = solvable.getAttribute("edition") |
| 362 |
packages[name]["oldversion"] = solvable.getAttribute("edition-old") |
| 363 |
status = solvable.getAttribute("status") |
| 364 |
packages[name]["installed"] = status == "installed" |
| 365 |
packages[name]["group"] = solvable.parentNode.nodeName |
| 366 |
if rc == 103 and firstrun: |
| 367 |
# if this was the first run and it failed with 103 |
| 368 |
# run zypper again with the same command to complete update |
| 369 |
return parse_zypper_xml(m, cmd, fail_not_found=fail_not_found, packages=packages) |
| 370 |
if rc == 107 and m.params["skip_post_errors"] and firstrun: |
| 371 |
# if this was the first run and it failed with 107 with skip_post_errors flag |
| 372 |
# run zypper again with the same command to complete update |
| 373 |
return parse_zypper_xml(m, cmd, fail_not_found=fail_not_found, packages=packages) |
| 374 |
|
| 375 |
# apply simple_errors logic to rc 0,102,103,106,107 |
| 376 |
if m.params["simple_errors"]: |
| 377 |
stdout = get_simple_errors(dom) or stdout |
| 378 |
|
| 379 |
return packages, rc, stdout, stderr |
| 380 |
|
| 381 |
# apply simple_errors logic to rc other than 0,102,103,106,107 |
| 382 |
if m.params["simple_errors"]: |
| 383 |
stdout = get_simple_errors(dom) or stdout |
| 384 |
|
| 385 |
m.fail_json(msg=f"Zypper run command failed with return code {rc}.", rc=rc, stdout=stdout, stderr=stderr, cmd=cmd) |
| 386 |
|
| 387 |
|
| 388 |
def get_simple_errors(dom): |
| 389 |
simple_errors = [] |
| 390 |
message_xml_tags = dom.getElementsByTagName("message") |
| 391 |
|
| 392 |
if message_xml_tags is None: |
| 393 |
return None |
| 394 |
|
| 395 |
for x in message_xml_tags: |
| 396 |
simple_errors.append(x.firstChild.data) |
| 397 |
return " \n".join(simple_errors) |
| 398 |
|
| 399 |
|
| 400 |
def get_cmd(m, subcommand): |
| 401 |
"puts together the basic zypper command arguments with those passed to the module" |
| 402 |
is_install = subcommand in ["install", "update", "patch", "dist-upgrade"] |
| 403 |
is_refresh = subcommand == "refresh" |
| 404 |
cmd = [m.get_bin_path("zypper", required=True), "--non-interactive", "--xmlout"] |
| 405 |
if m.params["quiet"]: |
| 406 |
cmd.append("--quiet") |
| 407 |
if transactional_updates(): |
| 408 |
cmd = [ |
| 409 |
m.get_bin_path("transactional-update", required=True), |
| 410 |
"--continue", |
| 411 |
"--drop-if-no-change", |
| 412 |
"--quiet", |
| 413 |
"run", |
| 414 |
] + cmd |
| 415 |
if m.params["extra_args_precommand"]: |
| 416 |
args_list = m.params["extra_args_precommand"].split() |
| 417 |
cmd.extend(args_list) |
| 418 |
# add global options before zypper command |
| 419 |
if (is_install or is_refresh) and m.params["disable_gpg_check"]: |
| 420 |
cmd.append("--no-gpg-checks") |
| 421 |
if is_install and m.params["auto_import_keys"]: |
| 422 |
cmd.append("--gpg-auto-import-keys") |
| 423 |
|
| 424 |
if subcommand == "search": |
| 425 |
cmd.append("--disable-repositories") |
| 426 |
|
| 427 |
cmd.append(subcommand) |
| 428 |
if subcommand not in ["patch", "dist-upgrade"] and not is_refresh: |
| 429 |
cmd.extend(["--type", m.params["type"]]) |
| 430 |
if m.check_mode and subcommand != "search": |
| 431 |
cmd.append("--dry-run") |
| 432 |
if is_install: |
| 433 |
cmd.append("--auto-agree-with-licenses") |
| 434 |
if m.params["disable_recommends"]: |
| 435 |
cmd.append("--no-recommends") |
| 436 |
if m.params["force"]: |
| 437 |
cmd.append("--force") |
| 438 |
if m.params["force_resolution"]: |
| 439 |
cmd.append("--force-resolution") |
| 440 |
if m.params["oldpackage"]: |
| 441 |
cmd.append("--oldpackage") |
| 442 |
if m.params["replacefiles"]: |
| 443 |
cmd.append("--replacefiles") |
| 444 |
if subcommand == "remove": |
| 445 |
if m.params["clean_deps"]: |
| 446 |
cmd.append("--clean-deps") |
| 447 |
if subcommand == "dist-upgrade" and m.params["allow_vendor_change"]: |
| 448 |
cmd.append("--allow-vendor-change") |
| 449 |
if m.params["extra_args"]: |
| 450 |
args_list = m.params["extra_args"].split(" ") |
| 451 |
cmd.extend(args_list) |
| 452 |
|
| 453 |
return cmd |
| 454 |
|
| 455 |
|
| 456 |
def set_diff(m, retvals, result): |
| 457 |
# TODO: if there is only one package, set before/after to version numbers |
| 458 |
packages = {"installed": [], "removed": [], "upgraded": []} |
| 459 |
if result: |
| 460 |
for p in result: |
| 461 |
group = result[p]["group"] |
| 462 |
if group == "to-upgrade": |
| 463 |
versions = f" ({result[p]['oldversion']} => {result[p]['version']})" |
| 464 |
packages["upgraded"].append(p + versions) |
| 465 |
elif group == "to-install": |
| 466 |
packages["installed"].append(p) |
| 467 |
elif group == "to-remove": |
| 468 |
packages["removed"].append(p) |
| 469 |
|
| 470 |
output = "" |
| 471 |
for state in packages: |
| 472 |
if packages[state]: |
| 473 |
output += f"{state}: {', '.join(packages[state])}\n" |
| 474 |
if "diff" not in retvals: |
| 475 |
retvals["diff"] = {} |
| 476 |
if "prepared" not in retvals["diff"]: |
| 477 |
retvals["diff"]["prepared"] = output |
| 478 |
else: |
| 479 |
retvals["diff"]["prepared"] += f"\n{output}" |
| 480 |
|
| 481 |
|
| 482 |
def package_present(m, name, want_latest): |
| 483 |
"install and update (if want_latest) the packages in name_install, while removing the packages in name_remove" |
| 484 |
retvals = {"rc": 0, "stdout": "", "stderr": ""} |
| 485 |
packages, urls = get_want_state(name) |
| 486 |
|
| 487 |
# add oldpackage flag when a version is given to allow downgrades |
| 488 |
if any(p.version for p in packages): |
| 489 |
m.params["oldpackage"] = True |
| 490 |
|
| 491 |
if not want_latest: |
| 492 |
# for state=present: filter out already installed packages |
| 493 |
# if a version is given leave the package in to let zypper handle the version |
| 494 |
# resolution |
| 495 |
packageswithoutversion = [p for p in packages if not p.version] |
| 496 |
prerun_state = {} |
| 497 |
if packageswithoutversion: |
| 498 |
prerun_state = get_installed_state(m, packageswithoutversion) |
| 499 |
# generate lists of packages to install or remove |
| 500 |
packages = [p for p in packages if p.shouldinstall != (p.name in prerun_state)] |
| 501 |
|
| 502 |
if not packages and not urls: |
| 503 |
# nothing to install/remove and nothing to update |
| 504 |
return None, retvals |
| 505 |
|
| 506 |
# zypper install also updates packages |
| 507 |
cmd = get_cmd(m, "install") |
| 508 |
cmd.append("--") |
| 509 |
cmd.extend(urls) |
| 510 |
# pass packages to zypper |
| 511 |
# allow for + or - prefixes in install/remove lists |
| 512 |
# also add version specifier if given |
| 513 |
# do this in one zypper run to allow for dependency-resolution |
| 514 |
# for example "-exim postfix" runs without removing packages depending on mailserver |
| 515 |
cmd.extend([str(p) for p in packages]) |
| 516 |
|
| 517 |
retvals["cmd"] = cmd |
| 518 |
result, retvals["rc"], retvals["stdout"], retvals["stderr"] = parse_zypper_xml(m, cmd) |
| 519 |
|
| 520 |
return result, retvals |
| 521 |
|
| 522 |
|
| 523 |
def package_update_all(m): |
| 524 |
"run update or patch on all available packages" |
| 525 |
|
| 526 |
retvals = {"rc": 0, "stdout": "", "stderr": ""} |
| 527 |
if m.params["type"] == "patch": |
| 528 |
cmdname = "patch" |
| 529 |
elif m.params["state"] == "dist-upgrade": |
| 530 |
cmdname = "dist-upgrade" |
| 531 |
else: |
| 532 |
cmdname = "update" |
| 533 |
|
| 534 |
cmd = get_cmd(m, cmdname) |
| 535 |
retvals["cmd"] = cmd |
| 536 |
result, retvals["rc"], retvals["stdout"], retvals["stderr"] = parse_zypper_xml(m, cmd) |
| 537 |
return result, retvals |
| 538 |
|
| 539 |
|
| 540 |
def package_absent(m, name): |
| 541 |
"remove the packages in name" |
| 542 |
retvals = {"rc": 0, "stdout": "", "stderr": ""} |
| 543 |
# Get package state |
| 544 |
packages, urls = get_want_state(name, remove=True) |
| 545 |
if any(p.prefix == "+" for p in packages): |
| 546 |
m.fail_json(msg="Can not combine '+' prefix with state=remove/absent.") |
| 547 |
if urls: |
| 548 |
m.fail_json(msg="Can not remove via URL.") |
| 549 |
if m.params["type"] == "patch": |
| 550 |
m.fail_json(msg="Can not remove patches.") |
| 551 |
prerun_state = get_installed_state(m, packages) |
| 552 |
packages = [p for p in packages if p.name in prerun_state] |
| 553 |
|
| 554 |
if not packages: |
| 555 |
return None, retvals |
| 556 |
|
| 557 |
cmd = get_cmd(m, "remove") |
| 558 |
cmd.extend([p.name + p.version for p in packages]) |
| 559 |
|
| 560 |
retvals["cmd"] = cmd |
| 561 |
result, retvals["rc"], retvals["stdout"], retvals["stderr"] = parse_zypper_xml(m, cmd) |
| 562 |
return result, retvals |
| 563 |
|
| 564 |
|
| 565 |
def repo_refresh(m): |
| 566 |
"update the repositories" |
| 567 |
retvals = {"rc": 0, "stdout": "", "stderr": ""} |
| 568 |
|
| 569 |
cmd = get_cmd(m, "refresh") |
| 570 |
|
| 571 |
retvals["cmd"] = cmd |
| 572 |
result, retvals["rc"], retvals["stdout"], retvals["stderr"] = parse_zypper_xml(m, cmd) |
| 573 |
|
| 574 |
return retvals |
| 575 |
|
| 576 |
|
| 577 |
def get_fs_type_and_readonly_state(mount_point): |
| 578 |
with open("/proc/mounts") as file: |
| 579 |
for line in file.readlines(): |
| 580 |
fields = line.split() |
| 581 |
path = fields[1] |
| 582 |
if path == mount_point: |
| 583 |
fs = fields[2] |
| 584 |
opts = fields[3] |
| 585 |
return fs, "ro" in opts.split(",") |
| 586 |
return None |
| 587 |
|
| 588 |
|
| 589 |
def transactional_updates(): |
| 590 |
return os.path.exists("/usr/sbin/transactional-update") and get_fs_type_and_readonly_state("/") == ("btrfs", True) |
| 591 |
|
| 592 |
|
| 593 |
# =========================================== |
| 594 |
# Main control flow |
| 595 |
|
| 596 |
|
| 597 |
def main(): |
| 598 |
module = AnsibleModule( |
| 599 |
argument_spec=dict( |
| 600 |
name=dict(required=True, aliases=["pkg"], type="list", elements="str"), |
| 601 |
state=dict( |
| 602 |
default="present", choices=["absent", "installed", "latest", "present", "removed", "dist-upgrade"] |
| 603 |
), |
| 604 |
type=dict( |
| 605 |
default="package", choices=["package", "patch", "pattern", "product", "srcpackage", "application"] |
| 606 |
), |
| 607 |
extra_args_precommand=dict(), |
| 608 |
disable_gpg_check=dict(default=False, type="bool"), |
| 609 |
auto_import_keys=dict(default=False, type="bool"), |
| 610 |
disable_recommends=dict(default=True, type="bool"), |
| 611 |
force=dict(default=False, type="bool"), |
| 612 |
force_resolution=dict(default=False, type="bool"), |
| 613 |
update_cache=dict(aliases=["refresh"], default=False, type="bool"), |
| 614 |
oldpackage=dict(default=False, type="bool"), |
| 615 |
extra_args=dict(), |
| 616 |
allow_vendor_change=dict(default=False, type="bool"), |
| 617 |
replacefiles=dict(default=False, type="bool"), |
| 618 |
clean_deps=dict(default=False, type="bool"), |
| 619 |
simple_errors=dict(default=False, type="bool"), |
| 620 |
quiet=dict(default=True, type="bool"), |
| 621 |
skip_post_errors=dict(default=False, type="bool"), |
| 622 |
), |
| 623 |
supports_check_mode=True, |
| 624 |
) |
| 625 |
|
| 626 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 627 |
|
| 628 |
name = module.params["name"] |
| 629 |
state = module.params["state"] |
| 630 |
update_cache = module.params["update_cache"] |
| 631 |
|
| 632 |
# remove empty strings from package list |
| 633 |
name = [_f for _f in name if _f] |
| 634 |
|
| 635 |
# Refresh repositories |
| 636 |
if update_cache and not module.check_mode: |
| 637 |
retvals = repo_refresh(module) |
| 638 |
|
| 639 |
if retvals["rc"] != 0: |
| 640 |
module.fail_json(msg="Zypper refresh run failed.", **retvals) |
| 641 |
|
| 642 |
# Perform requested action |
| 643 |
if name == ["*"] and state in ["latest", "dist-upgrade"]: |
| 644 |
packages_changed, retvals = package_update_all(module) |
| 645 |
elif name != ["*"] and state == "dist-upgrade": |
| 646 |
module.fail_json(msg="Can not dist-upgrade specific packages.") |
| 647 |
else: |
| 648 |
if state in ["absent", "removed"]: |
| 649 |
packages_changed, retvals = package_absent(module, name) |
| 650 |
elif state in ["installed", "present", "latest"]: |
| 651 |
packages_changed, retvals = package_present(module, name, state == "latest") |
| 652 |
|
| 653 |
retvals["changed"] = retvals["rc"] in [0, 102] and bool(packages_changed) |
| 654 |
|
| 655 |
if module._diff: |
| 656 |
set_diff(module, retvals, packages_changed) |
| 657 |
|
| 658 |
if retvals["rc"] not in [0, 102]: |
| 659 |
module.fail_json(msg="Zypper run failed.", **retvals) |
| 660 |
|
| 661 |
if not retvals["changed"]: |
| 662 |
del retvals["stdout"] |
| 663 |
del retvals["stderr"] |
| 664 |
|
| 665 |
module.exit_json(name=name, state=state, update_cache=update_cache, **retvals) |
| 666 |
|
| 667 |
|
| 668 |
if __name__ == "__main__": |
| 669 |
main() |