Ansible Run Playbooks in Parallel: Async, Forks & Concurrent Execution

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to run Ansible playbooks and tasks in parallel. Use async, forks, free strategy, and GNU parallel to execute multiple playbooks simultaneously with examples.

Introduction

By default, Ansible runs tasks sequentially — one task across all hosts before moving to the next. But many scenarios require parallel execution: running two independent playbooks simultaneously, launching long tasks without waiting, or maximizing throughput across hundreds of hosts.

This guide covers every approach to parallel execution in Ansible, from simple shell backgrounding to advanced async patterns.

Method 1: Run Two Playbooks Simultaneously (Shell)

The simplest approach — run separate ansible-playbook processes:

With output logging:

Method 2: GNU Parallel

Method 3: Async Tasks (Within a Playbook)

Launch tasks without waiting, then poll for results:

Method 4: Free Strategy

Default strategy (linear) waits for all hosts to finish a task before moving on. free lets each host proceed independently:

With free, fast hosts don't wait for slow ones.

Method 5: Increase Forks

This controls how many hosts are processed simultaneously per task.

Method 6: Multiple Plays in One Playbook

To make them truly parallel, split into separate files and use shell backgrounding (Method 1).

Method 7: AWX/AAP Workflow Templates

In AWX or Ansible Automation Platform, use Workflow Templates to run playbooks in parallel:

Workflow nodes can run simultaneously when they don't depend on each other.

Method 8: Import with Async

Comparison

| Method | Scope | Complexity | Best For | |--------|-------|-----------|----------| | Shell & | Playbooks | Low | 2-3 independent playbooks | | GNU parallel | Playbooks | Low | Many independent playbooks | | Async tasks | Tasks within play | Medium | Long-running tasks | | Free strategy | Hosts within play | Low | Heterogeneous host speeds | | Forks | Hosts per task | Low | Large inventories | | AWX Workflows | Playbooks | Medium | Production orchestration |

Pitfalls to Avoid

Race Conditions

Resource Contention

Async + become

FAQ

Can I run two plays in the same playbook in parallel?

No — plays within a playbook always run sequentially. To run plays in parallel, put them in separate files and launch with & or GNU parallel.

What happens if an async task fails?

Check the result with async_status. If the task fails, async_status returns finished: true and failed: true. Handle it with failed_when or ignore_errors.

How many forks should I use?

Start with forks = 20-50. Each fork uses ~100MB on the controller. Monitor controller resources and adjust. For SSH, also consider pipelining = true to reduce connections.

Can I mix async and synchronous tasks?

Yes — async tasks run in the background while subsequent synchronous tasks run normally. Use async_status to collect results when needed.

Related Articles

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home