Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue execution on failed task after fixing error in playbook?

When writing and debugging Ansible playbooks, typical workflow is as follows:

  1. ansible-playbook ./main.yaml
  2. Playbook fails on some task
  3. Fix this task and repeat line 1, waiting for all previous tasks to execute again. Which takes a lot of time

Ideally, i'd like to resume execution on failed task, having inventory and all facts collected by previous tasks. Is it even possible? How to make playbook writing/debugging faster?

like image 590
Sergey Alaev Avatar asked Apr 27 '15 15:04

Sergey Alaev


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 one option that Ansible gives you to check your playbook for syntax errors?

You may want to verify your playbooks to catch syntax errors and other problems before you run them. The ansible-playbook command offers several options for verification, including --check , --diff , --list-hosts , --list-tasks , and --syntax-check .

What keyword makes Ansible not exit on error and continue play execution?

If you use ignore_errors, ansible will continue attempting to run tasks against that host.

What is used to run the specific task of a playbook?

To start executing your playbook at a particular task (usually the task that failed on the previous run), use the --start-at-task option. In this example, Ansible starts executing your playbook at a task named “install packages”.


2 Answers

Take a look at Executing playbooks for troubleshooting. If you want to start executing your playbook at a particular task, you can do so with the --start-at-task option:

ansible-playbook playbook.yml --start-at-task="install packages" 

The above will start executing your playbook at a task named “install packages”.

Alternatively, take a look at this previous answer How to run only one task in ansible playbook?

Finally, when a play fails, it usually gives you something along the lines of:

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

Use that --limit command and it should retry from the failed task.

like image 157
Mxx Avatar answered Sep 28 '22 08:09

Mxx


Future readers:

The --limit @/home/user/site.retry would not help in such a scenario, the .retry only stores the failed host and nothing more, so will just execute all tasks against failed hosts.

If you are using the latest version (Ansible 2.x) the --start-at-task does not work for tasks defined inside roles.

You can achieve similar effect by just using the --step flag e.g: ansible-playbook playbook.yml --step. The step asks you on before executing each task and you could choose (N)o/(y)es/(c)ontinue.

With this approach you selectively execute tasks when needed and also continue from point where it failed, after fixes.

like image 39
Segmented Avatar answered Sep 28 '22 08:09

Segmented