From 826b99d4bda336d24626bce9daa3a9aba9115eb3 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Tue, 14 May 2019 23:34:11 +0200 Subject: [PATCH] dnf: fix wildcard matching for state: absent (#56013) * dnf: fix wildcard matching for state: absent Fixes #55938 * Add changelog... * Fix sanity check failure... --- ...nf-fix-wildcard-matching-state-absent.yaml | 2 ++ lib/ansible/modules/packaging/os/dnf.py | 8 ++++++++ test/integration/targets/dnf/tasks/repo.yml | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 changelogs/fragments/55938-dnf-fix-wildcard-matching-state-absent.yaml diff --git a/changelogs/fragments/55938-dnf-fix-wildcard-matching-state-absent.yaml b/changelogs/fragments/55938-dnf-fix-wildcard-matching-state-absent.yaml new file mode 100644 index 0000000000..8f6fa1c61a --- /dev/null +++ b/changelogs/fragments/55938-dnf-fix-wildcard-matching-state-absent.yaml @@ -0,0 +1,2 @@ +bugfixes: + - "dnf - fix wildcard matching for state: absent (https://github.com/ansible/ansible/issues/55938)" diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index 7a395675a6..25454ddd55 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -1108,6 +1108,14 @@ class DnfModule(YumDnf): installed = self.base.sack.query().installed() for pkg_spec in pkg_specs: + # short-circuit installed check for wildcard matching + if '*' in pkg_spec: + try: + self.base.remove(pkg_spec) + except dnf.exceptions.MarkingError as e: + failure_response['failures'].append('{0} - {1}'.format(pkg_spec, to_native(e))) + continue + installed_pkg = list(map(str, installed.filter(name=pkg_spec).run())) if installed_pkg: candidate_pkg = self._packagename_dict(installed_pkg[0]) diff --git a/test/integration/targets/dnf/tasks/repo.yml b/test/integration/targets/dnf/tasks/repo.yml index 89aa87462a..978d4817d7 100644 --- a/test/integration/targets/dnf/tasks/repo.yml +++ b/test/integration/targets/dnf/tasks/repo.yml @@ -256,6 +256,26 @@ that: - rpm_main_result.rc == 0 - rpm_weak_result.rc == 1 # the weak dependency shouldn't be installed + + # https://github.com/ansible/ansible/issues/55938 + - name: Install foo-* + dnf: + name: foo-* + state: present + + - name: Uninstall foo-* + dnf: + name: foo-* + state: absent + + - name: Check if all foo packages are removed + shell: rpm -qa foo-* | wc -l + register: rpm_result + + - name: Verify rpm result + assert: + that: + - rpm_result.stdout == '0' always: - name: Clean up dnf: