When writing and debugging Ansible playbooks, typical workflow is as follows:
ansible-playbook ./main.yaml
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?
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.
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 .
If you use ignore_errors, ansible will continue attempting to run tasks against that host.
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”.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With