From a55fd1e41564223c9aea2e59d930d89dd3e4e94d Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Tue, 15 May 2018 16:43:48 +0100 Subject: [PATCH] apt: better error msg when update_cache fails (#37410) (#40143) (cherry picked from commit a6d31b6fb13af1f3697c4517b088592bb850787a) --- lib/ansible/modules/packaging/os/apt.py | 7 ++++--- test/integration/targets/apt/tasks/apt.yml | 23 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py index ea74653ac6f..dbf8b09d077 100644 --- a/lib/ansible/modules/packaging/os/apt.py +++ b/lib/ansible/modules/packaging/os/apt.py @@ -979,14 +979,15 @@ def main(): tdelta = datetime.timedelta(seconds=p['cache_valid_time']) if not mtimestamp + tdelta >= now: # Retry to update the cache up to 3 times + err = '' for retry in range(3): try: cache.update() break - except apt.cache.FetchFailedException: - pass + except apt.cache.FetchFailedException as e: + err = to_native(e) else: - module.fail_json(msg='Failed to update apt cache.') + module.fail_json(msg='Failed to update apt cache: %s' % err) cache.open(progress=None) updated_cache = True mtimestamp, updated_cache_time = get_updated_cache_time() diff --git a/test/integration/targets/apt/tasks/apt.yml b/test/integration/targets/apt/tasks/apt.yml index c55a66ec00d..43e498b0bc7 100644 --- a/test/integration/targets/apt/tasks/apt.yml +++ b/test/integration/targets/apt/tasks/apt.yml @@ -197,6 +197,29 @@ - libcaca-dev - libslang2-dev +# https://github.com/ansible/ansible/issues/23155 +- name: create a repo file + copy: + dest: /etc/apt/sources.list.d/non-existing.list + content: deb http://ppa.launchpad.net/non-existing trusty main + +- name: test for sane error message + apt: + update_cache: yes + register: apt_result + ignore_errors: yes + +- name: verify sane error message + assert: + that: + - "'E:Failed to fetch' in apt_result['msg']" + - "'403' in apt_result['msg']" + +- name: Clean up + file: + name: /etc/apt/sources.list.d/non-existing.list + state: absent + - name: autoclean during install apt: pkg=hello state=present autoclean=yes