13

I have an ansible playbook running on many machines. In that playbook I have a few packages I am trying to install using apt, but occasionally they fail, either because other playbooks are running, a periodic update or any other apt instance running in parallel and grabbing the lock.

I basically want to add a retry loop before giving up but failed to do so as retries is not supported for apt, apparently: I looked into the apt module page in ansible's documentation, and even tried to actually use it even though it is not there (which obviously failed).

Anyway - I need an idea on how to get ansible to retry for let's say 3 times, with 30 seconds delay, but only on failures to install the package.

techraf
  • 64,883
  • 27
  • 193
  • 198
  • Adding data: I found a way to do the retries and delay on the apt (missed 2 spaces in the indentation), but am still missing the part of how to know that apt succeeded. I thought that checking the rc is 0 would be enough, it is not. Can I rely on the stderr to be empty? – אורן Oren שריד Sarid Nov 29 '17 at 11:17

1 Answers1

20

There are universal task results tests, so you can use:

- apt:
    name: build-essential
    state: present
  register: apt_res
  retries: 5
  until: apt_res is success

With Ansible 2.4 and earlier use the filter syntax - this was deprecated in Ansible 2.5 and will be removed from 2.9

until: apt_res | success
notapatch
  • 6,569
  • 6
  • 41
  • 45
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193