Skip to Content
Mengty Learns
Platform tools

Ansible Essentials

Agentless host configuration and rolling VM deployments—inventory, variable precedence, idempotency, handlers, and safe rollouts.

referenceintermediateTested with ansible-core 2.22 (docs)Updated Nov 27, 2026

TL;DR

Ansible is agentless push over SSH with no state file: it owns host configuration and VM rollouts, while Terraform/OpenTofu owns infrastructure lifecycle. Always dry-run with --check --diff, put overridable variables in defaults/main.yml, bound rollouts with serial + max_fail_percentage: 0, and test roles for idempotency.

Daily commands

# Inspect inventory (not run)
ansible-inventory -i inventories/prod --graph
ansible-inventory -i inventories/prod --host web-01

# Dry-run with diff before any production change (not run)
ansible-playbook -i inventories/prod playbooks/site.yml --check --diff
ansible-playbook -i inventories/prod playbooks/deploy.yml --limit web-01 -e "app_version=1.4.2"

# Syntax and lint gates for CI (not run)
ansible-playbook --syntax-check playbooks/site.yml
ansible-lint playbooks/ roles/

Variable precedence (the trap)

From lowest to highest (the layers that matter daily):

  1. Role defaults/main.yml (lowest — put overridable values here)
  2. Inventory group_vars/all → specific group_vars → host_vars
  3. Play vars
  4. Role vars/main.yml (beats inventory!)
  5. set_fact / register
  6. Extra vars -e (highest — overrides everything, including safety defaults)

Rolling VM deploy pattern

# Rolling update: 25% at a time, stop on first failure (not run)
- name: Deploy app to web tier
  hosts: web
  become: true
  serial: "25%"
  max_fail_percentage: 0
  tasks:
    - name: Deploy and verify health
      block:
        - name: Drain from load balancer
          ansible.builtin.command: lb-cli drain {{ inventory_hostname }}
          delegate_to: localhost
        - name: Deploy new artifact
          ansible.builtin.template:
            src: app.service.j2
            dest: /etc/systemd/system/app.service
          notify: Restart app
        - name: Flush handlers before health check
          ansible.builtin.meta: flush_handlers
        - name: Wait for healthy endpoint
          ansible.builtin.uri:
            url: "http://{{ ansible_host }}:8080/healthz"
            status_code: 200
          retries: 10
          delay: 3
      always:
        - name: Re-enable in load balancer
          ansible.builtin.command: lb-cli enable {{ inventory_hostname }}
          delegate_to: localhost

Gotchas

  • Disabling host_key_checking in prod: Turning off SSH host-key verification exposes your management path to MITM attacks; populate known_hosts from provisioning outputs instead.
  • Unflushed handlers on failure: If a play aborts before the end, notified handlers never run—leaving new config on disk while the old process keeps running. Use meta: flush_handlers before health checks.
  • Overlapping with Terraform: Never manage the same firewall rule or security group in both Terraform and Ansible; let Terraform own infrastructure lifecycle and Ansible own OS/app configuration.
  • Forgetting no_log: true: Tasks that render tokens or passwords print their parameters to stdout and CI logs unless no_log: true is set.

Production checklist

  • host_key_checking = True in ansible.cfg for all non-local inventories.
  • Every role passes ansible-lint and a two-pass idempotency test (e.g. Molecule).
  • Production rollouts use serial + max_fail_percentage: 0 + block/always LB drain.
  • Secrets come from vault-agent or hashi_vault lookups with no_log: true, never plaintext group_vars.

Check yourself

Check Yourself

1. You set `http_port: 8080` in `roles/web/vars/main.yml` and `http_port: 9090` in `inventories/prod/group_vars/web.yml`. Which value does Ansible use?

2. Why should you call `meta: flush_handlers` before running a post-deploy URI health check?

Sources