20

It seems that getting failures due to /var/lib/dpkg/lock is something not very rare. Based on observations these are caused most of the time 9/10 due to state lock file or while a cron job was running.

This means that a retry mechanism combined with a removal of stale file could be the solution.

How can I do this in ansible?

2
  • I dont think you can do loops in ansible. You could try the wait_for module to wait for the lock file to disappear and then run your apt. Commented Apr 15, 2016 at 23:26
  • 2
    Please have a look at this "Wait for automatic system updates" example. @user2599522, please see this relevant issue using wait_for that way.
    – Paolo
    Commented Jan 10, 2018 at 21:54

3 Answers 3

20

I'd try to solve this with until feature of ansible (http://docs.ansible.com/ansible/latest/playbooks_loops.html#do-until-loops)

    - name: Apt for sure
      apt: name=foobar state=present
      register: apt_status
      # 2018 syntax:
      # until: apt_status|success
      # 2020 syntax:
      until: apt_status is success
      delay: 6
      retries: 10
5
  • doesn't work: FAILED! => {"msg": "The conditional check 'apt_status|success' failed. The error was: template error while templating string: no filter named 'success'. String: {% if apt_status|success %} True {% else %} False {% endif %}"} Commented May 11, 2020 at 20:40
  • 3
    It was Ansible 2018, and now it's 2020. apt_status is success. Commented May 13, 2020 at 8:24
  • Seems to no longer work with 2024 ansible: {"msg": "The conditional check 'apt_status is success' failed. The error was: The 'failed' test expects a dictionary"}
    – silverwind
    Commented Mar 6 at 18:40
  • There is no 2024 Ansible. In ansible-core 2.16 it is working. You may have other problems with inventory, but simple is success is still working in case of apt module failing. Commented Mar 9 at 11:35
  • Also getting {"msg": "The conditional check 'apt_status is success' failed. The error was: The 'failed' test expects a dictionary"}, inventory is fine, if I remove the apt task all my playbook runs fine.
    – ascub
    Commented Apr 4 at 14:31
0

If using a module without the lock_timeout setting (like ansible.builtin.package you can wait for the lock before the apt step:

- name: Wait for DPKG lock on Debian and friends
  ansible.builtin.command: /usr/bin/lckdo -W 300 /var/lib/dpkg/lock true
  when:
    - ansible_os_family == "Debian"

- name: Apt for sure
  ansible.builtin.package:
    name: foobar
    state: present

That uses lckdo from the moreutils package to wait up to 5 minutes to acquire a lock on the file (which it then release after running true), to ensure that whatever has the lock currently has time to complete.

1
  • flock is more commonly available, but uses a different type of lock, so it does not work for this use - it gets the lock even if dpkg is running... Commented Aug 2 at 8:25
0

You can increase the time that the apt module waits for a lock:

- name: Apt for sure
  ansible.builtin.apt:
    name: foobar
    state: present
    lock_timeout: 600

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.