Operations 10 min read

Using Ansible Async Mode to Run Long‑Running Tasks in Playbooks

This article explains how Ansible’s async and poll parameters enable background execution of long‑running tasks, allowing playbooks to continue without blocking, and provides practical examples, configuration tips, and status‑checking techniques for reliable DevOps automation.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Ansible Async Mode to Run Long‑Running Tasks in Playbooks

Ansible is an automation tool for managing and configuring computer systems and network devices using YAML playbooks, communicating over SSH without requiring agents on target nodes, and can scale from small servers to large enterprise networks.

Application Scenarios of Ansible in DevOps

In the DevOps lifecycle, Ansible serves many roles such as continuous delivery, automation, configuration management, and testing.

Automated deployment: quickly deploy and configure applications from code repositories to production.

Application configuration management: organize and maintain configuration files, simplify upgrades and rollbacks.

System configuration management: manage OS packages, security settings, network configuration, users and permissions.

Remote task execution: collect server information or run commands across many hosts.

Automated test environments: provision and configure test environments for developers and testers.

Problem Description

By default, Ansible runs tasks synchronously, keeping the SSH connection open until each task finishes, which blocks subsequent tasks. This behavior is problematic for long‑running operations such as restarting services, executing lengthy scripts, or performing software upgrades, as they may exceed SSH timeout limits.

Restarting a client and waiting for it to come back online.

Running scripts that take a long time.

Long‑running shell commands or software upgrades.

The solution is to use Ansible’s asynchronous mode.

What Is Async Mode?

Async mode lets you run tasks in the background while the playbook continues. You enable it with the async and poll parameters:

async : total time (in seconds) allowed for the task to complete. The task is launched in the background and Ansible returns control immediately.

poll : frequency (in seconds) with which Ansible checks the task’s status. If the task exceeds the async timeout, it is marked as failed.

Setting a Longer Timeout

To give a specific task a longer timeout, set positive values for async and poll . The playbook will still wait for the asynchronous task to finish, fail, or time out, but only after the specified async period.

- name: example playbook
  hosts: all
  tasks:
    - name: first task sleep 10
      shell: sleep 10
      async: 5
      poll: 3

    - name: second task sleep 5
      shell: sleep 5

Running Tasks Asynchronously

If you set poll: 0 , Ansible starts the task and immediately moves to the next one without waiting—this is the “fire‑and‑forget” approach. Multiple tasks can run in parallel by using async with poll: 0 .

- name: example playbook
  hosts: all
  tasks:
    - name: first task sleep 10
      shell: sleep 10
      async: 20
      poll: 0

    - name: second task sleep 5
      shell: sleep 5

The playbook above contains two tasks:

The first task runs sleep 10 asynchronously with a 20‑second timeout.

The second task runs sleep 5 normally.

Because the first task is asynchronous, Ansible does not wait for it to finish before starting the second task.

async $ ansible all -m async_status -a "jid=JID" -b -kK

Using async_status

The async_status module can be used within a playbook to poll the status of a previously launched asynchronous job. You can register the job ID, then later query its state until it finishes.

- name: example playbook
  hosts: all
  tasks:
    - name: first task sleep 10
      shell: sleep 10
      async: 20
      poll: 0
      register: first_task_status

    - name: second task sleep 5
      shell: sleep 5

    - name: until_async_status
      async_status:
        jid: "{{ first_task_status.ansible_job_id }}"
      register: job_result
      until: job_result.finished

Summary

When used together, async and poll address two common issues:

Long‑running tasks that would otherwise block the playbook.

Situations where a command finishes but the playbook still waits for a response, leading to unnecessary delays.

In short, these options provide powerful asynchronous capabilities that improve the reliability and efficiency of Ansible playbooks.

automationDevOpsAsyncAnsiblePollingBackground Executionplaybook
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.