diff options
-rw-r--r-- | changelogs/fragments/apt_cache.yml | 3 | ||||
-rw-r--r-- | lib/ansible/modules/apt.py | 15 | ||||
-rw-r--r-- | lib/ansible/modules/apt_repository.py | 19 | ||||
-rw-r--r-- | test/integration/targets/apt/tasks/apt.yml | 2 |
4 files changed, 30 insertions, 9 deletions
diff --git a/changelogs/fragments/apt_cache.yml b/changelogs/fragments/apt_cache.yml new file mode 100644 index 0000000000..d407431e03 --- /dev/null +++ b/changelogs/fragments/apt_cache.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - apt_* - add more info messages raised while updating apt cache (https://github.com/ansible/ansible/issues/77941). diff --git a/lib/ansible/modules/apt.py b/lib/ansible/modules/apt.py index cc5edd0f6d..858711e3a8 100644 --- a/lib/ansible/modules/apt.py +++ b/lib/ansible/modules/apt.py @@ -1394,16 +1394,25 @@ def main(): if not module.check_mode: cache.update() break - except apt.cache.FetchFailedException as e: - err = to_native(e) + except apt.cache.FetchFailedException as fetch_failed_exc: + err = fetch_failed_exc + module.warn( + f"Failed to update cache after {retry + 1} retries due " + f"to {to_native(fetch_failed_exc)}, retrying" + ) # Use exponential backoff plus a little bit of randomness delay = 2 ** retry + randomize if delay > update_cache_retry_max_delay: delay = update_cache_retry_max_delay + randomize time.sleep(delay) + module.warn(f"Sleeping for {int(round(delay))} seconds, before attempting to refresh the cache again") else: - module.fail_json(msg='Failed to update apt cache: %s' % (err if err else 'unknown reason')) + msg = ( + f"Failed to update apt cache after {update_cache_retries} retries: " + f"{err if err else 'unknown reason'}" + ) + module.fail_json(msg=msg) cache.open(progress=None) mtimestamp, post_cache_update_time = get_updated_cache_time() diff --git a/lib/ansible/modules/apt_repository.py b/lib/ansible/modules/apt_repository.py index 4d01679db7..aa50c54c17 100644 --- a/lib/ansible/modules/apt_repository.py +++ b/lib/ansible/modules/apt_repository.py @@ -738,29 +738,38 @@ def main(): if changed and not module.check_mode: try: + err = '' sourceslist.save() if update_cache: - err = '' update_cache_retries = module.params.get('update_cache_retries') update_cache_retry_max_delay = module.params.get('update_cache_retry_max_delay') randomize = random.randint(0, 1000) / 1000.0 + cache = apt.Cache() for retry in range(update_cache_retries): try: - cache = apt.Cache() cache.update() break - except apt.cache.FetchFailedException as e: - err = to_native(e) + except apt.cache.FetchFailedException as fetch_failed_exc: + err = fetch_failed_exc + module.warn( + f"Failed to update cache after {retry + 1} due " + f"to {to_native(fetch_failed_exc)} retry, retrying" + ) # Use exponential backoff with a max fail count, plus a little bit of randomness delay = 2 ** retry + randomize if delay > update_cache_retry_max_delay: delay = update_cache_retry_max_delay + randomize time.sleep(delay) + module.warn(f"Sleeping for {int(round(delay))} seconds, before attempting to update the cache again") else: revert_sources_list(sources_before, sources_after, sourceslist_before) - module.fail_json(msg='Failed to update apt cache: %s' % (err if err else 'unknown reason')) + msg = ( + f"Failed to update apt cache after {update_cache_retries} retries: " + f"{err if err else 'unknown reason'}" + ) + module.fail_json(msg=msg) except (OSError, IOError) as ex: revert_sources_list(sources_before, sources_after, sourceslist_before) diff --git a/test/integration/targets/apt/tasks/apt.yml b/test/integration/targets/apt/tasks/apt.yml index dba6b265eb..64e00d3ca9 100644 --- a/test/integration/targets/apt/tasks/apt.yml +++ b/test/integration/targets/apt/tasks/apt.yml @@ -328,7 +328,7 @@ - name: verify logical error message assert: that: - - "'Failed to fetch' in apt_result['msg']" + - "'Failed to update apt cache' in apt_result['msg']" - "'403' in apt_result['msg']" - name: Clean up |