Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apt: better error msg when update_cache fails (#37410) #40144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/ansible/modules/packaging/os/apt.py
Expand Up @@ -955,14 +955,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()
Expand Down
23 changes: 23 additions & 0 deletions test/integration/targets/apt/tasks/apt.yml
Expand Up @@ -162,6 +162,29 @@
- "not apt_result.changed"
- "apt_result.failed"

# 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

Expand Down