Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Understanding Asynchronous Tasks in Ansible

More than just modules, some core features of Ansible make it a great tool for any systems administrator. Learn about asynchronous tasks in Ansible now.

Jun 08, 2023 • 3 Minute Read

Please set an alt value for this image...

Most users know Ansible well for its ability to perform configuration management as well as orchestrate complex software deployment. However, Ansible also has a reasonable arsenal of features that lend themselves to operational tasks. There are modules that can handle simple tasks such as creating user accounts and restarting daemons. But more than just modules, some core features of Ansible make it a great tool for any systems administrator.

Reporting in

One of the common tasks that systems administrators regularly perform has to do with executing various jobs. Some of these jobs generate reports while some of these jobs may perform some essential maintenance tasks such as rebuilding a database index. Obviously the complexity of executing such a task depends on what, exactly, the task is. All that said, a common theme found in these tasks is a lot of waiting. This is especially true in report building scenarios.

Introducing Asynchronous tasks

You might think that Ansible will eventually timeout on long-running jobs. You would be correct in the default case. However, with a little configuration, you can still have Ansible take care of these tasks for you! Ansible offers the ability to asynchronously execute tasks. You have the option of configuring Ansible check back on a regular interval or you can even have Ansible "fire and forget" if you so choose. This can help you get around pesky ssh timeouts among other things!What is especially great about the asynchronous task feature is that it is really easy to use! There are only two flags affiliated with the feature. The -B flag is used to set our task timeout value. We pass a number of seconds with the flag. For example:ansible -m command -a "/opt/long-running-command" -B 600This command will execute /opt/long-running-command and allow a 600 second (or 10 minutes) timeout. By default, Ansible will hold on to your shell session and check back on the task every 15 seconds. However, you can control how often Ansible checks back or even disable checking back altogether!

Are we there yet?

The property Ansible uses for how often to check back on a launched asynchronous task is known as the poll value. We use the -p flag with a value in seconds to instruct Ansible on how often it should check to see if our task is complete. As mentioned earlier, 15 seconds is the default. What if we provide the following command:ansible -m command -a "/opt/long-running-command" -B 600 -p 0This exemplifies the Ansible "fire and forget".  Ansible starts the task and your shell prompt will be returned before the task finishes. With polling set to 0 seconds, Ansible will not check back on the task at all. This can be handy if you do not care to wait for the task to finish for some reason or another.

Conclusion

Dealing with Asynchronous tasks is just one of the many features we can find in Ansible. If you are interested in learning more about the fundamentals of ansible Ad Hoc commands, I recommend you check out Ansible Setup, Configure, and Ad-Hoc Commands Deep Dive.