DEV Community

Cover image for Ansible at Scale: Automating 100+ Linux Servers Without Chaos
Shuvo Halder
Shuvo Halder

Posted on

Ansible at Scale: Automating 100+ Linux Servers Without Chaos

Ansible's pitch is simple: automate over SSH, no agent needed, describe the desired state in YAML, and run it again safely. That pitch holds at 5 servers. At 100+ servers, the same playbook can become slow, unpredictable, and dangerous — a bad task applied everywhere at once, a secret leaked in a log, a playbook that works on your laptop and fails in production.

The difference is not the tool. It is the structure around it: inventory, roles, execution strategy, secrets, and a dry-run habit. This post covers how production teams run Ansible at scale without chaos.

Why Ansible Works: Agentless by Design

Ansible's control node pushes tasks to target hosts over SSH. There is no agent to install, no persistent daemon, no port to open on the managed servers — only SSH and Python on the target. That is why teams adopt it fast, and why it scales: adding a server means adding a line to the inventory, not deploying software to it.

Two concepts carry the whole design:

  • Idempotency. Running a playbook twice produces no change the second time. A task that installs a package checks first, installs only if missing, and reports "ok" instead of "changed" on re-run. Safe to re-run means safe to retry, safe to schedule, and safe to run in CI.
  • Desired state, not scripts. Playbooks describe what the system should look like — package installed, service running, file present with these contents — not the steps to get there. Ansible figures out the steps, and skips the ones already done.

How It Is Organised: Inventory, Playbooks, Roles

Scale starts with structure. A flat folder of playbooks collapses under its own weight; a role-based layout survives growth.

Inventory is the list of hosts, grouped by environment, role, or datacenter. Static YAML inventory works for small fleets. At scale, use dynamic inventory — pulled from your cloud provider or CMDB — so a new server appears in Ansible the moment it exists, and a terminated one disappears instead of failing every run.

# inventory/prod.yml
all:
  children:
    web:
      hosts:
        web01.example.com:
        web02.example.com:
    db:
      hosts:
        db01.example.com:
Enter fullscreen mode Exit fullscreen mode

Playbooks are the entry points: site.yml describes the desired state in YAML, calling tasks and roles in order. Tasks call modules — the units that do the work, like apt, copy, or service — and modules run on the hosts, gathering facts (OS, network, installed packages) first.

Roles are the reusable unit: roles/ with tasks/, handlers/, templates/, and vars/ inside. A role does one thing well — installs and configures NGINX, hardens SSH, sets up monitoring — and every playbook that needs it reuses it instead of copying tasks. Reusable, organised, modular: that is the whole point.

A layout that scales:

inventory/
  prod.yml
  staging.yml
playbooks/
  site.yml
  web.yml
roles/
  common/
  nginx/
  hardening/
group_vars/
  all.yml
  web.yml
Enter fullscreen mode Exit fullscreen mode

Staying Safe at Scale: The Five Controls

1. Dynamic inventory

Pull hosts from the cloud or CMDB. A static list goes stale the week a server is replaced, and stale inventory means tasks running against dead hosts — or worse, against the wrong host.

2. Limit and tags

Never run a fleet-wide playbook without a way to narrow it. --limit runs only on a subset, --tags runs only the tagged tasks. Test on one host, then ten, then the fleet. The command you run in production should be the same one you tested — only the limit changes.

ansible-playbook site.yml --limit web01 --check
ansible-playbook site.yml --limit web --tags nginx
Enter fullscreen mode Exit fullscreen mode

3. Check mode

--check is a dry run: Ansible reports what would change without changing anything. Run it first, every time, before the real run. Pair it with --diff to see exactly what a file task would change, line by line. A playbook that surprises you in check mode would have hurt you in the real run.

4. Vault for secrets

Secrets never go in plaintext vars. ansible-vault encrypt protects sensitive variables, and the vault password lives outside the repository — in CI secrets, a password manager, or a vault service. A playbook in Git should be safe to read by anyone; the secrets it needs are decrypted only at run time.

5. Batch with serial

serial: 10 runs a playbook in batches of ten hosts. If a task breaks, it breaks ten servers, not a hundred — and the run stops before the rest are touched. Rolling updates, canary batches, and maintenance windows all depend on this one setting. Avoid downtime by never changing everything at once.

Performance: When 100 Servers Feel Slow

Ansible's default is five parallel forks. At 100+ hosts, raise forks in ansible.cfg — 20 to 50 is a common production range — but watch the control node's memory and the target's SSH limits. Two more levers matter more than forks:

  • Fact caching. Gathering facts on every host on every run is slow. Cache facts (JSON file, Redis, or Memcached) so a re-run skips the discovery step.
  • SSH pipelining. Enable pipelining = True in ansible.cfg to reduce SSH round trips per task. It is the single biggest speedup for most playbooks, and it is off by default.

Measure before tuning: ansible-playbook with --verbose shows where the time goes. Usually it is facts and SSH handshakes, not the tasks themselves.

The Workflow: Run Safely at Scale

  1. Update inventory — confirm the hosts are right before anything runs.
  2. Dry-run first — ansible-playbook site.yml --check --diff.
  3. Review the changes — read the diff. If a task would change something you did not expect, stop and find out why.
  4. Run on a limited set — --limit to one host, then one group.
  5. Run in serial batches — serial: 10, watching the first batch before the rest.
  6. Verify and repeat — check the service, check the logs, then run the same playbook against the next batch.

The same playbook, the same command, every time. The only thing that changes is the size of the blast radius — and it grows only after the small run proves itself.

Testing: Don't Learn About Bugs in Production

  • ansible-lint in CI catches style errors, deprecated modules, and risky patterns before a playbook ever runs.
  • Molecule tests roles in containers: create, converge, verify, destroy. A role that passes Molecule is a role you can trust on a hundred servers.
  • A staging inventory that mirrors production. A playbook that has never run against staging has never been tested.

The Mistakes That Cause Chaos

  • Running a new playbook against the whole fleet without --limit or --check.
  • Secrets in plaintext vars, committed to Git.
  • Static inventory that nobody updates — tasks failing on dead hosts, or hitting the wrong ones.
  • No serial batching: one bad task, a hundred broken servers.
  • Skipping check mode "to save time" — and spending the night rolling back.
  • Roles that do five things, copied and pasted instead of reused.
  • No fact caching, so every run takes twenty minutes and nobody runs it.

The Production Checklist

  • Dynamic inventory, or a static one with an owner and a review cadence.
  • Roles, one job each, tested with Molecule.
  • --check --diff before every real run.
  • --limit and --tags for every change, tested small first.
  • serial batching on every fleet-wide change.
  • Vault for every secret, vault password outside the repo.
  • Fact caching and pipelining tuned, forks raised deliberately.
  • ansible-lint in CI, staging inventory that mirrors production.

What Hiring Managers Actually Ask

If you are targeting remote DevOps, SRE, or sysadmin roles, Ansible at scale is interview material because it tests judgement, not syntax:

  • "You need to patch 200 servers with zero downtime — walk me through it." (Answer: check mode first, limit to a canary host, serial batches of ten, verify the service after each batch, and stop the run the moment a batch fails.)
  • "How do you keep secrets out of your playbooks?" (Answer: Ansible Vault for sensitive vars, vault password from CI secrets or a secrets manager, never plaintext in Git, and no_log: true on tasks that handle secrets.)
  • "A playbook works on 5 servers but times out on 100 — what do you change?" (Answer: raise forks, enable pipelining and fact caching, and check whether a task is gathering facts or opening a new SSH connection per host.)
  • "How do you test a role before it touches production?" (Answer: ansible-lint in CI, Molecule converge-and-verify in containers, then a staging inventory run with --check --diff before the production run.)

The answer they want is never "I know YAML." It is the system around it: small blast radius, dry runs, secrets discipline, and a tested path from laptop to fleet.

Key Takeaway

Always use --check first. Keep roles reusable. Store secrets in Vault. Run in batches to avoid chaos. Ansible is simple — scale is what makes it serious.


Your turn: How do you run Ansible in your team — static or dynamic inventory, and what batch size do you use for fleet-wide changes? Tell me in the comments.

Top comments (0)