Lesson 8 of 9 · 27m
Scaling Ansible to Hundreds of Hosts Without Losing the Plot
Tune forks, SSH behavior, fact caching, batching, and output so large fleet runs remain fast, observable, and operationally safe.
Ansible scales horizontally until the day it doesn't. The first symptoms are rarely dramatic: runs take an hour, facts time out, output scrolls too fast to inspect, one overloaded control node drops SSH connections, and nobody is sure which half of the fleet converged. Scale is not just making things faster. It is preserving correctness and evidence while parallelism rises.
Forks control concurrency, not safety
Increase parallel connections deliberately:
# ansible.cfg
[defaults]
forks = 50
timeout = 30
[ssh_connection]
pipelining = true
ssh_args = -o ControlMaster=auto -o ControlPersist=60sFifty forks means fifty targets may be actively executing tasks. It does not mean fifty machines are safe to disrupt at once. Performance and blast radius are separate decisions. Do not raise forks because a run is slow and then forget you also raised incident capacity.
Use serial for failure domains
serial limits how many hosts execute a play together:
- name: Configure application servers
hosts: web
serial:
- 1
- "10%"
- -1
tasks:
- ansible.builtin.include_role:
name: app_deployThis canary pattern starts with one host, expands to ten percent, then opens the remaining fleet. It is one of the highest-value controls in Ansible. Combine it with verification tasks and any_errors_fatal where appropriate so the batch stops before a bad configuration spreads everywhere.
Facts cost real time
Gathering facts across hundreds of nodes is expensive. If only three tasks need facts, do not pay the full cost every play:
- hosts: web
gather_facts: false
tasks:
- name: Gather only service-relevant facts
ansible.builtin.setup:
gather_subset:
- "!all"
- "network"Fact caching can help across repeated runs:
fact_caching = jsonfile
fact_caching_connection = /var/cache/ansible/facts
fact_caching_timeout = 3600A cache is a trade: speed now, potentially stale truth later. Use short timeouts for operational decision-making and never treat cached facts as proof after disruptive change.
Control node bottlenecks are real
The control node terminates SSH sessions, renders templates, forks workers, writes output, and sometimes copies large artifacts. Monitor CPU, memory, file descriptors, network throughput, and disk I/O. A laptop on hotel Wi-Fi is not a fleet deployment system. Use a controlled automation runner with network reachability, locked dependencies, and auditable logs.
Output must survive the scale
Human scrolling is not a scaling strategy. Configure concise output for routine runs and preserve machine-readable evidence:
stdout_callback = ansible.posix.yaml
callbacks_enabled = ansible.posix.profile_tasks
display_skipped_hosts = falseprofile_tasks helps find slow tasks. For compliance or incident reconstruction, ship structured logs to durable storage. If the only evidence is a terminal scrollback buffer, you do not have evidence.
Reduce work before increasing horsepower
The fastest task is the one you do not execute. Split large plays by role target, avoid unnecessary package index refreshes, distribute artifacts through object storage or a package repository instead of copying them serially from the control node, and remove obsolete compatibility tasks. Scaling a wasteful playbook merely creates waste faster.
Field notes
asyncandpollare for bounded long-running work, not a way to abandon tasks and hope.freestrategy can improve throughput but destroys simple linear reasoning; use it only when host interdependence is absent.- SSH multiplexing helps until control persistence masks network instability or creates stale connection confusion.
- A host failing in batch one may invalidate assumptions for batch two. Verification belongs inside the rollout.
- Measure baseline runtime before tuning. Otherwise every optimization story becomes folklore.
Was this lesson helpful?