How to use Ansible asynchronous actions and polling

abstract business code coder
Reading Time: 3 minutes

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 .

Text

Description automatically generated

  • 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

Text

Description automatically generated

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.

Written by 

Rahul Miglani is Vice President at Knoldus and heads the DevOps Practice. He is a DevOps evangelist with a keen focus to build deep relationships with senior technical individuals as well as pre-sales from customers all over the globe to enable them to be DevOps and cloud advocates and help them achieve their automation journey. He also acts as a technical liaison between customers, service engineering teams, and the DevOps community as a whole. Rahul works with customers with the goal of making them solid references on the Cloud container services platforms and also participates as a thought leader in the docker, Kubernetes, container, cloud, and DevOps community. His proficiency includes rich experience in highly optimized, highly available architectural decision-making with an inclination towards logging, monitoring, security, governance, and visualization.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading