From 9ec9f18b135fa70d4189142014617d36341fc95e Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Mon, 21 Jan 2019 23:20:27 +0100 Subject: [PATCH] dnf: allow to operate on file paths (#51080) Fixes #50843 --- changelogs/fragments/50843.yaml | 2 ++ lib/ansible/modules/packaging/os/dnf.py | 14 ++++++++++++++ test/integration/targets/dnf/tasks/dnf.yml | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 changelogs/fragments/50843.yaml diff --git a/changelogs/fragments/50843.yaml b/changelogs/fragments/50843.yaml new file mode 100644 index 00000000000..a68b361bf3a --- /dev/null +++ b/changelogs/fragments/50843.yaml @@ -0,0 +1,2 @@ +bugfixes: + - dnf - allow to operate on file paths (https://github.com/ansible/ansible/issues/50843) diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index 67ca827c68d..a4bfacbb810 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -747,6 +747,13 @@ class DnfModule(YumDnf): "results": [] } + def _whatprovides(self, filepath): + available = self.base.sack.query().available() + pkg_spec = available.filter(provides=filepath).run() + + if pkg_spec: + return pkg_spec[0].name + def _parse_spec_group_file(self): pkg_specs, grp_specs, module_specs, filenames = [], [], [], [] already_loaded_comps = False # Only load this if necessary, it's slow @@ -758,6 +765,13 @@ class DnfModule(YumDnf): elif name.endswith(".rpm"): filenames.append(name) elif name.startswith("@") or ('/' in name): + # like "dnf install /usr/bin/vi" + if '/' in name: + pkg_spec = self._whatprovides(name) + if pkg_spec: + pkg_specs.append(pkg_spec) + continue + if not already_loaded_comps: self.base.read_comps() already_loaded_comps = True diff --git a/test/integration/targets/dnf/tasks/dnf.yml b/test/integration/targets/dnf/tasks/dnf.yml index 990e0f1106c..d410dbc7e2e 100644 --- a/test/integration/targets/dnf/tasks/dnf.yml +++ b/test/integration/targets/dnf/tasks/dnf.yml @@ -664,3 +664,24 @@ state: absent # end test case where disable_excludes is supported + +- name: Test "dnf install /usr/bin/vi" + block: + - name: Clean vim-minimal + dnf: + name: vim-minimal + state: absent + + - name: Install vim-minimal by specifying "/usr/bin/vi" + dnf: + name: /usr/bin/vi + state: present + + - name: Get rpm output + command: rpm -q vim-minimal + register: rpm_output + + - name: Check installation was successful + assert: + that: + - "'vim-minimal' in rpm_output.stdout"