Asynchronous Tasks in Ansible

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 600

This 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 0

This 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.


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.

The post Asynchronous Tasks in Ansible appeared first on Linux Academy.


Veröffentlicht von Paul Christoph

Mein Name ist Paul Christoph Feichtinger, geboren am 15.5.1991 in Oberndorf bei Salzburg und mittlerweile stolzer Autor von 11 Büchern (7 in Deutsch und 4 in Englisch), 4 Apps (bald kommt Nummer 5) und dieser Webseite. Bei Paul Solutions bekommen alle Bücher und Apps ihre Form. Sieh dich ruhig ein bisschen auf meiner Webseite um, vielleicht gibt es auch für dich noch das ein oder andere zu entdecken. ;-)

Kommentar verfassen

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

%d Bloggern gefällt das: