diff --git a/changelogs/fragments/package_facts_apt_fix.yml b/changelogs/fragments/package_facts_apt_fix.yml new file mode 100644 index 0000000000..32eeb3395b --- /dev/null +++ b/changelogs/fragments/package_facts_apt_fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - For package_facts, correct information about apt being missing and fix missing attribute. diff --git a/lib/ansible/module_utils/facts/packages.py b/lib/ansible/module_utils/facts/packages.py index 39b65f7812..8a3cb198f0 100644 --- a/lib/ansible/module_utils/facts/packages.py +++ b/lib/ansible/module_utils/facts/packages.py @@ -18,6 +18,8 @@ def get_all_pkg_managers(): class PkgMgr(with_metaclass(ABCMeta, object)): + warnings = [] + @abstractmethod def is_available(self): # This method is supposed to return True/False if the package manager is currently installed/usable diff --git a/lib/ansible/modules/packaging/os/package_facts.py b/lib/ansible/modules/packaging/os/package_facts.py index 5175642bc1..9a331664d3 100644 --- a/lib/ansible/modules/packaging/os/package_facts.py +++ b/lib/ansible/modules/packaging/os/package_facts.py @@ -155,6 +155,7 @@ ansible_facts: from ansible.module_utils._text import to_native, to_text from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.process import get_bin_path from ansible.module_utils.facts.packages import LibMgr, CLIMgr, get_all_pkg_managers @@ -189,6 +190,16 @@ class APT(LibMgr): self._cache = self._lib.Cache() return self._cache + def is_available(self): + ''' we expect the python bindings installed, but if there is apt/apt-get give warning about missing bindings''' + we_have_lib = super(APT, self).is_available() + if not we_have_lib: + for exe in ('apt', 'apt-get'): + if get_bin_path(exe): + self.warnings.append('Found "%s" but python bindings are missing, so we cannot get package information.' % exe) + break + return we_have_lib + def list_installed(self): return [pk for pk in self.pkg_cache.keys() if self.pkg_cache[pk].is_installed] @@ -299,11 +310,15 @@ def main(): if manager.is_available(): found += 1 packages.update(manager.get_packages()) + except Exception as e: if pkgmgr in module.params['manager']: module.warn('Requested package manager %s was not usable by this module: %s' % (pkgmgr, to_text(e))) continue + for warning in getattr(manager, 'warnings', []): + module.warn(warning) + except Exception as e: if pkgmgr in module.params['manager']: module.warn('Failed to retrieve packages with %s: %s' % (pkgmgr, to_text(e)))