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
| Word | What it means | Example |
|---|---|---|
| Playbook | a file that describes how a machine should be | |
| Task | one thing in a playbook that should be true | a folder exists |
| Inventory | the list of machines a playbook sets up | |
| Idempotent | safe to run again, because a second run changes nothing | |
| Drift | a machine slowly stops matching its description | |
| Dry run | a 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
cd ~/ansible-labShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the inventory
cat inventory.iniShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Check Ansible can reach it
ansible localhost -m pingShown 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
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.ymlShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run it the first time
ansible-playbook till.ymlShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run it a second time
ansible-playbook till.ymlShown 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
| Run | ok | changed | What it means |
|---|---|---|---|
| First | 2 | 2 | both tasks were not true yet, so Ansible made them true |
| Second | 2 | 0 | both 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
printf 'Free cake\n' > /tmp/till-demo/prices.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run the playbook again
ansible-playbook till.ymlShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the price list
cat /tmp/till-demo/prices.txtShown 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?