1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

yum_versionlock: Fix entry matching (#4183)

As an input the module receives names of packages to lock.
Those never matched existing entries and therefore always reported
changes.

For compatibility yum is symlinked to dnf on newer systems,
but versionlock entries defer. Try to parse both formats.

Signed-off-by: Florian Achleitner <flo@fopen.at>
This commit is contained in:
fachleitner 2022-02-18 21:07:09 +01:00 committed by GitHub
parent 44f9bf545d
commit f5ec73735f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "yum_versionlock - fix matching of existing entries with names passed to the module. Match yum and dnf lock format (https://github.com/ansible-collections/community.general/pull/4183)."

View file

@ -17,7 +17,7 @@ description:
options: options:
name: name:
description: description:
- Package name or a list of packages. - Package name or a list of package names with optional wildcards.
type: list type: list
required: true required: true
elements: str elements: str
@ -74,10 +74,17 @@ state:
sample: present sample: present
''' '''
import re
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from fnmatch import fnmatch from fnmatch import fnmatch
# on DNF-based distros, yum is a symlink to dnf, so we try to handle their different entry formats.
NEVRA_RE_YUM = re.compile(r'^(?P<exclude>!)?(?P<epoch>\d+):(?P<name>.+)-'
r'(?P<version>.+)-(?P<release>.+)\.(?P<arch>.+)$')
NEVRA_RE_DNF = re.compile(r"^(?P<exclude>!)?(?P<name>.+)-(?P<epoch>\d+):(?P<version>.+)-"
r"(?P<release>.+)\.(?P<arch>.+)$")
class YumVersionLock: class YumVersionLock:
def __init__(self, module): def __init__(self, module):
@ -102,6 +109,15 @@ class YumVersionLock:
self.module.fail_json(msg="Error: " + to_native(err) + to_native(out)) self.module.fail_json(msg="Error: " + to_native(err) + to_native(out))
def match(entry, name):
m = NEVRA_RE_YUM.match(entry)
if not m:
m = NEVRA_RE_DNF.match(entry)
if not m:
return False
return fnmatch(m.group("name"), name)
def main(): def main():
""" start main program to add/remove a package to yum versionlock""" """ start main program to add/remove a package to yum versionlock"""
module = AnsibleModule( module = AnsibleModule(
@ -123,20 +139,20 @@ def main():
# Ensure versionlock state of packages # Ensure versionlock state of packages
packages_list = [] packages_list = []
if state in ('present'): if state in ('present', ):
command = 'add' command = 'add'
for single_pkg in packages: for single_pkg in packages:
if not any(fnmatch(pkg.split(":", 1)[-1], single_pkg) for pkg in versionlock_packages.split()): if not any(match(pkg, single_pkg) for pkg in versionlock_packages.split()):
packages_list.append(single_pkg) packages_list.append(single_pkg)
if packages_list: if packages_list:
if module.check_mode: if module.check_mode:
changed = True changed = True
else: else:
changed = yum_v.ensure_state(packages_list, command) changed = yum_v.ensure_state(packages_list, command)
elif state in ('absent'): elif state in ('absent', ):
command = 'delete' command = 'delete'
for single_pkg in packages: for single_pkg in packages:
if any(fnmatch(pkg, single_pkg) for pkg in versionlock_packages.split()): if any(match(pkg, single_pkg) for pkg in versionlock_packages.split()):
packages_list.append(single_pkg) packages_list.append(single_pkg)
if packages_list: if packages_list:
if module.check_mode: if module.check_mode: