Skip to content

apt - update the sources first if the cache can't be opened with default_release - #81743

Closed
s-hertel wants to merge 2 commits into
ansible:develfrom
s-hertel:retry-and-handle-apt-cache-config-error
Closed

s-hertel wants to merge 2 commits into
ansible:develfrom
s-hertel:retry-and-handle-apt-cache-config-error

Conversation

@s-hertel

@s-hertel s-hertel commented Sep 20, 2023 •

Copy link
Copy Markdown
Contributor
SUMMARY

Fixes #28991

Not sure how to test for this easily. At minimum we should handle the SystemError and indicate the user needs to update the cache from a previous operation.

here's a reproducer
FROM debian:bullseye

RUN apt-get update && apt-get install -y \
  python3 \
  python3-pip \
  git \
  apt-utils

RUN pip install git+https://github.com/ansible/ansible.git@stable-2.15 --user

RUN echo '---\n\
- hosts: localhost\n\
  gather_facts: no\n\
  tasks:\n\
  - name: Add bullseye-backports repository\n\
    apt_repository:\n\
      filename: "bullseye-backports"\n\
      repo: "deb http://deb.debian.org/debian bullseye-backports main"\n\
      state: present\n\
      update_cache: false\n\
  - name: Install from bullseye-backports\n\
    apt:\n\
      name: "golang"\n\
      default_release: "bullseye-backports"\n\
      state: present\n\
      update_cache: yes' > /tmp/playbook.yml

CMD /bin/bash -c "python3 -m ansible playbook /tmp/playbook.yml -vvv"

without the patch, results in

TASK [Install from bullseye-backports] *****************************************
task path: /tmp/playbook.yml:11
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660 `" && echo ansible-tmp-1713979441.7980669-380-92052647695660="` echo /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660 `" ) && sleep 0'
Using module file /root/.local/lib/python3.9/site-packages/ansible/modules/apt.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-6omabiozr/tmpl88k2hci TO /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/ /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible.modules.apt', init_globals=dict(_module_fqn='ansible.modules.apt', _modlib_path=modlib_path),
  File "/usr/lib/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_apt_payload_sgb6pdqp/ansible_apt_payload.zip/ansible/modules/apt.py", line 1512, in <module>
  File "/tmp/ansible_apt_payload_sgb6pdqp/ansible_apt_payload.zip/ansible/modules/apt.py", line 1342, in main
  File "/usr/lib/python3/dist-packages/apt/cache.py", line 214, in open
    self._cache = apt_pkg.Cache(progress)
apt_pkg.Error: E:The value 'bullseye-backports' is invalid for APT::Default-Release as such a release is not available in the sources
fatal: [localhost]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1713979441.7980669-380-92052647695660/AnsiballZ_apt.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible.modules.apt', init_globals=dict(_module_fqn='ansible.modules.apt', _modlib_path=modlib_path),\n  File \"/usr/lib/python3.9/runpy.py\", line 210, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.9/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.9/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_apt_payload_sgb6pdqp/ansible_apt_payload.zip/ansible/modules/apt.py\", line 1512, in <module>\n  File \"/tmp/ansible_apt_payload_sgb6pdqp/ansible_apt_payload.zip/ansible/modules/apt.py\", line 1342, in main\n  File \"/usr/lib/python3/dist-packages/apt/cache.py\", line 214, in open\n    self._cache = apt_pkg.Cache(progress)\napt_pkg.Error: E:The value 'bullseye-backports' is invalid for APT::Default-Release as such a release is not available in the sources\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}
ISSUE TYPE
  • Bugfix Pull Request

@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. module This issue/PR relates to a module. has_issue labels Sep 20, 2023
Comment thread lib/ansible/modules/apt.py Resolved
Comment thread lib/ansible/modules/apt.py Resolved
Comment thread lib/ansible/modules/apt.py Resolved
@mattclay mattclay removed the needs_triage Needs a first human triage before being processed. label Sep 21, 2023
Comment on lines +1543 to +1561
except SystemError as e:
module.fail_json(msg=to_native(e))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't want to add the complexity, this could be simplified by removing the rest of the patch and adding a note to the documentation that the default_release is configured before updating the cache.

If someone's hitting this, they're either adding a repository without apt_repository and not updating the cache, or going out of their way to disable update_cache with apt_repository.

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Oct 6, 2023
@s-hertel
s-hertel force-pushed the retry-and-handle-apt-cache-config-error branch from 2c78e90 to c7b3965 Compare April 24, 2024 17:06
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 24, 2024
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 8, 2024
@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Apr 29, 2025
@s-hertel s-hertel closed this Jan 14, 2026
@ansible ansible locked and limited conversation to collaborators Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug This issue/PR relates to a bug. has_issue module This issue/PR relates to a module. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_pr This PR has not been pushed to for more than one year.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apt module does not update cache before checking release exists

3 participants