Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Setting up every machine the same way (Ansible)

After this lesson: I can set up a machine the same way every time.

50 min lesson9 commandsAnsible lab: configure a machine with a playbook, in your browser

Thirty laptops by Monday

A college technician has to set up thirty laptops for a new class. Each one needs the same apps, folders and settings. She does them by hand, one at a time.

By laptop twenty she is tired. On one laptop she skips a step and nobody notices for a week. A file that describes the laptop could set up all thirty the same way.

By the end of this lesson

Note

I can set up a machine the same way every time.

Key words

WordWhat it meansExample
Playbooka file that describes how a machine should be
Taskone thing in a playbook that should be truea folder exists
Inventorythe list of machines a playbook sets up
Idempotentsafe to run again, because a second run changes nothing
Drifta machine slowly stops matching its description
Dry runa run that shows what would change and changes nothing

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top. This lab is a whole Ubuntu machine of your own, so it takes a few minutes to start. If a Run says the machine is still starting, wait and press it again.

A playbook describes, it does not list steps

Ansible reads a playbook and makes a machine match it. Each task says one thing that should be true, such as "this folder exists".

The inventory lists the machines to set up. Your lab's inventory has one machine, the lab itself. The third Run asks Ansible to say hello to it.

Go to the project folder

Command
cd ~/ansible-lab

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the inventory

Command
cat inventory.ini

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check Ansible can reach it

Command
ansible localhost -m ping

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run it twice and count the changes

The next Run writes a small playbook called till.yml. It makes a folder for a café till and writes a price list into it. The shop's name is a variable, shop.

Run the playbook twice. Each run ends with a recap line. Look at the number after changed= each time.

Write the practice playbook

Command
printf '%s\n' '- hosts: localhost' '  gather_facts: false' '  vars:' '    shop: Corner Cafe' '  tasks:' '    - name: Make a till folder' '      ansible.builtin.file:' '        path: /tmp/till-demo' '        state: directory' '    - name: Write the price list' '      ansible.builtin.copy:' '        dest: /tmp/till-demo/prices.txt' '        content: "Prices for {{ shop }}\n"' > till.yml

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run it the first time

Command
ansible-playbook till.yml

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run it a second time

Command
ansible-playbook till.yml

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: the two recap lines

RunokchangedWhat it means
First22both tasks were not true yet, so Ansible made them true
Second20both tasks were already true, so there was nothing to do

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a playbook is a list of commands that runs every step every time. Ansible checks each task first and only acts when it is not true yet. That is why a second run says changed=0, and why it is safe to run again.

Running it again puts things right

Over time somebody changes a machine by hand and forgets. That is called drift.

The next Run plays that person. It writes "Free cake" over the price list. Then run the playbook again and read the file.

Change the price list by hand

Command
printf 'Free cake\n' > /tmp/till-demo/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run the playbook again

Command
ansible-playbook till.yml

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the price list

Command
cat /tmp/till-demo/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further: a dry run

Run ansible-playbook till.yml --check --diff -e shop=Harbour in the terminal. -e gives the variable a new value for this run only. --check makes it a dry run.

Read the lines that start with a minus and a plus. Then read the price list again. Did the dry run change it?

About this lab and its source: Ansible lab: configure a machine with a playbook, in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.