Package facts apt fix (#55963)

* fixes for apt on package_facts

* reverse order, apt is newer

* moved warnings prop to base
This commit is contained in:
Brian Coca 2019-05-01 22:11:12 -04:00 committed by GitHub
parent edafa71f42
commit 24a46deef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- For package_facts, correct information about apt being missing and fix missing attribute.

View file

@ -18,6 +18,8 @@ def get_all_pkg_managers():
class PkgMgr(with_metaclass(ABCMeta, object)): class PkgMgr(with_metaclass(ABCMeta, object)):
warnings = []
@abstractmethod @abstractmethod
def is_available(self): def is_available(self):
# This method is supposed to return True/False if the package manager is currently installed/usable # This method is supposed to return True/False if the package manager is currently installed/usable

View file

@ -155,6 +155,7 @@ ansible_facts:
from ansible.module_utils._text import to_native, to_text from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.basic import AnsibleModule 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 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() self._cache = self._lib.Cache()
return self._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): def list_installed(self):
return [pk for pk in self.pkg_cache.keys() if self.pkg_cache[pk].is_installed] 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(): if manager.is_available():
found += 1 found += 1
packages.update(manager.get_packages()) packages.update(manager.get_packages())
except Exception as e: except Exception as e:
if pkgmgr in module.params['manager']: if pkgmgr in module.params['manager']:
module.warn('Requested package manager %s was not usable by this module: %s' % (pkgmgr, to_text(e))) module.warn('Requested package manager %s was not usable by this module: %s' % (pkgmgr, to_text(e)))
continue continue
for warning in getattr(manager, 'warnings', []):
module.warn(warning)
except Exception as e: except Exception as e:
if pkgmgr in module.params['manager']: if pkgmgr in module.params['manager']:
module.warn('Failed to retrieve packages with %s: %s' % (pkgmgr, to_text(e))) module.warn('Failed to retrieve packages with %s: %s' % (pkgmgr, to_text(e)))