Every level, in the open
Ansible
5 worlds · 50 levels · 5 bosses
SOLUTION HIDDENTeachers see answer keys in the classroom.
One order, a whole fleet: inventories, playbooks, variables, Jinja2 templates, handlers, roles, Galaxy and Vault, then a real fleet on the lab server. Honest note: in the browser the Ansible tools are SIMs (vault files use the real AES256 format); The Real Fleet runs the real ones.
Guided levels show the command first. Alone levels give the goal only. Bosses and exams are timed.
Jump to a world
World 1The Roster
11 levelsA fleet of servers, one list of names. Learn to describe your machines in an inventory and give all of them the same order with one command.
Level 1: Roll call
Objective: Ping every host in ~/ansible/roster/inventory.ini with Ansible and tell Kern which one stays silent.
Ansible runs on ONE machine (the controller) and reaches the others over plain ssh, with no agent on them. The inventory lists who. hidden: 'pong' = ssh and python work, UNREACHABLE = they do not.
Commands taught
ansibleansible-simLevel 2: Who is in web?
Objective: How many hosts are in the web group of inventory.ini? Tell Kern the number.
An INI inventory lists one host per line under a [group] header. A host can sit in several groups. ansible GROUP --list-hosts prints a group's members and ansible-inventory --graph draws the whole tree.
Commands taught
ansible-inventoryinventory-iniLevel 3: A new server
Objective: web4 has just been racked. Add it to the web group in inventory.ini (db stays as it is).
A host belongs to the group whose [header] is above its line. To add a server, put its name under the right header, then check with --list-hosts. A line placed at the end of the file lands in whatever section comes last.
Level 4: Groups of groups
Objective: Make a group called prod that contains the web group and the db group.
A group can contain other groups: put the section [prod:children] and list group names under it. prod then holds every host of web and db. A [prod] section (no :children) would list HOSTS named web and db, which is a classic mistake.
Level 5: Which user?
Objective: The ping fails with Permission denied. Fix the inventory (or ansible.cfg) so a plain ping works for all three hosts without extra flags.
Ansible logs in as YOUR local user unless told otherwise, but these servers only allow deploy. Set ansible_user=deploy on a host, under [all:vars], or remote_user = deploy in ansible.cfg. Typing -u deploy works once and is saved nowhere.
Commands taught
ansible-connectionLevel 6: Ask the whole group
Objective: One web server names a different datacenter in /etc/motd than the others. Find it with one ad-hoc command and tell Kern the odd datacenter name.
Ad-hoc: ansible GROUP -m MODULE -a 'ARGS'. The default module is command, so ansible web -a 'cat /etc/motd' runs the command on every web server and prints each answer under its host name. Perfect for quick questions and one-off facts.
Commands taught
ansible-modules-adhocLevel 7: Ask for the keys
Objective: Create the empty file /etc/ready on the web servers with -b, then remove it again everywhere.
You log in as deploy, an ordinary user, so /etc and /var/www are off limits. -b (become) tells Ansible to switch to root on the server for this command (sudo), after logging in as deploy.
Commands taught
ansible-becomeLevel 8: Not that one
Objective: Create the flag file /etc/maintenance on web1 and web2 only: web3 is still serving customers and the db must not get it. One ansible command.
Patterns pick hosts: web, web:db (either), web:&canary (both), web:!web3 (web except web3), web1,web2 (listed hosts). Quote patterns with ! in single quotes so the shell leaves them alone. The file module with state=touch creates an empty file; writing in /etc needs root: add -b.
Commands taught
host-patternsLevel 9: Drop a file everywhere
Objective: Write the page Under maintenance (exactly that text) into /var/www/html/index.html on every web server, one command. The db server gets nothing.
The copy module puts a file on the servers: content= writes text you give, src= copies a file from the controller, dest= is the path on the server. Unlike command it is idempotent: run it twice and the second run says ok, not changed.
Level 10: Lock the door
Objective: SECURITY: set PermitRootLogin no in /etc/ssh/sshd_config on ALL four servers (web and db) without touching the other lines.
lineinfile makes sure one line exists in a file: regexp= finds the old line, line= is what it should become. Without regexp it just appends, and you end up with two conflicting lines. Everything else in the file stays untouched.
Level 11: Onboard the new fleet
Objective: Read TICKET, then bring the new fleet under Ansible: inventory, a green ping, chrony installed everywhere, password logins off on every server except cache1.
A new fleet arrives with one ticket and no inventory. Build the inventory, get every host to answer, then give them orders: install a package everywhere, change one setting on all but one host.
World 2The Scroll
10 levelsCommands typed in a terminal are gone tomorrow. Write them in YAML and the fleet can be set up again and again, the same way, by anyone.
Level 12: Indent or die
Objective: site.yml has one line indented wrongly. Find it with yamllint, fix it, and make hidden pass.
Playbooks are YAML: structure IS indentation, spaces only (never tabs). Siblings start in the same column; nested lines sit further right. yamllint FILE finds syntax errors; ansible-playbook FILE --syntax-check checks the playbook without touching a server.
Commands taught
yamllintyaml-basicsansible-playbookLevel 13: Maps and lists
Objective: Write crew.yml with exactly the data in TICKET: a team, an on-call list in that order, and limits with numbers (not text).
YAML has three building blocks: a map (key: value pairs), a list (one '- item' per line) and scalars (text, numbers, true/false). 2 is a number, "2" is text. A list sits under its key, indented. [a, b] and {k: v} are the one-line forms.
Level 14: First playbook
Objective: Write site.yml with one play for the web group and one task that uses the ping module, then run it with ansible-playbook (inventory is in ansible.cfg).
A playbook is a list of plays. A play says which hosts and what tasks; a task is a module call with a name. Run it with ansible-playbook FILE. The output has a PLAY, a TASK per step with ok/changed per host, and a PLAY RECAP: the report card.
Commands taught
playbook-anatomyLevel 15: Make a folder
Objective: Write mkdir.yml: on the web servers create the folder /opt/shop with mode 0755, then run it.
Module arguments are a map under the module name: file: with path:, state: and mode:. state: directory means 'this folder must exist'. Quote modes ("0755") so YAML keeps them as text. Folders under /opt are root's: add become: true to the play.
Commands taught
playbook-modulesLevel 16: Each server signs its name
Objective: Write version.yml so each web server gets /opt/shop/VERSION saying: served by <its own hostname> (web1 says web1). The folder exists. Run it.
Facts are data each server reports at the start (Gathering Facts), like ansible_hostname. Use a variable as {{ name }}. A value that STARTS with {{ must be quoted, or YAML takes it for a map and fails.
Commands taught
ansible-factsLevel 17: Run it twice
Objective: ready.yml reports changed on every run. Fix it so the first run builds /opt/shop/READY on both web servers and the SECOND run reports changed=0. Run it twice.
Idempotent = running again changes nothing: changed=0 on the second run is a healthy playbook. command and shell can't know the state, so they say changed every time unless you add creates: (skip if this path exists) or use a real module.
Commands taught
idempotenceLevel 18: Look before you leap
Objective: deploy.yml would rewrite /etc/motd. Without changing any server, find out how many web servers would change and tell Kern the number.
--check (-C) is a dry run: modules report what WOULD change and change nothing. Read the recap: changed=1 means 'would change'. It's the safe preview before every production run (it's not perfect for command/shell tasks, which are skipped).
Commands taught
check-modeLevel 19: Change one line
Objective: Write tune.yml: each web server's /etc/app/app.conf gets exactly one log_level = warn line (other lines stay). Not the db. Run it.
lineinfile in a playbook: path:, regexp: (finds the old line) and line: (the final line). It replaces the matching line, or appends one when nothing matches, and leaves the rest of the file alone. Without regexp you get duplicates.
Level 20: The nightly that broke
Objective: PAGER: nightly.yml fails. Fix it until hidden succeeds and each web server has /opt/shop/CONFIG containing its own hostname.
Fix errors one at a time, in the order Ansible finds them: YAML syntax first (yamllint, --syntax-check), then unknown modules and keys, then runtime errors on the servers (Permission denied → become). Read the first error, fix it, run again.
Level 21: Ship release 17
Objective: Read TICKET and ship release 17 with one playbook, release.yml: dry-run, apply, prove idempotence, then extend it to the db server.
A real change: one playbook, dry-run first, applied, proven idempotent, then extended to a second group. Every skill of the Scroll in one ticket.
World 3The Ledger
11 levelsHard-coded values rot. Keep them in one ledger: variables per group and per host, templates that fill themselves in, restarts only on real change.
Level 22: Write it down once
Objective: Write vars.yml, run info.yml with it, override a value with -e, then change the file itself.
A variable is a name with a value; {{ name }} uses it. A vars file is a flat YAML map (name: value per line) that a play loads with vars_files:. ansible-playbook FILE -e name=value sets a variable for one run and beats every other source.
Commands taught
ansible-variablesLevel 23: Every group its own
Objective: Give the web group and web2 their own ports, check web2's result, then run site.yml and tell Kern db1's port.
Next to the inventory, group_vars/GROUP.yml gives variables to every host of that group and host_vars/HOST.yml to one host; group_vars/all.yml is for everyone. A host's own file beats its group's, a group beats all. ansible-inventory --host NAME shows the merged result.
Commands taught
group-varsLevel 24: Who wins?
Objective: Three files set app_port for web1. Find out which value web1 really uses, without guessing, and tell Kern.
When several places set the same variable, the most specific and the most explicit wins. From weak to strong (the ones you'll meet): role defaults, group_vars/all, group_vars/GROUP, host_vars/HOST, play vars:, vars_files:, role vars/, then -e (always wins).
Commands taught
var-precedenceLevel 25: Remember the answer
Objective: Read each server's version into a variable, print it, then reuse it in later tasks.
register: NAME saves a task's result: NAME.stdout (the text), NAME.rc (exit code), NAME.stdout_lines (list of lines). debug: var: NAME.stdout prints it; later tasks use {{ NAME.stdout }}.
Commands taught
registerLevel 26: Only where it fits
Objective: Install nginx only on Debian, see each host's family, then add httpd for the RedHat family.
when: CONDITION runs a task only where the condition is true. It's an expression without {{ }}: ansible_os_family == "Debian", ver.stdout == "1.4.2", app_tls | default(false). Facts like ansible_os_family let one play serve different systems.
Commands taught
whenLevel 27: One task, many items
Objective: Create one account per team member, grow the team, then create one folder per member with a single task.
loop: takes a list (often a variable: loop: "{{ team }}"); the task runs once per element and item is the current one. A YAML list can be one line, [a, b, c], or one '- a' line each.
Commands taught
loopsLevel 28: Fill in the blanks
Objective: Build templates/app.conf.j2 one line at a time: a variable, a filter, a default, and render it per server.
A template is a text file with blanks. The template module (src: in templates/, dest: on the server) renders it with Jinja2: {{ var }} prints a value, {{ var | filter }} transforms it (upper, lower, default(x)). --diff shows what changed in the file.
Commands taught
jinja-templatesLevel 29: If and for
Objective: Write a template that lists the admins with a loop, adds a TLS line only where tls is true, then lists the team on your own.
Jinja2 logic tags print nothing themselves: {% for x in list %} ... {% endfor %} repeats the lines between them, {% if cond %} ... {% endif %} keeps them only when cond is true. Ansible drops the newline after a tag, so each tag sits on its own line.
Commands taught
jinja-controlLevel 30: Restart only when it changed
Objective: Watch a handler restart nginx, see it stay quiet when nothing changed, then make a second task notify it.
notify: NAME on a task calls the handler with that exact name, only when the task reports changed. handlers: is a list of tasks under the play; each notified handler runs once, at the end, however many tasks called it. The keys of a play (handlers:, tasks:) can come in any order.
Commands taught
handlersLevel 31: Night shift
Objective: PAGER: night.yml fails. Make it pass: only the server on 1.4.2 gets /etc/app/PATCH and restarts, and every web server gets the on-call accounts.
Fix the first error, run again, repeat. YAML errors come first (a value starting with {{ needs quotes), then undefined variables (a registered name must match where it's used), then handler names (notify must match exactly).
Level 32: Close the ledger
Objective: Read TICKET and ship it in four parts: group variables, the template + first run, a calm re-run, and a one-host override.
Variables per group and per host, a template with a loop and a condition, a handler that fires only on change: one release, the Ansible way.
World 4The Guild
10 levelsCopy-pasted playbooks drift apart. Package the work into roles anyone can reuse, install shared roles with one command, and lock every secret.
Level 33: A role is born
Objective: Create two role skeletons with ansible-galaxy, look inside one, then tell Kern which folder holds the weakest variables.
A role packages one job (a web server, a database) in a fixed folder layout: tasks/, handlers/, templates/, files/, defaults/, vars/, meta/, each with a main.yml. ansible-galaxy init NAME creates that skeleton; --init-path DIR says where.
Commands taught
ansible-galaxyansible-rolesLevel 34: Move into the role
Objective: Write the webserver role's tasks, a play that uses the role, run it, then build the db role on your own.
roles/NAME/tasks/main.yml is a plain list of tasks (no hosts:, no tasks: key). A play uses it with roles: and a list of names; the output prefixes each task with its role: TASK [webserver : Web folder].
Commands taught
ansible-rolesLevel 35: Sensible defaults
Objective: Give the role a default port, override it for the web group, then give one host its own value.
defaults/main.yml gives a role's variables a starting value, the weakest of all: group_vars, host_vars, play vars and -e all beat it. vars/main.yml is strong (it beats the inventory) and is for values the role must keep.
Commands taught
role-defaultsLevel 36: The role restarts itself
Objective: The role notifies 'restart nginx' but has no such handler. Give it one: the first run restarts nginx once, a second run restarts nothing.
Inside a role, templates live in templates/ (src: needs no path) and handlers in handlers/main.yml, a plain list like tasks/main.yml. A role's task notifies its handler by name.
Level 37: Borrow from the guild
Objective: Install the project's roles from requirements.yml, list them, use one, then add a second role on your own.
Ansible Galaxy shares roles. requirements.yml lists what a project needs (- src: a Galaxy name, a git URL or a tarball, name: the folder); hidden installs them all. This lab has no internet, so the sources are local tarballs.
Commands taught
ansible-galaxyLevel 38: It needs a friend
Objective: Make the app role bring common with it, run it, then do the same for monitor.
meta/main.yml describes the role; its dependencies: list names roles that must run first. Ansible runs each dependency once, before the role that needs it, even if the play doesn't list it.
Commands taught
role-dependenciesLevel 39: Lock it up
Objective: Encrypt secrets.yml, look at it, read it, change its password, then lock api.yml on your own.
ansible-vault encrypts a whole file with AES256 (header $ANSIBLE_VAULT;1.1;AES256). view prints it without unlocking the file, rekey changes the password, decrypt unlocks it for good. --vault-password-file names a file, kept out of git, that holds the password.
Commands taught
ansible-vaultLevel 40: Secrets at work
Objective: Watch a play fail without the password, give it the password, make that the default, hide the output, then rotate the secret yourself.
A play can load a vaulted file with vars_files:; Ansible needs the password (--vault-password-file, or vault_password_file in ansible.cfg) or it stops before any task. no_log: true on a task hides its values from the output.
Commands taught
vault-in-playsLevel 41: Leaked
Objective: PAGER: .vault_pass was pasted in a public chat. Move every vaulted file to the password in .vault_new, point ansible.cfg at it, and prove site.yml still runs.
When a vault password leaks, every file locked with it must be rekeyed to a new password, and the configuration must point at the new password file. ansible-vault rekey takes several files at once.
Level 42: The guild charter
Objective: Read TICKET and deliver the shop role in four parts.
A role built from scratch, a dependency installed from requirements.yml, a template with a handler, a vaulted vars file, and no plaintext secret left behind.
World 5The Real Fleet
8 levelsThe lab server gives you a controller and up to five real servers on a private network: real ssh, real packages, real failures.
Level 43: The first real run
Objective: Trust the three servers' host keys, write the inventory, get a green ping, then make plain hidden work.
This is real Ansible on real servers. The controller logs in to each server over ssh as the user deploy with a key, and checks each server's host key against ~/.ssh/known_hosts first: an unknown key stops the connection (that's a safety feature).
Commands taught
ansible-real-fleetLevel 44: Real packages, real services
Objective: Install and start nginx with ad-hoc commands, run the full playbook, prove it's idempotent, then install two tools on your own.
The apt module installs Debian/Ubuntu packages (here from the lab's offline repository); the service module starts, stops and restarts services. Both are idempotent: a second run reports ok. ansible-playbook --check asks every module what it would change.
Level 45: One at a time
Objective: Roll a release out one server at a time, then two at a time, then add a health check on your own.
serial: N runs the whole play on N hosts, then the next N: a rolling update, so most servers keep serving while some change. A health check task (uri) after the change stops the rollout when a batch is broken, before the next batch is touched.
Commands taught
rolling-updatesLevel 46: Someone edited by hand
Objective: One web server's nginx settings were edited by hand. Find it without changing anything, write its name in /root/answer, then restore it with the playbook.
Drift is a server that no longer matches the playbook because someone changed it by hand. ansible-playbook --check --diff shows exactly which lines differ on which host, without touching anything; a normal run puts them back.
Level 47: A safety net
Objective: Run a deploy that fails on db1 and watch rescue catch it, read the recap, then add an always: section.
block: groups tasks. When one fails, rescue: runs instead of stopping the host (like try/except), and always: runs in both cases. The recap counts rescued=1 for a host that failed and was rescued.
Commands taught
block-rescueLevel 48: Drift on every server
Objective: PAGER: all three web servers drifted in different ways. Bring them back with site.yml only, until a dry run reports changed=0 everywhere.
Desired state: the playbook is the truth. Find what drifted with --check --diff, fix it by running the playbook (never by hand), and prove it with a dry run that reports changed=0.
Level 49: The silent handler
Objective: PAGER: conf.yml renders a new port, but nginx keeps the old one. Fix the playbook so nginx really serves the port you roll out (try -e app_port=8081).
When a run 'works' but nothing happens: run with -v to see each task's result, --diff to see file changes, and read every task option. changed_when: false makes a task always report ok, so it never notifies its handler.
Level 50: Patch five servers
Objective: Read TICKET and patch all five servers in four parts: the plan, the run with rollback, the fix, the proof.
A real patch day: databases first and one at a time, web servers in batches of two, a health check after each upgrade, and an automatic rollback for a server that fails it. Then prove the fleet is clean.
For teachers
Use these levels with a class
SOLUTION HIDDEN Teachers see answer keys in the classroom.