From 37361d9b0b7faabade909003ae30aa6da87de3a4 Mon Sep 17 00:00:00 2001 From: Alexey Sheplyakov Date: Fri, 10 Aug 2018 23:16:15 +0400 Subject: [PATCH] pkg_mgr: fixed apt_rpm detection (#43769) Instead of checking the distribution name (which apparently is tricky to find out) check if /usr/bin/apt-get is managed by RPM. Fixes #43539 --- .../module_utils/facts/system/pkg_mgr.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/ansible/module_utils/facts/system/pkg_mgr.py b/lib/ansible/module_utils/facts/system/pkg_mgr.py index 6ea67880c1..ec3a13de30 100644 --- a/lib/ansible/module_utils/facts/system/pkg_mgr.py +++ b/lib/ansible/module_utils/facts/system/pkg_mgr.py @@ -19,6 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os +import subprocess from ansible.module_utils.facts.collector import BaseFactCollector @@ -89,6 +90,23 @@ class PkgMgrFactCollector(BaseFactCollector): pkg_mgr_name = 'dnf' return pkg_mgr_name + def _check_apt_flavor(self, pkg_mgr_name): + # Check if '/usr/bin/apt' is APT-RPM or an ordinary (dpkg-based) APT. + # There's rpm package on Debian, so checking if /usr/bin/rpm exists + # is not enough. Instead ask RPM if /usr/bin/apt-get belongs to some + # RPM package. + rpm_query = '/usr/bin/rpm -q --whatprovides /usr/bin/apt-get'.split() + if os.path.exists('/usr/bin/rpm'): + with open(os.devnull, 'w') as null: + try: + subprocess.check_call(rpm_query, stdout=null, stderr=null) + pkg_mgr_name = 'apt_rpm' + except subprocess.CalledProcessError: + # No apt-get in RPM database. Looks like Debian/Ubuntu + # with rpm package installed + pkg_mgr_name = 'apt' + return pkg_mgr_name + def collect(self, module=None, collected_facts=None): facts_dict = {} collected_facts = collected_facts or {} @@ -108,6 +126,10 @@ class PkgMgrFactCollector(BaseFactCollector): if pkg_mgr_name == 'apt': pkg_mgr_name = 'apt_rpm' + # Check if /usr/bin/apt-get is ordinary (dpkg-based) APT or APT-RPM + if pkg_mgr_name == 'apt': + pkg_mgr_name = self._check_apt_flavor(pkg_mgr_name) + # pacman has become available by distros other than those that are Arch # based by virtue of a dependency to the systemd mkosi project, this # handles some of those scenarios as they are reported/requested