From 66b1adfc83dd7e76fb106e637b6b17e796cd7db1 Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Wed, 25 Jul 2018 18:20:07 -0500 Subject: [PATCH] fix fedora version dnf fact, default pkg_mgr detection per distro family (#43261) * fix fedora version dnf fact, default pkg_mgr detection per distro family * loop over possible dnf/yum paths in case there are multiple canonical sources later in life Signed-off-by: Adam Miller --- .../module_utils/facts/system/pkg_mgr.py | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/ansible/module_utils/facts/system/pkg_mgr.py b/lib/ansible/module_utils/facts/system/pkg_mgr.py index dd277cf8934..6ea67880c13 100644 --- a/lib/ansible/module_utils/facts/system/pkg_mgr.py +++ b/lib/ansible/module_utils/facts/system/pkg_mgr.py @@ -70,13 +70,19 @@ class PkgMgrFactCollector(BaseFactCollector): _platform = 'Generic' required_facts = set(['distribution']) - def _check_rh_versions(self, collected_facts): + def _check_rh_versions(self, pkg_mgr_name, collected_facts): if collected_facts['ansible_distribution'] == 'Fedora': try: - if int(collected_facts['ansible_distribution_major_version']) < 15: - pkg_mgr_name = 'yum' + if int(collected_facts['ansible_distribution_major_version']) < 23: + for yum in [pkg_mgr for pkg_mgr in PKG_MGRS if pkg_mgr['name'] == 'yum']: + if os.path.exists(yum['path']): + pkg_mgr_name = 'yum' + break else: - pkg_mgr_name = 'dnf' + for dnf in [pkg_mgr for pkg_mgr in PKG_MGRS if pkg_mgr['name'] == 'dnf']: + if os.path.exists(dnf['path']): + pkg_mgr_name = 'dnf' + break except ValueError: # If there's some new magical Fedora version in the future, # just default to dnf @@ -92,14 +98,14 @@ class PkgMgrFactCollector(BaseFactCollector): if os.path.exists(pkg['path']): pkg_mgr_name = pkg['name'] - # apt is easily installable and supported by distros other than those - # that are debian based, this handles some of those scenarios as they - # are reported/requested - if pkg_mgr_name == 'apt' and collected_facts['ansible_os_family'] in ["RedHat", "Altlinux"]: - if collected_facts['ansible_os_family'] == 'RedHat': - pkg_mgr_name = self._check_rh_versions(collected_facts) - - elif collected_facts['ansible_os_family'] == 'Altlinux': + # Handle distro family defaults when more than one package manager is + # installed, the ansible_fact entry should be the default package + # manager provided by the distro. + if collected_facts['ansible_os_family'] == "RedHat": + if pkg_mgr_name not in ('yum', 'dnf'): + pkg_mgr_name = self._check_rh_versions(pkg_mgr_name, collected_facts) + elif collected_facts['ansible_os_family'] == 'Altlinux': + if pkg_mgr_name == 'apt': pkg_mgr_name = 'apt_rpm' # pacman has become available by distros other than those that are Arch