Skip to main content

Ansible APT module randomly fails

While using Ansible to automate the system updates on my personal servers, I ran into an issue where the apt module randomly failed with the error:

FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not get lock /var/lib/apt/lists/lock."}

Apparently it has something to do with the Unattended Upgrades service. Since this is something I want to keep enabled, the workaround is to set some retries on the task that uses the apt module. Here is an example:

- name: Update the package cache
  apt:
    update_cache: yes
  register: update_cache
  retries: 3
  delay: 10
  until: update_cache is succeeded

Where:

  • retries is the number of retries to attempt before failing the task.
  • delay is the number of seconds to wait between retries.
  • until is the condition to check if the task succeeded.

Then you will see this message in the output:

TASK [Update the package cache] ***********************
FAILED - RETRYING: Update the package cache (3 retries left).
ok: [server1]

This will make your playbook run a little bit slower, but it will be more reliable.

Hope this helps!

See you on the next one.