From ba3345b52b529813464c5ae4949ef426b5039532 Mon Sep 17 00:00:00 2001 From: Masaki Furuta Date: Fri, 8 Mar 2019 06:48:18 -0800 Subject: [PATCH] Make "absent" runnable with specifying NVR in DNF module. (#53206) * Make "absent" runnable with specifying NVR in DNF module. * Fix indent and comment. * Adjust indent. * Fix removing multiple packages with NVR. * Replace nevra_strict with nevra , since it's not included in libdnf on Fedora 28. --- lib/ansible/modules/packaging/os/dnf.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index 44c14fbba86..9ef66cd0e6c 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -1102,8 +1102,21 @@ class DnfModule(YumDnf): installed = self.base.sack.query().installed() for pkg_spec in pkg_specs: - if ("*" in pkg_spec) or installed.filter(name=pkg_spec): - self.base.remove(pkg_spec) + installed_pkg = list(map(str, installed.filter(name=pkg_spec).run())) + if installed_pkg: + candidate_pkg = self._packagename_dict(installed_pkg[0]) + installed_pkg = installed.filter(name=candidate_pkg['name']).run() + else: + candidate_pkg = self._packagename_dict(pkg_spec) + installed_pkg = installed.filter(nevra=pkg_spec).run() + if installed_pkg: + installed_pkg = installed_pkg[0] + evr_cmp = self._compare_evr( + installed_pkg.epoch, installed_pkg.version, installed_pkg.release, + candidate_pkg['epoch'], candidate_pkg['version'], candidate_pkg['release'], + ) + if evr_cmp == 0: + self.base.remove(pkg_spec) # Like the dnf CLI we want to allow recursive removal of dependent # packages