diff options
author | Sloane Hertel <19572925+s-hertel@users.noreply.github.com> | 2021-08-06 16:01:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-06 13:01:55 -0700 |
commit | f0b227009c58e30f4b72aa8697ecfc62ac9c55cc (patch) | |
tree | 564350724aa79c17503e5e016557700c6768a894 /lib/ansible/modules | |
parent | de1129c8e5e58ccc6373e35ea348c2dfc3b49a27 (diff) | |
download | ansible-f0b227009c58e30f4b72aa8697ecfc62ac9c55cc.tar.gz |
Fix dnf package matching (#75411) (#75418)
* Fix a bug with the dnf module not using all components of a package name when filtering to determine if it's installed
* changelog
* Simplify splitting on the last '.'
* Update lib/ansible/modules/dnf.py
(cherry picked from commit b541a148d51e75b298e71161c022b12cd8ebba7c)
Diffstat (limited to 'lib/ansible/modules')
-rw-r--r-- | lib/ansible/modules/dnf.py | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/lib/ansible/modules/dnf.py b/lib/ansible/modules/dnf.py index b3a73fc1a4..da88ade15c 100644 --- a/lib/ansible/modules/dnf.py +++ b/lib/ansible/modules/dnf.py @@ -422,15 +422,7 @@ class DnfModule(YumDnf): return result - def _packagename_dict(self, packagename): - """ - Return a dictionary of information for a package name string or None - if the package name doesn't contain at least all NVR elements - """ - - if packagename[-4:] == '.rpm': - packagename = packagename[:-4] - + def _split_package_arch(self, packagename): # This list was auto generated on a Fedora 28 system with the following one-liner # printf '[ '; for arch in $(ls /usr/lib/rpm/platform); do printf '"%s", ' ${arch%-linux}; done; printf ']\n' redhat_rpm_arches = [ @@ -445,15 +437,26 @@ class DnfModule(YumDnf): "sparc", "sparcv8", "sparcv9", "sparcv9v", "x86_64" ] - rpm_arch_re = re.compile(r'(.*)\.(.*)') + name, delimiter, arch = packagename.rpartition('.') + if name and arch and arch in redhat_rpm_arches: + return name, arch + return packagename, None + + def _packagename_dict(self, packagename): + """ + Return a dictionary of information for a package name string or None + if the package name doesn't contain at least all NVR elements + """ + + if packagename[-4:] == '.rpm': + packagename = packagename[:-4] + rpm_nevr_re = re.compile(r'(\S+)-(?:(\d*):)?(.*)-(~?\w+[\w.+]*)') try: arch = None - rpm_arch_match = rpm_arch_re.match(packagename) - if rpm_arch_match: - nevr, arch = rpm_arch_match.groups() - if arch in redhat_rpm_arches: - packagename = nevr + nevr, arch = self._split_package_arch(packagename) + if arch: + packagename = nevr rpm_nevr_match = rpm_nevr_re.match(packagename) if rpm_nevr_match: name, epoch, version, release = rpm_nevr_re.match(packagename).groups() @@ -708,7 +711,20 @@ class DnfModule(YumDnf): def _is_installed(self, pkg): installed = self.base.sack.query().installed() - if installed.filter(name=pkg): + + package_spec = {} + name, arch = self._split_package_arch(pkg) + if arch: + package_spec['arch'] = arch + + package_details = self._packagename_dict(pkg) + if package_details: + package_details['epoch'] = int(package_details['epoch']) + package_spec.update(package_details) + else: + package_spec['name'] = name + + if installed.filter(**package_spec): return True else: return False |