Ansible Tutorial
Learn Ansible step by step: 29 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Ansible? Learn what Ansible is, how its agentless push model works over SSH, why idempotence and desired state matter, and when to pick it over shell scripts.
- Installing Ansible and Your First Ping Install Ansible with pip, pipx or a package manager, learn ansible-core versus ansible, set up a lab host, and run your first ping against localhost and SSH.
- Inventory: Hosts and Groups Learn Ansible inventory files in INI and YAML: groups, children, ranges, host patterns, ansible-inventory, group_vars and host_vars, and dynamic inventory.
Core Concepts
- Ad-Hoc Commands Run one-off tasks on many servers with Ansible ad-hoc commands: ping, command, shell, copy, apt and service, plus -b, -l, -i and forks, and when to use them.
- Playbooks and YAML Essentials Write your first Ansible playbook: YAML rules and traps, plays, tasks, ansible-playbook, --check and --diff, and how to read the output and the play recap.
- Modules and Idempotence Understand why Ansible modules beat shell commands: ok, changed and failed states, command vs shell vs raw, creates and removes, changed_when, failed_when and check mode.
Variables and Logic
- Variables, Facts and Precedence Learn how Ansible variables work: vars, group_vars, host_vars, extra vars, gathered facts, set_fact, register, debug and the precedence rules that decide which value wins.
- Conditionals and Loops Control which Ansible tasks run with when, repeat tasks over lists with loop, retry with until, and avoid the truthiness and registered-result pitfalls that trip up beginners.
- Jinja2 Templates and Filters Use Jinja2 in Ansible: {{ }} expressions, {% %} blocks, filters like default, join, map and selectattr, the template module, lookups, whitespace control and YAML quoting rules.
Structuring Automation
- Handlers, Tags and Task Control Run Ansible actions only when something changed with handlers, select tasks with tags, handle failures with block/rescue/always, and control execution with delegate_to, run_once and serial.
- Roles and Project Layout Organize Ansible automation with roles: the role directory structure, defaults versus vars, handlers and templates, import_role versus include_role, ansible-galaxy init and a repo layout.
- Collections, Galaxy and Reusable Content Understand Ansible collections and fully qualified names (FQCN), install content with ansible-galaxy and requirements.yml, pin versions, and package a minimal role for sharing.
Everyday Modules
- Files, Users and Packages Manage files, users and packages with Ansible: copy, template, file, lineinfile, blockinfile, user, group, apt/dnf, get_url, unarchive and authorized_key, with idempotent examples.
- Services, Commands and Scheduling Control services with service and systemd, schedule jobs with cron, run commands safely, wait for ports, check health with uri, reboot hosts and restart through handlers.
- Windows, Network Devices and Cloud Modules Automate Windows over WinRM or SSH, network devices with network_cli and resource modules, and cloud APIs from localhost. Learn connection types and collections.
Secrets and Security
- Ansible Vault and Secrets Keep passwords and keys safe in Ansible: ansible-vault create, encrypt and edit, encrypt_string, vault IDs, password files, no_log, secret lookups and git hygiene.
- SSH, Privilege Escalation and Hardening Secure Ansible access: SSH keys and agents, ansible.cfg connection settings, become and become_user, least privilege, host key checking and a server hardening playbook.
Real-World Practice
- Testing, Linting and Debugging Test Ansible automation: syntax-check, check and diff mode, ansible-lint, yamllint, Molecule with containers, assert and fail, verbosity levels, the debugger and a CI pipeline.
- Performance, Dynamic Inventory and AWX Make Ansible faster and scale it to big fleets: forks, pipelining, strategies, async tasks, fact caching, dynamic inventory plugins, ansible-pull and AWX.
- Ansible with Docker, Terraform and CI/CD Combine Ansible with Terraform, Docker and CI pipelines: provision then configure, community.docker modules, idempotent deploys and rolling updates with serial.
Projects
- Project: Provision a Web Server Build a complete Ansible playbook that installs nginx, renders a site config from a template, opens the firewall, reloads safely with handlers and verifies the result.
- Project: Deploy an Application with Rolling Updates Deploy an application with Ansible using release directories, an atomic symlink swap, a systemd unit template, serial rollout, health checks and automatic rollback.
- Project: Security Baseline Role Write a reusable Ansible role that applies a security baseline: SSH settings, admin users and sudoers, unattended upgrades and auditing, with tags, check mode and defaults.
- Project: Multi-Tier Stack with Roles and Vault Build a load balancer, app servers and a database with Ansible roles, separate dev and prod inventories, group_vars, Vault secrets, play ordering and delegate_to.
- Project: Build a Mini Ansible in JavaScript Build a tiny Ansible in JavaScript: parse an inventory and a playbook, register modules, support check mode, notify handlers and print a play recap. Runs in your browser.
Practice and Review
- Exercises with Solutions Practice Ansible with 12 graded exercises on inventory, host patterns, ad-hoc commands, playbooks, templates, handlers, roles, vault and rolling updates, with full solutions.
- Common Mistakes and Troubleshooting Fix the Ansible problems everyone hits: YAML indentation and quoting errors, unreachable hosts, become failures, non-idempotent shell tasks, variable precedence surprises.
- Interview Questions Prepare for an Ansible interview: concepts, idempotence, roles vs collections, vault, handlers, variable precedence, scenarios and troubleshooting questions with clear answers.
- Ansible Cheat Sheet One-page Ansible cheat sheet: CLI flags, inventory patterns, playbook keywords, common modules, Jinja2 filters, project layout, magic variables and ansible-vault commands.