Ansible Essentials
Agentless host configuration and rolling VM deployments—inventory, variable precedence, idempotency, handlers, and safe rollouts.
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):
- Role
defaults/main.yml(lowest — put overridable values here) - Inventory
group_vars/all→ specificgroup_vars→host_vars - Play
vars - Role
vars/main.yml(beats inventory!) set_fact/register- 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: localhostGotchas
- Disabling
host_key_checkingin prod: Turning off SSH host-key verification exposes your management path to MITM attacks; populateknown_hostsfrom 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_handlersbefore 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 unlessno_log: trueis set.
Production checklist
-
host_key_checking = Trueinansible.cfgfor all non-local inventories. - Every role passes
ansible-lintand a two-pass idempotency test (e.g. Molecule). - Production rollouts use
serial+max_fail_percentage: 0+block/alwaysLB drain. - Secrets come from
vault-agentorhashi_vaultlookups withno_log: true, never plaintextgroup_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
Platform Security Baseline: Hosts, Clusters, and Exceptions
Enforce a practical, auditable security baseline across Linux hosts (CIS, SELinux, firewalld, auditd, AIDE) and Kubernetes namespaces (PodSecurity, NetworkPolicy, Kyverno).
Nginx Edge & Reverse Proxy
Production Nginx patterns—graceful reloads, upstream keepalive pools, proxy_pass trailing-slash rules, security headers, rate limiting, and 499/502/504 triage.