Your DevOps study space

Real explanations. Videos with tests. Fresh exams every time. Keep the streak alive and watch the XP stack up.

Configuration management with Ansible · DevOps Docs · StackwisePlanDevOps Configuration management with Ansible · DevOps Docs · StackwiseCodePython Configuration management with Ansible · DevOps Docs · StackwiseBuildDocker Configuration management with Ansible · DevOps Docs · StackwiseTestDevOps Configuration management with Ansible · DevOps Docs · StackwiseReleaseGit Configuration management with Ansible · DevOps Docs · StackwiseDeployKubernetes Configuration management with Ansible · DevOps Docs · StackwiseOperateLinux Configuration management with Ansible · DevOps Docs · StackwiseMonitorDevOps

Pick a topic

Eight tracks, each with docs, a cheat sheet, videos and tests.

How it works

One loop, repeated until it sticks.

Jump back in

Picked up from where you left off.

Free forever

Your DevOps journey starts with one click.

Sign in with Google or GitHub to open every chapter, video, test and challenge. Your progress is saved wherever you learn.

Configuration management with Ansible

Terraform creates servers; Ansible makes them useful: packages installed, config files in place, services running. You describe the desired state in YAML, and Ansible logs in over SSH and makes each machine match it, as many times as you like, on as many machines as you like.

How Ansible works

control node: your laptop or CIinventory.iniwhich machinessite.ymlwhat they should look likeruns when you run itweb1SSH + Python 3, no agentSSHweb2SSH + Python 3, no agentdb1SSH + Python 3, no agent
Ansible pushes: nothing runs on the servers until you run a playbook. The modules check first and only change what isn't already right.
  • Agentless: nothing to install on the servers. The control node (your laptop or a CI runner, Linux or macOS; on Windows use WSL) connects over plain SSH with your normal keys, copies a small Python module over, runs it and removes it. The managed nodes just need SSH and Python 3.
  • Push-based: changes happen when you run Ansible, not on a schedule.
  • Modules do the work: ansible.builtin.apt, copy, template, service, user and thousands more in collections. Each one knows how to check the current state before changing anything.
$ pipx install --include-deps ansible     # or: sudo apt install ansible
$ ansible --version

Inventory: which machines

The inventory lists hosts and sorts them into groups. The INI format is the simplest:

# inventory.ini
[web]
web1 ansible_host=203.0.113.11
web2 ansible_host=203.0.113.12

[db]
db1 ansible_host=10.0.2.20

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_ed25519

A small ansible.cfg next to it saves typing -i inventory.ini every time:

# ansible.cfg
[defaults]
inventory = inventory.ini

In the cloud, dynamic inventory plugins (like amazon.aws.aws_ec2) build the list from your account instead, grouping instances by tags.

Ad-hoc commands

For one-off jobs you don't need a playbook: ansible <hosts> -m <module> -a <arguments>.

$ ansible all -m ansible.builtin.ping          # can Ansible log in and run Python?
web1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
$ ansible web -a "uptime"                      # default module: command
web1 | CHANGED | rc=0 >>
 10:42:07 up 12 days,  3:10,  0 user,  load average: 0.08, 0.03, 0.01
$ ansible web -b -m ansible.builtin.apt -a "name=htop state=present"   # -b = sudo
$ ansible db -m ansible.builtin.setup -a "filter=ansible_distribution*" # facts

(ping here isn't ICMP ping: it checks SSH login and Python.)

A playbook: install and run nginx

A playbook is a YAML file with one or more plays; each play maps a group of hosts to a list of tasks, run in order.

# site.yml
---
- name: Install and run nginx
  hosts: web
  become: true                      # run tasks with sudo
  vars:
    site_title: "DevOps practice"

  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true
        cache_valid_time: 3600      # refresh the package list at most once an hour

    - name: Deploy the nginx site config
      ansible.builtin.template:
        src: templates/site.conf.j2
        dest: /etc/nginx/sites-available/default
        owner: root
        group: root
        mode: "0644"
      notify: Reload nginx          # trigger the handler, only if this changed

    - name: Deploy the homepage
      ansible.builtin.template:
        src: templates/index.html.j2
        dest: /var/www/html/index.html
        mode: "0644"

    - name: Make sure nginx is running and starts on boot
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

  handlers:
    - name: Reload nginx
      ansible.builtin.service:
        name: nginx
        state: reloaded

The templates use Jinja2: templates/index.html.j2 could contain <h1>{{ site_title }}</h1><p>Served by {{ inventory_hostname }}</p>, and each server gets its own name filled in.

$ ansible-playbook site.yml --syntax-check
$ ansible-playbook site.yml
...
PLAY RECAP *********************************************************
web1   : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
web2   : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

That recap is from a second run: everything is already correct, so nothing changed. On the first run you'd see changed for the tasks that did real work, and the handler running at the end.

Idempotency and handlers

Idempotent means running the same playbook once or ten times gives the same result. Modules make this work by describing a state ("nginx is present") rather than an action ("install nginx"): if it's already true, they report ok and do nothing. The raw command and shell modules can't know that, so they report changed every run. Prefer a real module, or add creates:/removes: so the command is skipped when its work is done:

# not idempotent: adds another copy of the line on every run
- name: Tune swappiness
  ansible.builtin.shell: echo "vm.swappiness=10" >> /etc/sysctl.conf

# idempotent: makes sure the line exists exactly once
- name: Tune swappiness
  ansible.builtin.lineinfile:
    path: /etc/sysctl.conf
    line: "vm.swappiness=10"

Handlers are tasks that run only when notified, and only once, at the end of the play, however many tasks notified them. So nginx is reloaded only when its config actually changed, never on a run where nothing did.

Variables, roles and dry runs

  • Variables can live in the play (vars:), in group_vars/web.yml (for every host in the group), in host_vars/web1.yml, or on the command line with -e "site_title=Test", which beats everything else. Facts gathered at the start of each play, like ansible_facts['distribution'], are variables too.
  • Roles package tasks, handlers, templates and default variables into a reusable folder: ansible-galaxy role init roles/nginx creates the structure, and a play uses it with roles: [nginx]. Community roles and collections come from Ansible Galaxy.
  • --check is a dry run: it reports what would change without changing it. Add --diff to see the exact lines that would change in files. Note that tasks depending on earlier ones can fail in check mode (the service task can't find nginx if it wasn't really installed).
  • --limit web1 runs on one host only, which is great for trying a change on one server first. -v to -vvvv adds detail when debugging.
  • Secrets go in files encrypted with Ansible Vault (ansible-vault create group_vars/web/vault.yml), and you run with --ask-vault-pass.

Common errors and fixes

  • UNREACHABLE with "Permission denied (publickey)": wrong user or key. Check ansible_user and ansible_ssh_private_key_file, and that ssh [email protected] works by hand.
  • Host key verification failed: the server isn't in known_hosts yet. Connect once with ssh, or add it with ssh-keyscan.
  • "Missing sudo password": become needs a password on that server. Run with -K (--ask-become-pass).
  • YAML errors: indentation must be spaces, and a value that starts with {{ must be quoted: dest: "{{ web_root }}/index.html".
  • "… is undefined": a variable is misspelled or not defined for that host. ansible-inventory --host web1 shows which variables a host gets from the inventory.

Questions

Pick an answer. Get it wrong and you can try again right away, no waiting. 10 questions per topic - pick a filter below, then use Recreate questions for a fresh set on that topic.

Fresh questions every time

Exam

A mixed test across the whole site, shuffled so you never know which topic comes next. When you submit, every mistake comes with the reason and a jump to the exact moment in the video that explains it.

30 questions 6 topics 20 minute limit Up to 70 XP
1–4 answer · ←→ move
History

Essay

0 / 200-250 words
Sample answer
History
0:00 / 0:00
Not started

on YouTube
Subscribe

Topic progress

Activity

Achievements

Settings

Your profile and your account.

Profile details

* Required. Saved in this browser until you sign in.

Letters, numbers, dots, dashes and underscores.
0 / 160

Account

Checking…

App

Install Stackwise on your phone or computer. It opens in its own window from your home screen, and the Docs and cheat sheets you've opened keep working without internet.

Only admins can see this

Dashboard

Sign-ups, activity, messages and reviews in one place.

Prove it

Certificates

Pass a topic's test and get a certificate with its own public page you can share or add to LinkedIn. Each test is 10 questions drawn at random and graded on the server; you need 8 right, you have 15 minutes, and you get one try per topic a day.

From first command to production

Linux for DevOps

A path through Linux the way DevOps engineers use it: ten stages, every command explained with its flags, examples, real output and mistakes, the combinations engineers chain together, and incidents worked through step by step. Practise everything in a safe lab folder.

Before the interview

Interview prep

Questions that come up in real DevOps interviews, with the kind of answer that lands. Say yours out loud first, then compare. Each one links to the chapter that goes deeper.

    Private to you

    My notes

    Everything you wrote down while watching and reading, in one place. Saved to your account, never shown to anyone.