Learn Ansible

Agentless automation for configuring servers and deploying software over SSH

18 lessons · 3 hours · 4 levels · covers Ansible ansible-core 2.21

Ansible automates server configuration, application deployment and routine operations by running idempotent tasks, written as YAML playbooks, on many machines over SSH with nothing to install on them. It is widely used to configure VMs and bare metal, often after Terraform has created them, and to manage network devices.

Before you start

Ansible builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics6 lessons · 55 min in total
2. PracticalUse it productively at work, with the usual idioms11 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs15 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know18 lessons · 3 hours in total

The Ansible tree

AnsibleWhat Ansible is: agentless and idempotentWhat Ansible is:agentless and idem…Inventory: hosts and groupsInventory: hosts andgroupsAd-hoc commands and core modulesAd-hoc commandsand core modulesWriting your first playbookWriting your firstplaybookVariables, facts and registered resultsVariables, facts andregistered resultsHandlers and change notificationHandlers and changenotificationansible.cfg, SSH and privilege escalationansible.cfg, SSH andprivilege escalationJinja2 templates and filtersJinja2 templates andfiltersConditionals and loopsConditionals andloopsRoles and project layoutRoles and projectlayoutSecrets with Ansible VaultSecrets with AnsibleVaultGenerating SSH key pairsGenerating SSH keypairsssh-agent: unlock once, use all dayssh-agent: unlockonce, use all day~/.ssh/config: hosts and aliases~/.ssh/config: hostsand aliasesConnection multiplexing (ControlMaster)Connectionmultiplexing (Contr…
Essentials and Practical shown (11 of 18 lessons); the full list is below. Click a lesson to read it.

All 18 lessons, in order

Basics

  1. What Ansible is: agentless and idempotent · Essentials · 7 min

    Ansible connects over SSH, runs small idempotent modules on each host and reports what changed.

  2. Inventory: hosts and groups · Essentials · 10 min

    List managed hosts and groups in INI or YAML inventories, with per-host connection details and group nesting.

  3. Ad-hoc commands and core modules · Essentials · 8 min

    Run single modules across hosts from the command line and get to know the builtin modules you'll use daily.

  4. ansible.cfg, SSH and privilege escalation · Practical · 10 min

    Configure Ansible with ansible.cfg, tune how it connects over SSH, and use become safely.

Playbooks

  1. Writing your first playbook · Essentials · 12 min

    Structure plays with hosts, become and tasks, run them with ansible-playbook and read the recap.

  2. Variables, facts and registered results · Essentials · 10 min

    Define variables, read gathered facts about hosts, and capture task results with register.

  3. Handlers and change notification · Essentials · 8 min

    Restart or reload services only when something actually changed, using notify and handlers.

  4. Jinja2 templates and filters · Practical · 12 min

    Generate configuration files from Jinja2 templates with variables, loops, conditionals and filters.

  5. Conditionals and loops · Practical · 10 min

    Run tasks only when conditions hold with when, and repeat them over lists and dicts with loop.

  6. Secrets with Ansible Vault · Practical · 8 min

    Encrypt secret variables and files with ansible-vault so they can live safely in git.

Roles and reuse

  1. Roles and project layout · Practical · 12 min

    Package tasks, handlers, templates, files and defaults into reusable roles and compose playbooks from them.

  2. Variable precedence and scoping · Advanced · 10 min

    Know which of Ansible's many variable sources wins, and organise variables so the answer is obvious.

  3. Collections and Galaxy · Advanced · 8 min

    Install and pin collections and roles with requirements.yml, and understand how content is namespaced.

Reliability and testing

  1. Error handling, blocks and changed_when · Advanced · 10 min

    Define what counts as failure and change, group tasks in blocks, and recover with rescue and always.

  2. Check mode, linting and Molecule · Advanced · 10 min

    Preview changes with --check --diff, enforce best practices with ansible-lint, and test roles with Molecule.

Scale and internals

  1. Performance: forks, pipelining, async · Expert · 12 min

    Speed up large runs with forks, SSH multiplexing, pipelining, fact caching, the free strategy and async tasks.

  2. Dynamic inventory plugins · Expert · 10 min

    Build inventory from cloud APIs and other sources with inventory plugins, and shape groups with keyed_groups and constructed.

  3. How tasks execute: modules, plugins, templating · Expert · 14 min

    Follow a task from parsing to AnsiballZ on the target, distinguish modules from plugins, and know the 2.19+ templating rules.