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

Free lesson

Ansible Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Virtual machine lab5 min read4 commands

What you will do

Setting up a machine by hand is slow, and nobody remembers every step. Ansible is a tool where you write down how a machine should be, in a file called a playbook, and it makes the machine match. You will look at a project, read a playbook, run it and ask Ansible about the machine. You will not finish the playbook: that is the lab's own task.

Before you start

Note

Press Start the lab on the bar at the top of this lesson. This lab is a whole Ubuntu machine of your own, so it takes a little longer to come up than a container. Each Run button below types its command into the machine's terminal, one after another. If one says the machine is still starting, wait a few seconds and press it again. To watch them, or to type your own, press Open terminal on the bar.

1. Look at the project

The lab leaves a folder, ansible-lab, in your home folder. ls -l lists what is in it: site.yml is the playbook, inventory.ini says which machines Ansible looks after (only this one), ansible.cfg is its settings, and templates holds a file with a blank to fill in.

Command
ls -l ansible-lab

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

2. Read the playbook

A playbook is a list of tasks, written in YAML. This one has a single task that prints a message, and a variable, site_name, that is Riverside College. Below it are three TODO lines where the lab's three jobs go.

Command
cat ansible-lab/site.yml

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

3. Run it

ansible-playbook reads the playbook and does what it says. The last lines are the recap: ok=2 means two tasks went well, and changed=0 means nothing changed, because printing a message changes nothing. Once the machine is right, a good playbook says changed=0 on every run. This is called being idempotent.

Command
ansible-playbook -i ansible-lab/inventory.ini ansible-lab/site.yml

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

4. Ask a module a question

Every task uses a module, a small program that does one job. stat looks at a file or folder. Asked about /etc/ansible-lab, the folder the lab wants made, it answers "exists": false and "changed": false: it looked and touched nothing.

Command
ansible localhost -i ansible-lab/inventory.ini -m ansible.builtin.stat -a path=/etc/ansible-lab

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

Now do the lab

Tip

You have seen what is missing: a package, a file and a unit file. Adding them to the playbook is the task of this lab: open README.md in your home folder with cat README.md in the terminal. It says what to do and gives none of the answers.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson is worked in a VM lab: each student has an Ubuntu machine of their own with Ansible on it, and a Run types into its terminal and shows what it printed. The lab itself has a check that runs the student's playbook twice and marks what it made, and whether the second run changed nothing. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

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

Teaching a class? Set these lessons for your students with marking and a lab for every student.