From 60dd2f1ffff08dae2d8f42804611a088622a898b Mon Sep 17 00:00:00 2001 From: jctanner Date: Tue, 1 Nov 2016 19:30:50 -0400 Subject: [PATCH] apt: update cache until corrupt package lists are fixed (#5448) * apt: If the cache object fails to lost due to a corrupt file, try to update the cache until it is fixed. * Append -q to the update parameters * Remove unused variable * Use a string that doesn't rely on internationalization * Use py24 exception style * Use get_exception Fixes #2951 --- lib/ansible/modules/packaging/os/apt.py | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py index 3983b6bd854..be8e1016a62 100644 --- a/lib/ansible/modules/packaging/os/apt.py +++ b/lib/ansible/modules/packaging/os/apt.py @@ -755,6 +755,31 @@ def get_updated_cache_time(): return mtimestamp, updated_cache_time +# https://github.com/ansible/ansible-modules-core/issues/2951 +def get_cache(module): + '''Attempt to get the cache object and update till it works''' + cache = None + try: + cache = apt.Cache() + except SystemError: + e = get_exception() + if '/var/lib/apt/lists/' in str(e).lower(): + # update cache until files are fixed or retries exceeded + retries = 0 + while retries < 2: + (rc, so, se) = module.run_command(['apt-get', 'update', '-q']) + retries += 1 + if rc == 0: + break + if rc != 0: + module.fail_json(msg='Updating the cache to correct corrupt package lists failed:\n%s\n%s' % (str(e), str(so) + str(se))) + # try again + cache = apt.Cache() + else: + module.fail_json(msg=str(e)) + return cache + + def main(): module = AnsibleModule( argument_spec = dict( @@ -821,8 +846,10 @@ def main(): if p['state'] == 'removed': p['state'] = 'absent' + # Get the cache object + cache = get_cache(module) + try: - cache = apt.Cache() if p['default_release']: try: apt_pkg.config['APT::Default-Release'] = p['default_release']