sorin
sorin

Reputation: 170598

How do I avoid ansible deployment failures due to dpkg lock file?

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?

Upvotes: 20

Views: 9122

Answers (3)

George Shuklin
George Shuklin

Reputation: 7907

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

Upvotes: 20

Gert van den Berg
Gert van den Berg

Reputation: 2776

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

Upvotes: 0

Gert van den Berg
Gert van den Berg

Reputation: 2776

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.

Upvotes: 0

Related Questions