Problem Statement
Asynchronous actions and polling : Let’s say you have a group of 3 servers and you run the ansible playbook.Ansible playbook contains 2 tasks , let say task 1 will run on 2 servers but it took time to run in the third server then , what happens to the task1 or task 2.
Solution Statement
The default working of ansible is to run synchronousely.It holds the connection to the remote node open untill the action is complete.Ansible basically evaluates each task results and tells on verbose itself.
Something like below which shows how many are ok , which mean already existing or ok , How many things you have changed with your playbook and how many are failed etc.
By default the setting is such as if some loop or some task fails the ansible playbook with stop then and there itself.
Now according to the question that task 1 runs on server 1 and 2 but fails on the 3rd server , so what happens to the next task and task 1 on the 3rd server.
The solution is how you have written your tasks. If you have handled the errors or not is the main question.
So by default if the tasks fails the play is aborted.
One solution is ignoring the failed tasks by adding ignore errors : yes in your task itself.
Or you can setup forced handlers to do a certain operation when one task has failed.Now our question says it takes time to run on server 3 but it’s not a failure
So we know that ansible run synchronously which means task 2 will not run if task 1 is not completed.
To avoid this you can use what is called as asynchronous mode of ansible playbook , basically to avoid connection timeout and to avoid blocking subsequent tasks.
You can set the value of poll and your asynchronous playbook will run accordingly.
Asynchronous – Best Solution is Ansible Asynchronouse mode.
- The best solution will be to run the playbook with async and poll=0.
- This will make ansible start the task 1 and without waiting for its failure or completion will move to task 2 .
- The playbook will end without checking for the async task’s .
- To avoid this you can register its output in a variable and use it later to complete with a condition until: job_result.finished
Useful Tricks for asynchronous runs:
For longer timeouts set positive poll value.
Set poll=5 for 5 seconds.
Top avoid timeouts specify maximum runtime.
Also specify how frequency you want to poll for status.
For longer timeouts set positive poll value.
There is no default for the async time limit.
To run tasks concurrently use poll:0
Look out for more from the Author.