Ansible Asynchronous Actions and Polling

Ansible runs tasks synchronously by default. It keeps the connection to the remote node open until the task is completed. This means within a playbook, each task blocks the subsequent tasks until the current task completes. This behavior may cause issues.

Suppose you have a task in your playbook which takes more than say 10 minutes to execute. This means that the ssh connection between Ansible controller and the target machine should be stable for more than 10 minutes. It may take longer to complete than the SSH session allows for, causing a timeout. One can run the long-running process to execute in the background to perform other tasks concurrently.

Ansible provides you Asynchronous mode, which lets you control how these long-running tasks execute. We can tell Ansible to check the status of the task after any particular interval.

Synchronous playbook tasks

To check if the connection is stable for a certain time by adding a hard wait (add a sleep for 'n' sec). Consider the below playbook:

- name: this is our 1st play.
  hosts: webserver1
  tasks:
    - name: "sleep for 120 sec"
      command: sleep 120
    - name: "second task"
      command: touch /tmp/second_task.txt

The first task sleep for 120 sec will take 2 minutes to complete. Once the sleep is over, it will create a file at a defined location. In this period, a connection might be interrupted causing failure.

Asynchronous playbook tasks

You can use asynchronous mode to avoid connection timeouts or to avoid blocking subsequent tasks. The behavior of asynchronous mode in a playbook depends on the value of poll.

Setting poll greater than 0

If you want to set a longer timeout limit for a certain task in your playbook, use async with a poll set to a positive value. Now, let's create a playbook with asynchronous actions and polling as shown below:

- name: this is our 1st play.
  hosts: webserver1
  tasks:
    - name: "sleep for 60 sec"
      command: sleep 60
      async: 70
      poll: 35
    - name: "second task"
      command: touch /tmp/second_task.txt

async = Maximum runtime. The task will timeout if it exceeds the limit of this parameter.

poll = This parameter tells how frequently you would like to poll for status

Setting poll equal to 0 (FIRE-AND-FORGET)

To run multiple tasks in a playbook concurrently, set poll to 0. When you set poll equal to 0, Ansible starts the task and moves on to the next task without waiting for a current task result. In this case, each and every async task will run until it either completes, fails or times out (runs longer than its async value). The playbook run completes the execution even without checking the status of async tasks.

Sample playbook for poll: 0

- name: this is our 1st play.
  hosts: webserver1
  tasks:
    - name: "sleep for 120 sec"
      command: sleep 120
      async: 60
      poll: 0
    - name: "second task"
      command: touch /tmp/second_task.txt

Default Poll Interval

The default poll value is set by the DEFAULT_POLL_INTERVAL setting which has a default value of 15 sec. There is no default for the async time limit. If you do not provide the 'async' parameter, the task runs synchronously.