Asynchronous Tasks in Ansible – Linux Academy
One of the widespread duties that techniques directors frequently carry out has to do with executing numerous jobs. Some of those jobs generate stories whereas a few of these jobs might carry out some important upkeep duties comparable to rebuilding a database index. Obviously the complexity of executing such a process is determined by what, precisely, the duty is. All that mentioned, a typical theme discovered in these duties is lots of ready. This is very true in report constructing eventualities.
You may suppose that Ansible will ultimately timeout on long-running jobs. You could be right in the default case. However, with slightly configuration, you possibly can nonetheless have Ansible maintain these duties for you! Ansible gives the flexibility to asynchronously execute duties. You have the choice of configuring Ansible verify again on a daily interval or you possibly can even have Ansible “fire and forget” in the event you so select. This might help you get round pesky ssh timeouts amongst different issues!
What is very nice concerning the asynchronous process function is that it’s very easy to make use of! There are solely two flags affiliated with the function. The
-B flag is used to set our process timeout worth. We go numerous seconds with the flag. For instance:
ansible -m command -a "/opt/long-running-command" -B 600
This command will execute
/choose/long-running-command and permit a 600 second (or 10 minutes) timeout. By default, Ansible will maintain on to your shell session and verify again on the duty each 15 seconds. However, you possibly can management how typically Ansible checks again and even disable checking again altogether!
The property Ansible makes use of for the way typically to verify again on a launched asynchronous process is called the ballot worth. We use the
-p flag with a price in seconds to instruct Ansible on how typically it ought to verify to see if our process is full. As talked about earlier, 15 seconds is the default. What if we offer the next command:
ansible -m command -a "/opt/long-running-command" -B 600 -p zero
This exemplifies the Ansible “fire and forget”. Ansible begins the duty and your shell immediate can be returned earlier than the duty finishes. With polling set to zero seconds, Ansible won’t verify again on the duty in any respect. This could be useful if you don’t care to attend for the duty to complete for some motive or one other.
Dealing with Asynchronous duties is simply one of many many options we are able to discover in Ansible. If you have an interest in studying extra concerning the fundamentals of ansible Ad Hoc instructions, I like to recommend you take a look at Ansible Setup, Configure, and Ad-Hoc Commands Deep Dive.