| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, Florian Paul Azim Hoberg (@gyptazy) <[email protected]> |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: yum_versionlock |
| 11 |
version_added: 2.0.0 |
| 12 |
short_description: Locks / unlocks a installed package(s) from being updated by yum package manager |
| 13 |
description: |
| 14 |
- This module adds installed packages to yum versionlock to prevent the package(s) from being updated. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Package name or a list of package names with optional version or wildcards. |
| 26 |
- Specifying versions is supported since community.general 7.2.0. |
| 27 |
type: list |
| 28 |
required: true |
| 29 |
elements: str |
| 30 |
state: |
| 31 |
description: |
| 32 |
- If state is V(present), package(s) is/are added to yum versionlock list. |
| 33 |
- If state is V(absent), package(s) is/are removed from yum versionlock list. |
| 34 |
choices: ['absent', 'present'] |
| 35 |
type: str |
| 36 |
default: present |
| 37 |
notes: |
| 38 |
- Requires yum-plugin-versionlock package on the remote node. |
| 39 |
requirements: |
| 40 |
- yum |
| 41 |
- yum-versionlock |
| 42 |
author: |
| 43 |
- Florian Paul Azim Hoberg (@gyptazy) |
| 44 |
- Amin Vakil (@aminvakil) |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
- name: Prevent Apache / httpd from being updated |
| 49 |
community.general.yum_versionlock: |
| 50 |
state: present |
| 51 |
name: |
| 52 |
- httpd |
| 53 |
|
| 54 |
- name: Prevent Apache / httpd version 2.4.57-2 from being updated |
| 55 |
community.general.yum_versionlock: |
| 56 |
state: present |
| 57 |
name: |
| 58 |
- httpd-0:2.4.57-2.el9 |
| 59 |
|
| 60 |
- name: Prevent multiple packages from being updated |
| 61 |
community.general.yum_versionlock: |
| 62 |
state: present |
| 63 |
name: |
| 64 |
- httpd |
| 65 |
- nginx |
| 66 |
- haproxy |
| 67 |
- curl |
| 68 |
|
| 69 |
- name: Remove lock from Apache / httpd to be updated again |
| 70 |
community.general.yum_versionlock: |
| 71 |
state: absent |
| 72 |
name: httpd |
| 73 |
""" |
| 74 |
|
| 75 |
RETURN = r""" |
| 76 |
packages: |
| 77 |
description: A list of package(s) in versionlock list. |
| 78 |
returned: success |
| 79 |
type: list |
| 80 |
elements: str |
| 81 |
sample: ["httpd"] |
| 82 |
state: |
| 83 |
description: State of package(s). |
| 84 |
returned: success |
| 85 |
type: str |
| 86 |
sample: present |
| 87 |
""" |
| 88 |
|
| 89 |
import re |
| 90 |
from fnmatch import fnmatch |
| 91 |
|
| 92 |
from ansible.module_utils.basic import AnsibleModule |
| 93 |
|
| 94 |
# on DNF-based distros, yum is a symlink to dnf, so we try to handle their different entry formats. |
| 95 |
NEVRA_RE_YUM = re.compile( |
| 96 |
r"^(?P<exclude>!)?(?P<epoch>\d+):(?P<name>.+)-" |
| 97 |
r"(?P<version>.+)-(?P<release>.+)\.(?P<arch>.+)$" |
| 98 |
) |
| 99 |
NEVRA_RE_DNF = re.compile( |
| 100 |
r"^(?P<exclude>!)?(?P<name>.+)-(?P<epoch>\d+):(?P<version>.+)-" |
| 101 |
r"(?P<release>.+)\.(?P<arch>.+)$" |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
class YumVersionLock: |
| 106 |
def __init__(self, module): |
| 107 |
self.module = module |
| 108 |
self.params = module.params |
| 109 |
self.yum_bin = module.get_bin_path("yum", required=True) |
| 110 |
|
| 111 |
def get_versionlock_packages(self): |
| 112 |
"""Get an overview of all packages on yum versionlock""" |
| 113 |
rc, out, err = self.module.run_command([self.yum_bin, "versionlock", "list"]) |
| 114 |
if rc == 0: |
| 115 |
return out |
| 116 |
elif rc == 1 and "o such command:" in err: |
| 117 |
self.module.fail_json(msg=f"Error: Please install rpm package yum-plugin-versionlock : {err}{out}") |
| 118 |
self.module.fail_json(msg=f"Error: {err}{out}") |
| 119 |
|
| 120 |
def ensure_state(self, packages, command): |
| 121 |
"""Ensure packages state""" |
| 122 |
rc, out, err = self.module.run_command([self.yum_bin, "-q", "versionlock", command] + packages) |
| 123 |
# If no package can be found this will be written on stdout with rc 0 |
| 124 |
if "No package found for" in out: |
| 125 |
self.module.fail_json(msg=out) |
| 126 |
if rc == 0: |
| 127 |
return True |
| 128 |
self.module.fail_json(msg=f"Error: {err}{out}") |
| 129 |
|
| 130 |
|
| 131 |
def match(entry, name): |
| 132 |
match = False |
| 133 |
m = NEVRA_RE_YUM.match(entry) |
| 134 |
if not m: |
| 135 |
m = NEVRA_RE_DNF.match(entry) |
| 136 |
if not m: |
| 137 |
return False |
| 138 |
if fnmatch(m.group("name"), name): |
| 139 |
match = True |
| 140 |
if entry.rstrip(".*") == name: |
| 141 |
match = True |
| 142 |
return match |
| 143 |
|
| 144 |
|
| 145 |
def main(): |
| 146 |
"""start main program to add/delete a package to yum versionlock""" |
| 147 |
module = AnsibleModule( |
| 148 |
argument_spec=dict( |
| 149 |
state=dict(default="present", choices=["present", "absent"]), |
| 150 |
name=dict(required=True, type="list", elements="str"), |
| 151 |
), |
| 152 |
supports_check_mode=True, |
| 153 |
) |
| 154 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 155 |
|
| 156 |
state = module.params["state"] |
| 157 |
packages = module.params["name"] |
| 158 |
changed = False |
| 159 |
|
| 160 |
yum_v = YumVersionLock(module) |
| 161 |
|
| 162 |
# Get an overview of all packages that have a version lock |
| 163 |
versionlock_packages = yum_v.get_versionlock_packages() |
| 164 |
|
| 165 |
# Ensure versionlock state of packages |
| 166 |
packages_list = [] |
| 167 |
if state in ("present",): |
| 168 |
command = "add" |
| 169 |
for single_pkg in packages: |
| 170 |
if not any(match(pkg, single_pkg) for pkg in versionlock_packages.split()): |
| 171 |
packages_list.append(single_pkg) |
| 172 |
if packages_list: |
| 173 |
if module.check_mode: |
| 174 |
changed = True |
| 175 |
else: |
| 176 |
changed = yum_v.ensure_state(packages_list, command) |
| 177 |
elif state in ("absent",): |
| 178 |
command = "delete" |
| 179 |
for single_pkg in packages: |
| 180 |
if any(match(pkg, single_pkg) for pkg in versionlock_packages.split()): |
| 181 |
packages_list.append(single_pkg) |
| 182 |
if packages_list: |
| 183 |
if module.check_mode: |
| 184 |
changed = True |
| 185 |
else: |
| 186 |
changed = yum_v.ensure_state(packages_list, command) |
| 187 |
|
| 188 |
module.exit_json(changed=changed, meta={"packages": packages, "state": state}) |
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__": |
| 192 |
main() |