Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to retry playbooks from where they failed?

Is there any way to retry playbooks from where they failed?

I'm starting it with

vagrant provision
like image 331
Javier Manzano Avatar asked May 27 '15 11:05

Javier Manzano


People also ask

What happens when one playbook fails a task?

Handlers and failure If a task notifies a handler but another task fails later in the play, by default the handler does not run on that host, which may leave the host in an unexpected state. For example, a task could update a configuration file and notify a handler to restart some service.

What is .retry file in Ansible?

The retry files save path is where Ansible will save .retry files when a playbook fails and retry_files_enabled is True (the default). The default location is ~/ and can be changed to any writeable path: retry_files_save_path = ~/.ansible-retry. It appears that in 1.9. x the default location is ~/ but in 2.0.

What is a playbook Ansible?

Ansible Playbooks are lists of tasks that automatically execute against hosts. Groups of hosts form your Ansible inventory. Each module within an Ansible Playbook performs a specific task. Each module contains metadata that determines when and where a task is executed, as well as which user executes it.


1 Answers

I'm not too sure why you'd want to do this as an Ansible playbook should be idempotent and so re-running the whole thing from the start should be completely fine.

That said, if you do have some need for this, Ansible exposes a retry mechanism at the end of a failed playbook that looks like:

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/user/playbook.retry

If you were directly on the box you could then run something along the lines of:

ansible-playbook playbook.yml --limit @/home/user/playbook.retry

To make this be available as a provisioner to Vagrant you need to add another named provisioner so your Vagrantfile might look something like:

Vagrant.configure("2") do |config|
  # ... other configuration

  # Does the normal playbook run
  config.vm.provision "bootstrap", type: "ansible" do |bootstrap|
    bootstrap.playbook = "playbook.yml"
  end

  # Picks up from any failed runs
  # Run this with: "vagrant provision --provision-with resume"
  config.vm.provision "resume", type: "ansible" do |resume|
    resume.playbook = "playbook.yml"
    resume.limit = "--limit @/home/user/playbook.retry"
  end
end

As pointed out in the comments of the Vagrantfile this will then try to run both the playbook.yml playbook and the playbook.retry retry play that is created on a failed run on a first vagrant up. If the playbook.yml fails then it will automatically try and resume (and presumably fail as you are yet to fix why it failed) and then exit.

You could then fix what needed fixing in your playbook or inventory and then run vagrant provision --provision-with resume to run the provisioning block called resume to pick up from where playbook.yml failed when you originally provisioned the instance.

Be warned though that the limit option on playbook will mean that any facts/variables gathered before the previous playbook failed will not be available to the retry attempt. I'm not sure if there's a good way to regather these facts prior to running the retry and as mentioned I'd definitely lean to re-running the whole playbook on failure anyway.

like image 188
ydaetskcoR Avatar answered Oct 19 '22 07:10

ydaetskcoR