Free course · Beginner
Ansible: Automate Everything from First Playbook to Production
Learn Ansible from scratch and build production-ready automation: inventory files and dynamic groups, ad-hoc commands, playbooks with handlers and conditionals, roles with Ansible Galaxy, Jinja2 templates, variables and vaults for secrets, idempotent deployments of web servers and databases, rolling updates with serial and health checks, and a CI pipeline that runs ansible-lint and deploys on merge. Every lesson runs real Ansible commands against local containers so you see exactly what changes.
1. Foundations: Installation, Inventory and Ad-hoc Commands
Install Ansible, write an inventory that groups your servers by role, and run ad-hoc commands to check connectivity, install packages and copy files — all without writing a single playbook.
- Install Ansible and Configure Your Control Node — Install Ansible via pip, verify the version, understand the control-node / managed-node split, and create an ansible.cfg that points at your inventory.
- Inventory Files: Hosts, Groups and Variables — Write INI and YAML inventories that group servers by role, set per-host and per-group variables, and use patterns to target subsets of your infrastructure.
- Ad-hoc Commands: ping, shell, copy, apt — Run one-off tasks with the `ansible` command: check connectivity, execute shell commands, copy files, install packages and manage services — no playbook required.
2. Playbooks: Tasks, Handlers and Conditionals
Write your first playbook to install and configure Nginx, use handlers so services restart only when config actually changes, and add conditionals, loops and error handling so playbooks adapt to different hosts.
- Your First Playbook: Install and Start Nginx — Write a YAML playbook that installs Nginx, deploys a default page and starts the service, then run it twice to see idempotency in action.
- Handlers: Restart Services Only When Config Changes — Use handlers and notify so Nginx restarts only when its configuration file actually changes — not on every playbook run.
- Conditionals, Loops and Registered Results — Use `when` to skip tasks on the wrong OS, `loop` to install multiple packages in one task, `register` to capture command output, and `block/rescue/always` for error handling.
3. Variables, Facts and Jinja2 Templates
Master the data layer: variable precedence that controls what wins, gathered facts that describe every host, and Jinja2 templates that turn one config file into environment-specific deployments.
- Variables: Defaults, Vars, Extra-vars and Precedence — Define variables at every level — inventory, playbook, role defaults, role vars, extra-vars — and predict which value wins when two levels set the same name.
- Gathering Facts: System Info and Custom Facts — Use Ansible facts to make playbooks adapt to each host's OS, memory, IP address and disk layout without hardcoding anything.
- Jinja2 Templates: Config Files That Adapt to Each Host — Write a single Jinja2 template that produces the right nginx or application config for every environment, using variables, conditionals, loops and filters.
4. Roles and Ansible Galaxy
Package automation into reusable roles with a standard directory structure, pull community roles and collections from Galaxy, and combine them into a full-stack deployment.
- Roles: Reusable Automation Packages — Organise tasks, handlers, templates, variables and files into a role that any playbook can include — the standard unit of reuse in Ansible.
- Ansible Galaxy: Install and Use Community Roles and Collections — Pull tested community automation from Galaxy, pin versions in a requirements file, and use collection-namespaced modules for reproducible builds.
- Project: Deploy a Web Application with Roles — Combine everything: a base role, a webserver role and a deploy role that together install packages, configure Nginx from a template, pull application code and restart services — across staging and production.
5. Vault, Security and Secrets Management
Encrypt secrets with Ansible Vault, harden servers with a security playbook, and pull credentials from external stores at deploy time.
- Ansible Vault: Encrypt Secrets in Your Repository — Encrypt sensitive variables and files so secrets live alongside code without being readable in plain text.
- Security Hardening Playbook: SSH, Users and Firewall — Write a playbook that locks down a fresh server: SSH hardening, a deploy user with key-only sudo, firewall rules, and fail2ban.
- Integrating External Secret Stores: AWS SSM and HashiCorp Vault — Pull secrets from AWS Parameter Store or HashiCorp Vault at deploy time instead of storing them in encrypted files.
6. Production Patterns: Testing, Rolling Updates and CI/CD
Test your automation before it touches servers, deploy with zero downtime using rolling updates, and run Ansible from a CI/CD pipeline.
- Testing Ansible Code: ansible-lint, Molecule and Check Mode — Catch mistakes before they reach servers: lint rules for style, Molecule for integration testing, and check mode for dry runs.
- Rolling Updates: serial, max_fail_percentage and Health Checks — Deploy to servers in batches with automatic rollback if too many fail, and drain traffic from each batch before updating.
- CI/CD Pipeline: Run Ansible from GitHub Actions — Set up a GitHub Actions workflow that lints on PRs, dry-runs against staging on merge, and deploys to production with a manual approval gate.