Ansible Variable Precedence — The 22-Level Silent Override
A forgotten host_vars file overrode group_vars with zero warnings, breaking prod.
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
- ✓Basic programming fundamentals
- ✓A computer with internet access
- ✓Willingness to follow along with examples
- Ansible is agentless configuration management — it connects via SSH, pushes small modules, and cleans up after itself
- Three core components: Inventory (what servers), Modules (how to act), Playbooks (when to act)
- Idempotency means running the same playbook 100 times produces the same result as running it once
- Performance trade-off: agentless means zero maintenance on servers but higher control node load (forks control parallelism)
- Production trap: variable precedence has 22 levels — your dev environment works but prod breaks because host_vars silently overrides group_vars with no warning
- Biggest mistake: a host_vars file left over from a debugging session six months ago quietly overrides your group-level config in production — compiles fine, deploys fine, serves the wrong value
Ansible is an open-source IT automation engine that eliminates manual toil by letting you define infrastructure as code — no agents required, just SSH and Python on the target. It solves the problem of configuring thousands of servers consistently without writing shell scripts that rot.
You describe the desired state in YAML (playbooks), and Ansible figures out the diff and applies only what's needed. Its agentless architecture means you don't install anything on managed nodes, which is why it dominates in heterogeneous environments where you can't control the OS.
The trade-off: it's not real-time (no daemon watching for drift) and can be slow at scale compared to pull-based tools like Puppet or Salt — Netflix runs 100,000+ nodes with Ansible, but they batch aggressively.
At its core, Ansible has three concepts: inventory (what you manage), playbooks (how you manage it), and modules (the actual work). Inventory can be static files or dynamic sources like AWS EC2 or vSphere. Playbooks are ordered lists of tasks, each calling a module — think of modules as idempotent functions that ensure a package is installed or a service is running.
The killer feature is variable precedence: a 22-level ladder that silently overrides values from defaults through command-line extras. Most teams get burned when a group_var in inventory overrides a role default without warning — you'll learn to pin variables at the right rung or use assert to catch surprises.
For production, you layer roles (reusable task bundles), Ansible Vault for secrets, and rolling update patterns with serial and max_fail_percentage. Error handling uses ignore_errors, failed_when, and block/rescue — but the real pattern is pre-flight validation with assert before touching state.
Ad-hoc commands (ansible -m ping) let you run one-off operations across fleets without writing a playbook, useful for quick health checks or reboots. When not to use Ansible: for real-time configuration drift detection (use Chef or a monitoring stack), or for complex orchestration with cross-host dependencies (Terraform or a workflow engine handles that better).
Managing 100 servers by logging into each one and typing commands is like calling 100 employees individually to give the same instruction. Ansible is like sending one company-wide email that everyone acts on simultaneously. You describe the desired state of your servers in plain English-like YAML, and Ansible connects over SSH and makes it happen — on all servers at once, with no software installed on them.
Think of it this way: if your server is a hotel room, Ansible is the housekeeping checklist pinned to the door. It doesn't live in the room. It walks in, checks what needs fixing, fixes only what's broken, and walks out. The room doesn't even know Ansible was there — it just ends up clean.
And unlike calling each employee individually, if you send the same company-wide email again tomorrow, nothing bad happens. Everyone already followed the instructions. They'll read the email, confirm nothing needs doing, and get back to work. That's idempotency — the property that makes Ansible safe to run on a schedule, in a CI pipeline, or in a panic at 2am.
Before configuration management tools, sysadmins maintained hundreds of servers by hand — logging in, running commands, hoping nothing went wrong. I lived this. In 2015, I managed a fleet of 80 web servers at a mid-size SaaS company, and every deploy night was a three-hour marathon of SSH sessions, copy-pasted commands, and prayer. One night, someone restarted the wrong database server. We lost four hours of customer data. That was the last straw.
Ansible was created by Michael DeHaan in 2012 and acquired by Red Hat in 2015 (now part of IBM). Today it runs infrastructure at NASA JPL, Capital One, and thousands of companies from Series A startups to Fortune 50 enterprises. Not because it's the most powerful automation tool, but because it's the simplest one that actually gets used.
What makes Ansible different from competitors like Chef and Puppet is that it is agentless. There is no daemon running on your managed servers, no SSL certificates to exchange, and no extra ports to open beyond standard SSH (or WinRM for Windows). Ansible runs from your control node, pushes small programs called Ansible Modules to the remote nodes, executes them, and then cleans up after itself.
One important nuance that comes up in almost every team adopting Ansible: Ansible and Terraform are not competitors — they solve different problems at different points in a server's life. Terraform creates infrastructure: it provisions the EC2 instance, creates the VPC, registers the DNS record. Ansible configures that infrastructure: it installs software, deploys application code, manages services, and corrects configuration drift on day 2, day 30, and day 300. Terraform's user_data and cloud-init can run a script at first boot, but they can't re-run idempotently when you need to update a config three months later. Ansible can. That's the real distinction — Terraform builds the house once, Ansible keeps it clean indefinitely.
In this guide, we'll break down Ansible's core architecture — inventories, playbooks, modules, and roles — cover ad-hoc commands for quick fleet operations, and build production-grade automation with real error handling, secret management, and reusable patterns. Every section includes the production detail that most tutorials skip.
How Ansible Variable Precedence Really Works
Ansible variable precedence is a 22-level hierarchy that determines which value wins when the same variable is defined in multiple places. At its core, it's a deterministic override chain: from lowest priority (command-line -e vars) to highest (role defaults). The mechanic is simple — the last definition in the chain wins — but the chain itself is long and easy to misread.
In practice, this means a variable set in group_vars/all (level 14) will be silently overridden by a host_vars entry (level 19), which in turn can be overridden by a --extra-vars flag (level 22). The hierarchy is fixed and cannot be modified. Most teams only use 5–7 levels, but the remaining 15 create invisible traps when variables collide across inventories, roles, playbooks, and includes.
You need this hierarchy to separate concerns: default values in roles, environment-specific overrides in inventory, and emergency overrides via CLI. Without understanding the full chain, you'll debug 'why is my variable wrong?' for hours — only to find a forgotten vars/main.yml in a nested role silently winning over your carefully set inventory variable.
--- - name: Demonstrate variable precedence hosts: localhost gather_facts: no vars: my_var: "play level" tasks: - name: Set variable at task level ansible.builtin.set_fact: my_var: "task level" - name: Display my_var ansible.builtin.debug: var: my_var vars_files: - vars_file.yml
group_vars/all is not the final value — it's just level 14 of 22. Any role, include, or CLI flag can override it without warning.group_vars/production but a nested role's vars/main.yml (level 20) silently overrode the database hostname, causing all production writes to hit a staging database.ansible-inventory --list to dump resolved variables before a run; never assume a variable's source is the one you set.ansible-inventory --list before trusting a playbook's behavior.Inventory, Playbooks, and Modules — The Three Core Concepts
Ansible's architecture relies on three primary building blocks. Get these right and everything else follows. Get any one of them wrong and you'll spend your time debugging instead of automating.
- The Inventory: A file (INI or YAML) that lists the servers you want to manage, organized into groups like [webservers] or [databases]. The inventory is your single source of truth about what exists. In production, you'll almost always use dynamic inventory — pulling host lists directly from AWS, GCP, or Azure APIs so your inventory stays accurate as servers are created and destroyed by autoscaling. Static inventories work for learning and small fixed fleets under 20 servers, but once you have autoscaling groups or spot instances, a static file becomes a liability. Stale IPs, terminated instances, missing new nodes — a static inventory in an elastic environment is a disaster on a timer.
- The Playbook: Your automation blueprint, written in YAML. A playbook maps groups of hosts to sequences of tasks and describes desired state rather than step-by-step instructions. This distinction matters operationally: if Nginx is already installed and running at the right version, Ansible confirms it and moves on. It doesn't reinstall. It doesn't restart unnecessarily. It checks and reports 'ok'.
- Modules: The tools in the toolbox. Instead of writing bash scripts, you use modules like apt, yum, service, copy, or template. These modules are idempotent — they check the current state of the server and only make changes when the server doesn't match your desired state. The shell and command modules are the notable exceptions. They run unconditionally every time, which is exactly why experienced Ansible engineers avoid them unless there is genuinely no dedicated module alternative.
For dynamic inventory specifically — here's what it looks like in practice. You create a plugin configuration file (aws_ec2.yml) that Ansible reads instead of a static hosts file. It queries the AWS EC2 API, groups instances by their tags, and returns a live host list. The inventory is never stale because it's rebuilt from the API on every run.
# io.thecodeforge: Static Inventory for Project Forge # Use this for fixed infrastructure under 20 servers. # For elastic/cloud environments, use dynamic inventory (aws_ec2.yml below). [webservers] web-01.thecodeforge.io ansible_host=192.168.1.10 ansible_user=ubuntu web-02.thecodeforge.io ansible_host=192.168.1.11 ansible_user=ubuntu [databases] db-01.thecodeforge.io ansible_host=192.168.1.20 ansible_user=ubuntu [production:children] webservers databases [production:vars] ansible_ssh_private_key_file=~/.ssh/forge_deploy_key # ────────────────────────────────────────────────────────────────────────────── # io.thecodeforge: Dynamic Inventory Plugin Config (aws_ec2.yml) # Save this as inventories/production/aws_ec2.yml # Run: ansible-inventory -i inventories/production/ --list # ────────────────────────────────────────────────────────────────────────────── # plugin: amazon.aws.aws_ec2 # regions: # - eu-west-1 # filters: # instance-state-name: running # tag:Environment: production # keyed_groups: # - key: tags.Role # prefix: role # separator: '_' # - key: tags.Environment # prefix: env # separator: '_' # hostnames: # - private-ip-address # compose: # ansible_user: "'ubuntu'" # ansible_ssh_private_key_file: "'~/.ssh/forge_deploy_key'" # cache: true # cache_plugin: jsonfile # cache_connection: /tmp/ansible_aws_cache # cache_timeout: 300 # # With this config: # - Instances tagged Role=webserver appear in group role_webserver # - Instances tagged Environment=production appear in group env_production # - Cache prevents hammering the EC2 API on every run (5-minute TTL) # - New instances appear automatically — no manual inventory updates
Your First Production Playbook — and the 22-Level Precedence Ladder
A playbook is a collection of plays. Each play targets a specific group from your inventory and executes a sequence of tasks in order, top to bottom. If a task fails on a specific host, Ansible stops executing for that host but continues for the others. To handle configuration changes — like restarting a web server only when a config file actually changes — Ansible uses Handlers: special tasks that only run when notified by another task that reported 'changed'.
The playbook below is a production pattern we actually use. Notice: update the package cache, install the binary, deploy a templated config, ensure the service is running. Every task is idempotent. Every task uses a dedicated module. No shell commands.
But here's what the Ansible documentation buries in a footnote that causes more production incidents than anything else: variable precedence has 22 levels, and Ansible enforces them silently. The most important levels to internalize — from highest to lowest priority:
- Extra vars (-e on the command line) — highest, overrides everything
- Task vars (set directly on a task)
- Block vars
- Role and include vars
- Set_facts and registered vars
- host_vars/hostname.yml — this is where the production incident in this article came from
- group_vars/groupname.yml
- group_vars/all.yml
- Playbook vars
- Role defaults (defaults/main.yml) — lowest, easily overridden by anything above
The rule that causes the most surprises: host_vars always overrides group_vars. Always. Without any warning. Without any log entry. If prod-web-01.yml exists in your host_vars directory, it wins over group_vars/all.yml, group_vars/webservers.yml, and everything you defined in your playbook's vars block — silently.
The diagnostic you need to run before every production deploy where variables are involved: ansible-inventory -i inventory.ini --host prod-web-01 --vars. This shows you the fully merged, fully resolved variable set that Ansible will actually use. Not what you think you set. Not what's in the playbook. The ground truth.
--- # io.thecodeforge: Standard Nginx Deployment Playbook # Variable precedence reminder (highest to lowest — the levels that matter most): # 1. Extra vars (-e) <- overrides EVERYTHING, use with extreme care in CI # 2. set_fact / registered <- runtime-computed values # 3. host_vars/hostname.yml <- PER-HOST OVERRIDE, silent, highest file-based precedence # 4. group_vars/groupname.yml <- group-specific values # 5. group_vars/all.yml <- global defaults # 6. Playbook vars block <- what you see below # 7. Role defaults/main.yml <- weakest, easily overridden # # Debug tip: ansible-inventory -i inventory.ini --host prod-web-01 --vars # shows the fully merged variable set before the playbook runs. - name: Deploy and Configure Nginx hosts: webservers become: true vars: nginx_port: 80 server_name: "thecodeforge.io" # NOTE: These vars sit at precedence level 6 (playbook vars). # A host_vars file for any target host will silently override these. # Run ansible-inventory --host <hostname> --vars to verify before deploying. tasks: - name: Verify expected variable state before making any changes ansible.builtin.debug: msg: "nginx_port resolved to {{ nginx_port }} on {{ inventory_hostname }}" # Add this debug task during onboarding or when variables behave unexpectedly. # Remove or tag it once the team trusts the variable sources. - name: Ensure apt cache is updated ansible.builtin.apt: update_cache: yes cache_valid_time: 3600 # cache_valid_time: 3600 means: skip the update if cache is less than 1 hour old. # Trade-off: saves 5-10 seconds per run but means security updates won't appear # for up to an hour. Acceptable for app servers; lower this for security-sensitive roles. - name: Install Nginx production package ansible.builtin.apt: name: nginx state: present # state: present = install if missing. state: latest = upgrade if a newer version exists. # Use present in production unless you explicitly want automatic upgrades. - name: Deploy custom Nginx configuration ansible.builtin.template: src: templates/nginx.conf.j2 dest: /etc/nginx/sites-available/default owner: root group: root mode: '0644' notify: Reload Nginx service # notify only fires when this task reports 'changed'. # If the rendered template is byte-for-byte identical to the existing file, # no notification is sent and Nginx is not reloaded. This is idempotency in action. - name: Ensure Nginx service is enabled and running ansible.builtin.service: name: nginx state: started enabled: yes handlers: - name: Reload Nginx service ansible.builtin.service: name: nginx state: reloaded # reloaded sends SIGHUP — Nginx reloads config without dropping connections. # restarted kills and restarts — drops all active connections. # Always use reloaded for config changes. Use restarted only for binary upgrades.
Ad-hoc Commands — Quick Fleet Operations Without a Playbook
Not everything needs a playbook. Sometimes you need to run a single command across your fleet right now — check disk space before a deploy, restart a hung service on 50 app servers, verify a kernel patch applied across the fleet, kill a runaway process that's consuming memory. That's what ad-hoc commands are for.
Ad-hoc commands are Ansible's underrated superpower for day-two operations. They're the reason senior SREs reach for Ansible instead of writing SSH for-loops. An SSH for-loop runs the command on every server sequentially and gives you raw unstructured output. Ansible ad-hoc runs in parallel across as many hosts as your forks setting allows, returns structured output per host, handles failures gracefully, and respects your inventory groups so you don't accidentally run something against the wrong environment.
Syntax: ansible <host-pattern> -i <inventory> -m <module> -a '<arguments>'
- -b or --become: run as root (sudo)
- -u or --user: specify the SSH username
- --limit 'web-01': restrict execution to a subset of the matched hosts — critical for safe fleet operations
- --check: dry run — show what would change without actually changing anything
- -f 50 or --forks 50: override the default parallelism for this single command
- -v, -vv, -vvv, -vvvv: increasing verbosity. -v shows task results. -vvv shows SSH connection details. -vvvv shows everything including the raw module arguments — use this when debugging SSH hangs.
In production I use ad-hoc commands daily. Checking disk space on 200 servers before a deploy: one-liner, 10 seconds, structured output. Restarting a hung worker process across 50 app servers: one-liner. Verifying that a security patch actually applied to every host in the fleet: one-liner. These replace what used to be 20-minute SSH marathons with copy-pasted commands and manually collated output.
#!/usr/bin/env bash # io.thecodeforge: Ad-hoc Command Reference # These replace SSH for-loops. Run these, not bash loops. # ── Connectivity and fact-checking ─────────────────────────────────────────── # Verify SSH connectivity to all production hosts before a major deploy ansible production -i inventory.ini -m ping # Check disk space across all web servers before a deploy # -o: one-line output mode — easier to scan for problems ansible webservers -i inventory.ini -m command -a "df -h /" -o # Gather full system facts from a single host (OS, IPs, memory, CPU) # Useful for debugging environment differences between hosts ansible db-01.thecodeforge.io -i inventory.ini -m setup # Gather only a subset of facts to speed up the call # gather_subset=min returns OS, hostname, IP — skips disk/CPU details ansible webservers -i inventory.ini -m setup -a 'gather_subset=min' -o # ── Safe fleet operations with --limit ──────────────────────────────────────── # The --limit flag restricts execution to a subset of the target group. # ALWAYS use --limit when you want to test on one host before hitting the fleet. # This is the most important safety habit for ad-hoc fleet operations. # Restart Nginx on ONE host first to verify the command is correct ansible webservers -i inventory.ini -m service \ -a "name=nginx state=restarted" --become \ --limit web-01.thecodeforge.io # Once verified, restart Nginx across all web servers ansible webservers -i inventory.ini -m service \ -a "name=nginx state=restarted" --become # ── Security and maintenance ────────────────────────────────────────────────── # Apply a security patch across the entire fleet in parallel # -f 20: process 20 hosts at a time (tune based on control node resources) ansible production -i inventory.ini \ -m apt -a "name=openssl state=latest update_cache=yes" \ --become -f 20 # Verify the patch was applied — check the installed version on every host ansible production -i inventory.ini \ -m command -a "dpkg -l openssl | grep '^ii'" -o # ── Dry run before any destructive operation ───────────────────────────────── # --check: show what WOULD happen without actually doing it # Use this before any ad-hoc command that modifies state ansible webservers -i inventory.ini \ -m apt -a "name=nginx state=absent" \ --become --check # ── Verbosity for SSH debugging ─────────────────────────────────────────────── # -v: show task result summary # -vv: show connection parameters # -vvv: show SSH connection details (use this when a host is unreachable) # -vvvv: show raw SSH protocol output (use this when SSH itself is misbehaving) ansible web-01.thecodeforge.io -i inventory.ini -m ping -vvv
Roles — Reusable Automation at Scale
Once your playbooks grow beyond 50 lines, you'll start copying tasks between files. That's when you need roles. A role is a self-contained unit of automation — tasks, handlers, templates, default variables, and static files — packaged in a standardized directory structure that Ansible knows how to load automatically. Roles are how Ansible scales from 'one playbook' to 'an entire infrastructure codebase that multiple teams can contribute to.'
The directory structure is Ansible's loading convention, not optional decoration. When you reference a role in a playbook, Ansible automatically loads tasks/main.yml, handlers/main.yml, defaults/main.yml, templates/, and files/ if they exist. The structure is the contract — deviate from it and things silently don't load.
Roles come from two sources: you write your own for application-specific automation, or you pull community roles from Ansible Galaxy (ansible-galaxy install geerlingguy.nginx). Galaxy has thousands of pre-built roles for common infrastructure software. For Nginx, Docker, PostgreSQL, certbot, Redis — a battle-tested community role saves hours and handles edge cases your first draft won't. For deploying your Java application, configuring your monitoring stack, or enforcing your company's specific security baseline — you write your own.
Critically, community roles must be version-pinned in a requirements.yml file. Not managed, not latest — a specific version tag. I've watched a Galaxy role change a default variable in a minor version update and restart PostgreSQL during a maintenance window without any warning. The role's changelog mentioned it. Nobody read the changelog because nobody expected a minor version to change default behavior. Pin the version. Test the upgrade in staging. Treat a Galaxy role update the same way you treat a library dependency upgrade — with the same caution and the same verification process.
--- # io.thecodeforge: Reusable Nginx Role # # Role directory structure (Ansible's loading convention — not optional): # roles/nginx/ # ├── defaults/ # │ └── main.yml <- weakest variable precedence, safe defaults # ├── handlers/ # │ └── main.yml <- service reload/restart handlers # ├── tasks/ # │ └── main.yml <- this file, core task logic # ├── templates/ # │ └── vhost.conf.j2 <- Jinja2 config templates # └── files/ # └── (static files if needed) # # Use this role in a playbook: # - hosts: webservers # roles: # - role: nginx # vars: # server_name: api.thecodeforge.io # nginx_port: 8080 - name: Install Nginx ansible.builtin.apt: name: nginx state: present update_cache: yes - name: Deploy virtual host configuration from template ansible.builtin.template: src: vhost.conf.j2 dest: "/etc/nginx/sites-available/{{ server_name }}.conf" owner: root group: root mode: '0644' validate: '/usr/sbin/nginx -t -c %s' # validate: runs nginx -t on the rendered config before writing it. # If the config is invalid, Ansible rejects it and the file is not updated. # This prevents deploying a broken Nginx config that would fail on reload. notify: Reload Nginx - name: Enable virtual host by creating symlink ansible.builtin.file: src: "/etc/nginx/sites-available/{{ server_name }}.conf" dest: "/etc/nginx/sites-enabled/{{ server_name }}.conf" state: link notify: Reload Nginx - name: Ensure Nginx is running and enabled on boot ansible.builtin.service: name: nginx state: started enabled: yes --- # io.thecodeforge: requirements.yml — Galaxy role version pinning # Install with: ansible-galaxy install -r requirements.yml # ALWAYS pin to a specific version. Never use 'latest'. # Treat a version bump the same as a library dependency upgrade: # test in staging, read the changelog, verify behavior before deploying to prod. # roles: # - name: geerlingguy.nginx # version: 3.2.0 # # Pinned: tested against Ubuntu 22.04 LTS on 2026-03-01 # # Upgrade checklist: test in staging, verify default variable changes # # - name: geerlingguy.docker # version: 6.1.0 # # Pinned: confirmed compatible with Docker 25.x on 2026-02-15 # # - name: geerlingguy.postgresql # version: 3.4.0 # # Pinned: restart behavior tested — does NOT restart on minor config changes # # Install all roles: # ansible-galaxy install -r requirements.yml --roles-path roles/ # # Upgrade a single role safely: # ansible-galaxy install geerlingguy.nginx,3.3.0 --force # # Then test in staging before updating the version in requirements.yml
Production Patterns — Error Handling, Vault, and Rolling Deploys
The playbook we built above works correctly for a single server in a controlled environment. Production is messier. Databases fail mid-migration. Network blips cause intermittent SSH timeouts. You need to deploy to 50 servers without taking all 50 offline simultaneously. And you absolutely cannot store database passwords in plain text YAML committed to Git — not because of policy, but because production credentials in version control is a breach waiting to happen.
Error Handling with block/rescue/always: Ansible has a try/catch equivalent. Wrap risky tasks in a block. If anything inside fails, the rescue section runs — rollback, alert, log. The always section runs regardless — cleanup, notifications. Without this pattern, a failed database migration leaves your server in a half-configured state with no automatic recovery and no notification that anything went wrong.
Rolling Deploys with serial: The serial keyword controls how many hosts Ansible processes simultaneously. serial: 3 means update 3 servers, verify they're healthy, then move to the next 3. Without serial, Ansible hits all hosts simultaneously — which is acceptable for config management but catastrophic for application deploys where you need zero downtime.
Ansible Vault for Secrets: Vault encrypts variables or entire files using AES256. Create an encrypted file with ansible-vault create group_vars/production/vault.yml, add your secrets, and commit the encrypted file to Git. Without the vault password, the file is gibberish — safe to store in version control. In CI/CD, pass the vault password via a file written from a CI secret: echo "$ANSIBLE_VAULT_PASSWORD" > /tmp/vault_pass, then ansible-playbook deploy.yml --vault-password-file /tmp/vault_pass. Never use --ask-vault-pass in CI — it expects interactive input and hangs silently.
For different environments, use different vault password files — one for staging, one for production. The vault file contents can be identical in structure but different in values (different database passwords per environment), while the passwords to decrypt them are stored separately in your CI secrets manager.
--- # io.thecodeforge: Production Deploy with Error Handling, Rolling Deploy, and Vault # # Before running: # 1. Create vault file: ansible-vault create group_vars/production/vault.yml # Add: db_password: "your_real_password" # webhook_url: "https://hooks.slack.com/your/webhook" # 2. Commit the encrypted vault file to Git (safe — AES256 encrypted) # 3. Store vault password in CI secrets as ANSIBLE_VAULT_PASSWORD # 4. CI runs with: ansible-playbook deploy.yml --vault-password-file /tmp/vault_pass - name: Deploy Application with Safety Rails hosts: webservers become: true serial: 3 # Rolling deploy: process 3 servers at a time # For 30 servers: 10 sequential batches of 3 # Trade-off: 10x longer than parallel, 0 simultaneous downtime max_fail_percentage: 0 # Stop the entire deploy if ANY server in a batch fails # max_fail_percentage: 30 would allow 30% failure before aborting # For database migrations, use 0 — one failure should stop everything vars_files: - group_vars/production/vault.yml # Encrypted with ansible-vault — safe in Git # vault.yml contains: # db_password: "{{ vault_db_password }}" # webhook_url: "{{ vault_webhook_url }}" # Reference in tasks as: {{ db_password }} # Ansible decrypts at runtime using the vault password file — never stores plaintext tasks: - name: Deploy application release with rollback on failure block: # ── Step 1: Pull the new code ───────────────────────────────────────── - name: Pull latest application code ansible.builtin.git: repo: "https://github.com/thecodeforge/app.git" dest: /opt/app version: "{{ release_version }}" # release_version passed via -e on the command line: # ansible-playbook deploy.yml -e release_version=v2.4.1 # ── Step 2: Run database migrations ────────────────────────────────── - name: Run database migrations ansible.builtin.command: cmd: /opt/app/bin/migrate --env production args: chdir: /opt/app environment: DATABASE_URL: "postgres://app:{{ db_password }}@db-01:5432/appdb" # db_password comes from the vault file — never hardcoded register: migration_result # register: captures the command output for use in later tasks or rescue block # ── Step 3: Verify the application is healthy ───────────────────────── - name: Verify application health endpoint responds 200 ansible.builtin.uri: url: "http://localhost:8080/health" status_code: 200 retries: 5 # Try up to 5 times delay: 3 # Wait 3 seconds between retries # If the health check fails after 5 retries, the block fails # and rescue runs automatically rescue: # Runs only if any task in the block above fails - name: Log deployment failure with context ansible.builtin.debug: msg: > Deploy FAILED on {{ inventory_hostname }}. Release: {{ release_version }}. Rolling back to: {{ previous_release }}. Migration output: {{ migration_result.stdout | default('N/A') }} - name: Rollback to previous known-good release ansible.builtin.git: repo: "https://github.com/thecodeforge/app.git" dest: /opt/app version: "{{ previous_release }}" # previous_release passed alongside release_version: # ansible-playbook deploy.yml -e release_version=v2.4.1 -e previous_release=v2.4.0 always: # Runs regardless of success or failure — use for notifications and cleanup - name: Send deployment status notification ansible.builtin.uri: url: "{{ webhook_url }}" method: POST body_format: json body: host: "{{ inventory_hostname }}" release: "{{ release_version }}" status: "{{ 'success' if ansible_failed_task is not defined else 'failed' }}" environment: production # webhook_url comes from the vault file # ansible_failed_task is set by Ansible when a task in the block fails
Key Features of Ansible — What Actually Matters in Production
Forget the marketing fluff. Here's what makes Ansible worth your time when you're firefighting at 3 AM.
Agentless. No daemons to install, no certificates to rotate, no agents to patch. Your managed nodes just need SSH or WinRM and Python. That's it. When a node goes belly-up, you don't debug a dead agent — you fix the node.
Idempotency isn't a feature, it's a contract. Ansible modules are built to declare state, not run commands. Run a playbook twice — the second run changes nothing if the system already matches your declaration. This isn't a nice-to-have; it's what stops you from crashing production with a forgotten restart.
Declarative YAML, not imperative scripts. You write what the end state looks — "Nginx should be installed and running on port 8080." Ansible figures out the how. This shifts your brain from "I need to write an if-else tower" to "I need to describe the target state." That's the difference between a script that rots and a playbook that survives.
Extensible via Python modules. Need to manage a proprietary API? Write a custom module. The framework is trivial — return a JSON dict with changed and msg. No special SDK to learn.
// io.thecodeforge — devops tutorial // Proving idempotency — run this twice - name: Ensure Nginx is at the right state hosts: webservers gather_facts: false tasks: - name: Install Nginx package ansible.builtin.apt: name: nginx state: present # Declarative, not "apt-get install" register: install_result - name: Report if Nginx was freshly installed ansible.builtin.debug: msg: "Nginx installed this run" when: install_result.changed - name: Report if Nginx was already present ansible.builtin.debug: msg: "Nginx was already installed — no change" when: not install_result.changed
shell or command modules without creates/removes guards. Those are imperative escape hatches — treat them like surgery. Use them only when no module exists.Ansible Architecture — The Minimal Moving Parts You Must Understand
Ansible's architecture is brutally simple compared to Puppet or Chef. That's the point. Fewer moving parts means fewer failure modes.
Control Node. This is where you install Ansible. Your laptop. A bastion host. A CI runner. Ansible sends commands from here to managed nodes. Note: Windows cannot be a control node natively — use WSL or a Linux jump box.
Managed Nodes. The servers, containers, or network devices you control. They need SSH (Linux), WinRM (Windows), or a network API target. That's it. No agent, no daemon. You push commands to them, or they pull via ansible-pull if you're doing scale-out without a central server.
Inventory. A file listing your managed nodes, grouped logically. Static or dynamic — you can pull from AWS EC2, GCP, or a CMDB. An inventory can be a flat INI file or a YAML file with variables. Critical mistake: hardcoding IPs instead of using group variables.
Modules. The actual workhorses. Each module is a Python script that runs on the managed node, returns JSON, and exits. copy, file, service, template, uri, package — learn these cold. Everything else is syntactic sugar around these core primitives.
Playbooks. YAML files that orchestrate modules in order. They define which hosts, which tasks, what variables, and how to handle failures. A playbook without error handling is a fire drill waiting to happen.
Plugins. Extend Ansible's core — connection plugins, callback plugins, filter plugins. You'll rarely write one, but you'll use them daily: ansible.builtin.debug is a plugin. So is community.general.docker_container.
// io.thecodeforge — devops tutorial // A production inventory with group separation [webservers] web-01 ansible_host=10.0.1.10 web-02 ansible_host=10.0.1.11 [databases] db-primary ansible_host=10.0.2.20 db-replica ansible_host=10.0.2.21 [loadbalancers] lb-01 ansible_host=10.0.3.30 # Group variables — apply to all webservers [webservers:vars] http_port=8080 nginx_config_path=/etc/nginx/nginx.conf
How Ansible Works — The SSH Handshake and Module Execution Path
Here's the cold, hard execution path when you run ansible-playbook deploy.yml:
- Parse the playbook. Ansible reads YAML, resolves variable precedence (remember the ladder?), compiles tasks into a list.
- Build the inventory. It resolves host patterns, applies group vars, and expands host ranges. This is where your
-llimit flag filters the host list. - SSH connection (default). Ansible opens an SSH connection to each managed node. It uses
controlpersistto reuse connections — that's why first-run is slow, subsequent runs are fast. For Windows, it uses WinRM viapywinrm. - Module transfer. Ansible serializes the module (a Python script) and its arguments into JSON. It
scps orsftps that module to the managed node, usually into/tmp/.ansible/.... Yes, it lands on disk temporarily. - Execute and collect. The control node runs the module script via SSH. The module executes, makes changes (e.g., writes a config file), and returns a JSON result dict:
{ "changed": true, "msg": "file created" }. - Cleanup. The module script is deleted from the managed node. Ansible stores the result in memory for use in later tasks (via
register: result). - Report. Ansible formats the results (with colors, if enabled), prints them to stdout, and writes them to log files if configured.
This happens per task, per host. That's why a 50-host fleet with 20 tasks takes 1000 SSH round trips. Mitigation? Use pipelining=True to reduce SSH overhead — cuts execution time by up to 40%.
// io.thecodeforge — devops tutorial // Enable SSH pipelining in ansible.cfg for faster execution [ssh_connection] pipelining = True # Without pipelining: one SSH session per module # With pipelining: one SSH session per task batch # # Requirement: Managed nodes need: # /etc/ssh/sshd_config: # AllowTcpForwarding yes # PermitTTY yes # # Without these, pipelining silently falls back to sftp
MaxSessions on your control node. Default is 10. Bump it to 100 with ansible_ssh_common_args: '-o ControlMaster=auto -o ControlPersist=600s'. Otherwise Ansible will serialize connections and execute slower than a junior dev on Monday morning.Security and Compliance Enforcement — Automate Your Audits, Don't Just Check Boxes
Security isn't something you bolt on after deployment. It's either baked into your playbooks from the start or you're firefighting breaches. Compliance enforcement in Ansible means writing idempotent policies that fail closed, not open. The WHY: you need to prove to auditors that SELinux is enforcing, fail2ban is running, and SSH root login is disabled — without SSH'ing into every box manually.
The HOW: Use the assert module to gate your deployments. Check kernel parameters with sysctl, verify file permissions with stat, and enforce package versions with dpkg_selections. Combine this with failed_when conditions that halt execution if a security control is misconfigured. For compliance frameworks like CIS or PCI-DSS, write dedicated roles that map to control IDs. Then run these roles in check mode as part of your CI pipeline — your build should fail before a non-compliant node ever sees production.
Senior shortcut: Don't just check for the presence of a file. Verify its contents, owner, and permissions. Auditors love sha256sum comparisons. Give them receipts.
// io.thecodeforge — devops tutorial - name: Enforce CIS Benchmark — SSH and File Permissions hosts: all become: true vars: cis_controls: - id: "5.2.1" desc: "Ensure permissions on /etc/ssh/sshd_config are 600" path: /etc/ssh/sshd_config mode: '0600' owner: root - id: "5.2.2" desc: "Ensure SSH MaxAuthTries is <= 4" param: MaxAuthTries value: "4" tasks: - name: Assert file permissions match CIS control {{ item.id }} ansible.builtin.stat: path: "{{ item.path }}" loop: "{{ cis_controls | selectattr('path', 'defined') }}" register: file_stats - name: Fail deployment if permissions are wrong ansible.builtin.assert: that: - file_stat.stat.mode == item.mode - file_stat.stat.owner == item.owner fail_msg: "{{ item.desc }} — mode is {{ file_stat.stat.mode }}, expected {{ item.mode }}" loop: "{{ file_stats.results }}" when: file_stat.stat.exists | bool
ignore_errors: true on security checks. If you silence compliance failures, you're hiding breaches. Let the playbook burn — you'll thank yourself during the post-mortem.Dynamic Inventories — Stop Hardcoding Server Lists in 2025
Hardcoding IP addresses in a static inventory file is a rookie move that scales to exactly zero production environments. The WHY: cloud instances auto-scale, containers get recycled, and on-prem servers get migrated. Your inventory must reflect reality, not a stale text file someone committed six months ago. Dynamic inventories query your infrastructure provider (AWS, GCP, vSphere) and return live groups and variables.
The HOW: Ansible ships with inventory scripts for AWS EC2, Azure, GCP, OpenStack, and VMware. You point the -i flag at a script or use the aws_ec2 plugin with a YAML config. The plugin tags become your group names. Want to target all production web servers with the tag Environment:prod and Role:web? Ansible builds that group automatically. No manual maintenance. If a new instance spins up with the right tags, it's in the next playbook run. Dead instances? Dropped automatically.
Senior shortcut: Use the keyed_groups plugin option to create nested groups from tags or custom variables. This lets you write targeted playbooks like rolling_update:frontend without touching inventory files.
// io.theforge — devops tutorial plugin: aws_ec2 regions: - us-east-1 filters: tag:Environment: - prod - staging instance-state-name: running keyed_groups: - key: tags.Name prefix: instance_ - key: tags.Role prefix: role_ - key: tags.Environment prefix: env_ hostnames: - private-dns-name compose: ansible_host: private_ip_address ansible_user: ubuntu ansible_ssh_private_key_file: /etc/ansible/prod-key.pem
ansible-inventory -i aws_ec2.yml --list before running any playbook. Catch missing tags or wrong filters when it costs nothing.Provisioning — Why Infrastructure Must Exist Before Automation Runs
Ansible is often used to configure running systems, but those systems must first exist. Provisioning is the act of creating infrastructure — VMs, containers, network interfaces, storage volumes — before any playbook touches them. Without provisioning, your automation is solving a problem on a machine that doesn't exist. Ansible provisions through cloud modules: amazon.aws.ec2_instance, azure.azcollection.azure_rm_virtualmachine, or community.general.digital_ocean. These modules send API calls to your cloud provider, wait for resource creation, and return facts like IP addresses. Do not hardcode IPs. Use add_host to dynamically insert new nodes into the in-memory inventory for downstream playbooks. Production pattern: separate provisioning into its own playbook or role, run it first, then target the fresh hosts with configuration. This keeps creation logic separate from configuration logic, making both auditable and reusable. Idempotency matters here: your provisioning playbook should detect existing resources and skip creation, not fail or duplicate.
// io.thecodeforge — devops tutorial --- - name: Provision EC2 instance and add to live inventory hosts: localhost gather_facts: no tasks: - name: Launch EC2 amazon.aws.ec2_instance: name: "web-{{ env }}" instance_type: t3.micro image_id: ami-0abcdef1234567890 state: running tags: Environment: "{{ env }}" register: ec2 - name: Add new host to in-memory inventory ansible.builtin.add_host: name: "{{ item.public_ip_address }}" groups: webservers ansible_user: ec2-user loop: "{{ ec2.instances }}"
Orchestration — Coordinating Multi-Node Workflows That Fail Gracefully
Orchestration is about sequencing and dependencies across multiple hosts, not just running the same command everywhere. When one service must start only after another database is ready, or when you need a rolling update across 50 web servers without dropping traffic, you need orchestration. Ansible orchestration uses serial, order, throttle, and wait_for. For example, a three-tier app: provision load balancer, then app servers, then databases — each stage waits for the previous to pass health checks. Use delegate_to to run tasks from one host that check another. Use run_once for idempotent setup tasks (e.g., creating database schemas) that must execute only once across a group. For rolling updates, set serial: 1 or serial: 20% and include wait_for after restarts to verify service health before proceeding to the next batch. This pattern prevents cascading failures. Orchestration fails safely when you design for retries: set retries: 5 with delay: 10 on critical health checks.
// io.thecodeforge — devops tutorial --- - name: Rolling update of web servers hosts: webservers serial: 1 tasks: - name: Take server out of load balancer community.general.nginx_upstream: name: backend state: down server: "{{ inventory_hostname }}" delegate_to: lb01 - name: Update application ansible.builtin.git: repo: https://github.com/example/app.git dest: /var/www/app version: "{{ git_tag }}" - name: Restart web service ansible.builtin.systemd: name: nginx state: restarted - name: Wait for health check ansible.builtin.wait_for: port: 80 host: "{{ inventory_hostname }}" timeout: 30 - name: Re-add to load balancer community.general.nginx_upstream: name: backend state: up server: "{{ inventory_hostname }}" delegate_to: lb01
wait_for or uri module before proceeding to the next batch.Introduction
Ansible is a radically simple IT automation engine that eliminates manual toil and human error from infrastructure operations. Unlike configuration management tools that require agents installed on every node, Ansible operates over standard SSH—meaning your servers remain untouched until execution. This architecture makes Ansible uniquely suited for heterogeneous environments where installing a permanent daemon is impractical or prohibited by security policy. The core philosophy is 'mechanism, not magic': every operation is a straightforward YAML description of system state, not a cryptic DSL. For teams drowning in repetitive firewall updates, user account provisioning, or application deployments, Ansible offers a path to repeatability without complexity. Before evaluating playbooks or roles, understand that Ansible's primary value is reducing the cognitive load of fleet management. It transforms tribal knowledge into executable, version-controlled specifications. This article assumes you manage more than three servers—beyond that number, manual processes break. Ansible restores sanity by making automation a side effect of documentation.
// io.thecodeforge — devops tutorial
all:
hosts:
web01:
ansible_host: 10.0.1.10
web02:
ansible_host: 10.0.1.11
vars:
ansible_user: deploy
ansible_ssh_private_key_file: ~/.ssh/deploy_keyWhen Not to Use Ansible
Ansible excels at configuration management, application deployment, and task automation—but it is not a universal hammer. Avoid using Ansible for real-time event-driven automation where sub-second latency matters; tools like SaltStack or event-driven frameworks are better suited. Similarly, Ansible is not a container orchestrator—Kubernetes handles pod lifecycle and scaling natively. For stateful services requiring continuous convergence (e.g., ensuring a process stays running indefinitely), Ansible's push model falls short compared to a daemon-based tool like Puppet or Chef. Lastly, Ansible's Python dependency on control nodes can be a constraint in minimal environments like embedded systems or restricted CI runners. The golden rule: if your task fits in a cron job or a single shell script, Ansible is overkill. If you are managing 100+ servers with versioned, auditable state, Ansible is the right tool. Choose purpose-built tools for purpose-built problems; Ansible fills the midrange sweet spot between shell scripts and full-blown Kubernetes.
# When not to use Ansible: one-off ad-hoc commands # Use direct SSH or shell instead ansible all -i inventory.ini -m ansible.builtin.shell -a "uptime"
The Variable Precedence Nightmare
- Variable precedence is not a suggestion — it is a hard 22-level ladder that Ansible enforces silently. Learn the top eight levels. host_vars overrides group_vars. Always. Without exception.
- ansible-inventory --host is your variable debug command. Run it against the specific failing host before touching the playbook. The resolved variable state is the ground truth — not what you think you set.
- Treat host_vars files as a code smell. Unless a host genuinely needs unique configuration that no other host in its group shares, keep variables at group level and delete host_vars files when the reason for them disappears.
- Your staging environment not mirroring production in inventory structure and variable sources is a disaster waiting to happen. The variable that breaks prod will always be the one that staging silently resolved differently.
ansible -i inventory.ini all -m ping -vvvssh -v -i ~/.ssh/your_key user@target_host echo connectedansible-playbook playbook.yml --check --diff > /tmp/ansible_diff.txtgrep -B 5 -A 15 'changed:' /tmp/ansible_diff.txtansible-inventory -i inventory.ini --host $TARGET_HOST --vars | jq '.nginx_port, .environment, .db_password'ansible -m debug -a 'var=nginx_port' -i inventory.ini $TARGET_HOSTansible-playbook playbook.yml --list-tasks | grep -A 5 handler_namegrep -r 'notify: handler_name' roles/ --include='*.yml'env | grep -E 'ANSIBLE|PYTHON|SSH' > local_env.txtansible --version && python3 --version| Tool | Agent Required | Language | Learning Curve | Best For |
|---|---|---|---|---|
| Ansible | No (agentless — SSH only) | YAML + Jinja2 | Low — most engineers are productive within a day | Configuration management, application deployment, ad-hoc fleet operations, and orchestration across mixed environments. The fastest path from zero automation to everything automated. Best choice for teams that don't have dedicated infrastructure engineers. |
| Chef | Yes (chef-client daemon running on every managed node) | Ruby DSL | High — requires Ruby knowledge and Chef Server administration | Complex, policy-based configuration in large enterprise fleets where teams have Ruby expertise and need a pull-based model. Chef Server handles 10,000+ nodes better than Ansible's push model at extreme scale. |
| Puppet | Yes (puppet agent daemon, certificate-based auth) | Puppet DSL | High — Puppet DSL is its own language with its own idioms | Long-term compliance enforcement and drift remediation in regulated industries (finance, healthcare, government) where continuous automated enforcement matters more than on-demand execution. Puppet's pull model means servers self-correct without a human initiating a run. |
| Terraform | No | HCL | Medium — HCL is readable but state management has a learning curve | Infrastructure provisioning — creating servers, VPCs, load balancers, DNS records, IAM roles, and managed services. Complementary to Ansible, not a replacement. Terraform creates the server. Ansible configures it. Most mature DevOps teams use both in sequence: Terraform provisions, Ansible configures on first boot and on every subsequent config change. |
| File | Command / Code | Purpose |
|---|---|---|
| precedence_demo.yml | - name: Demonstrate variable precedence | How Ansible Variable Precedence Really Works |
| io | [webservers] | Inventory, Playbooks, and Modules |
| io | - name: Deploy and Configure Nginx | Your First Production Playbook |
| io | ansible production -i inventory.ini -m ping | Ad-hoc Commands |
| io | - name: Install Nginx | Roles |
| io | - name: Deploy Application with Safety Rails | Production Patterns |
| idempotency_demo.yml | - name: Ensure Nginx is at the right state | Key Features of Ansible |
| minimal_architecture_inventory.yml | [webservers] | Ansible Architecture |
| pipelining_config.yml | [ssh_connection] | How Ansible Works |
| enforce-cis-benchmark.yml | - name: Enforce CIS Benchmark — SSH and File Permissions | Security and Compliance Enforcement |
| aws_ec2_inventory.yml | plugin: aws_ec2 | Dynamic Inventories |
| provision-aws-ec2.yml | - name: Provision EC2 instance and add to live inventory | Provisioning |
| rolling-update.yml | - name: Rolling update of web servers | Orchestration |
| inventory.yml | all: | Introduction |
| cli_example.sh | ansible all -i inventory.ini -m ansible.builtin.shell -a "uptime" | When Not to Use Ansible |
Key takeaways
Common mistakes to avoid
6 patternsUsing ignore_errors: yes as a band-aid for tasks that matter
Committing plain-text secrets to version control
Using shell or command modules when a dedicated module exists
Not disabling host key checking in CI/CD environments
Forgetting become: true and spending an hour debugging the wrong thing
Ignoring YAML indentation and spending time on cryptic parse errors
Interview Questions on This Topic
Explain the agentless architecture of Ansible. How does it compare to agent-based tools like Puppet or Chef in terms of security footprint, operational overhead, and onboarding friction for new servers?
What is idempotency in the context of Ansible modules? Can you name a module that is not idempotent by default, and explain when you'd intentionally use it?
How does Ansible handle parallel execution? What is a fork in ansible.cfg, and how does tuning it impact performance on a 500-node fleet?
What is the difference between a task and a handler? In what scenario would a handler be skipped even if it is notified by a task that reported changed?
How would you use Ansible Vault to manage environment-specific secrets in a CI/CD pipeline? Walk through the workflow from encrypting the variable to injecting it during a Jenkins or GitLab CI run.
What are Ansible facts? How can you disable fact gathering to speed up playbook execution, and when do you actually need them?
Explain how dynamic inventory works with a cloud provider like AWS. What are the advantages over a static inventory file, and what challenges does it introduce?
Describe the difference between include_role and import_role. When would you choose one over the other, and how does each affect task execution order and variable scope?
How would you structure an Ansible project to manage 500+ servers across dev, staging, and production environments? Describe your directory layout, variable hierarchy, and how you'd prevent production changes from accidentally running against dev.
Frequently Asked Questions
An ad-hoc command is a single one-liner executed directly from the command line — ideal for quick checks or one-off operations like restarting a service or checking disk space across your fleet. A playbook is a reusable, version-controlled YAML file that defines a sequence of tasks with variables, handlers, and error handling. Think of ad-hoc commands as shouting instructions across the room, and playbooks as writing a detailed runbook that anyone can execute repeatedly with the same result. The rule of thumb: if you've run the same ad-hoc command twice, it belongs in a playbook.
Ansible provides Ansible Vault, which encrypts variables or entire files using AES256. Encrypt individual strings with ansible-vault encrypt_string and paste them into your playbooks, or encrypt entire variable files with ansible-vault encrypt. At runtime, provide the vault password via --vault-password-file pointing to a file written from a CI secret. Vault-encrypted content is safe to commit to Git — without the password it's gibberish. For larger teams, integrate Vault with HashiCorp Vault using the hashi_vault lookup plugin, which fetches secrets at runtime from a centralized secrets manager rather than storing them in encrypted files.
Dynamic inventory queries an external source — typically a cloud provider API like AWS EC2, GCP, or Azure — at runtime instead of reading a static file. Ansible builds the host list from live API data based on tags, regions, and instance states. Use dynamic inventory when your infrastructure is elastic: autoscaling groups, spot instances, or any environment where servers are created and destroyed regularly. Static inventory works for fixed infrastructure under 20 servers with stable hostnames. Beyond that, a static file becomes a liability — stale IPs, missing new instances, terminated hosts that are still listed. Enable the inventory cache (cache_timeout: 300) to avoid rate limiting the cloud API on every run.
Ansible provides a block/rescue/always construct that works like try/catch/finally. Wrap risky operations in a block. If any task inside fails, the rescue section executes — rollback to a known-good state, send an alert, log the failure context. The always section runs regardless of success or failure — cleanup, status notifications. For rolling deployments, combine this with serial (how many hosts to update at once) and max_fail_percentage (abort the entire deploy if too many hosts fail). Set max_fail_percentage: 0 for database migrations — any failure should stop everything. Without block/rescue, a failed migration on server 3 of 20 leaves 17 servers on the new schema and 1 on the old, with the application broken and no automatic recovery.
They solve different problems at different points in a server's life. Terraform provisions infrastructure — it creates EC2 instances, VPCs, load balancers, DNS records, and IAM roles. Ansible configures that infrastructure — it installs software, deploys application code, manages services, and corrects configuration drift. Terraform's user_data and cloud-init can run a script at first boot, but they can't re-run idempotently three months later when you need to update a config file. Ansible can. Most production teams use Terraform to build the infrastructure and Ansible to configure and maintain it. They're complementary tools in the same pipeline, not alternatives.
Use --check mode for a dry run — Ansible shows what would change without applying anything. Combine it with --diff to see exact file content differences. For automated testing, use Molecule: it spins up Docker containers or VMs, runs your role, verifies the result with Testinfra assertions, and tears everything down. Run Molecule in CI to catch regressions before they reach any environment. Also run ansible-lint on all playbooks and roles to catch deprecated modules, style violations, and common structural mistakes. The combination of --check, --diff, Molecule, and ansible-lint catches the vast majority of problems before a human needs to review them.
Ansible Galaxy is a repository of community-contributed roles for common infrastructure software — Nginx, Docker, PostgreSQL, certbot, Redis, and hundreds more. Install with ansible-galaxy install -r requirements.yml. Community roles save hours for commodity software and are often more battle-tested than what you'd write from scratch. For application-specific automation — deploying your Java app, configuring your monitoring stack — write custom roles. The mandatory practice: pin every Galaxy role to a specific version in requirements.yml. A community role is a dependency you don't control. A minor version update can change default behavior in ways that affect production. Pin it, test upgrades in staging, read the changelog before bumping the version.
Ansible's parallelism scales with the forks setting in ansible.cfg (default: 5, which is too low for large fleets). For 1000 servers, start at forks=50 and monitor control node CPU, memory, and open file descriptor counts. Enable pipelining=True to reduce SSH round-trips per module from 3 to 1 — this alone can cut playbook runtime by 30-40%. Disable fact gathering for playbooks that don't need system facts, or use gather_subset=min to collect only essential information. For operational visibility at scale — job scheduling, RBAC, audit logging, workflow orchestration, and a web UI — deploy AWX (the open-source version) or Ansible Automation Platform. Plain Ansible from the command line works at 1000+ nodes, but AWX gives you the operational control that large teams need to manage concurrent jobs safely.
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
That's Ansible. Mark it forged?
14 min read · try the examples if you haven't