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

Free lesson

Set up a machine from a file (Ansible)

After this lesson: I can write a playbook that sets up a machine and changes nothing the second time it runs.

55 min lesson10 commandsAnsible lab: configure a machine with a playbook, in your browser

Five servers, five different set-ups

A small company has five web servers. Different people set each one up by hand, over two years. One of them answers slowly and nobody can say why.

The cure is to write the set-up down in a file, and let a tool make every machine match it. When a server dies, you build the same one again in minutes.

By the end of this lesson

Note

I can write a playbook that sets up a machine and changes nothing the second time it runs.

Key words

WordWhat it meansExample
Playbooka file that lists how machines should be set up
Taskone step in a playbook
Modulea built-in tool that a task usesansible.builtin.file
Inventorythe list of machines to manage
Idempotentsafe to run again, because the second run changes nothing
Templatea file with blanks that variables fill in

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

A playbook says how the machine should be

A playbook does not say "make a folder". It says "this folder must exist".

The tool looks at the machine, sees what is already right and fixes only what is wrong. You describe the end state. The tool finds the steps.

This VM holds a project folder. Move into it and read the files. The Runs share one terminal, so the move lasts.

Move into 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 playbook

Command
cat site.yml

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.

Run it twice and nothing more changes

A task that is idempotent can run any number of times with the same result. The first run does the work. The second finds nothing to do.

Now try it. The next Run writes a small playbook of your own, practice.yml. It makes a folder and a note in /tmp. It sits beside site.yml and leaves it alone.

Write your own practice playbook

Command
printf '%s\n' '- hosts: localhost' '  gather_facts: false' '  vars:' '    team: Finance' '  tasks:' '    - name: Make a notes folder' '      ansible.builtin.file:' '        path: /tmp/notes-demo' '        state: directory' '    - name: Write a note' '      ansible.builtin.copy:' '        dest: /tmp/notes-demo/note.txt' '        content: "Hello {{ team }}"' > practice.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 practice.yml

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

Run it again

Command
ansible-playbook practice.yml

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

A common mistake

Warning

Many people think a playbook is a shell script written in YAML. A script does every step every time. A playbook module checks first and acts only when it must. Use the shell module only when no module exists, because it reports a change on every run.

A template fills blanks from variables

A template is a file with blanks, such as {{ site_name }}. A variable fills each blank when the file is made.

Change a variable and the file changes, with no editing by hand. You can set a variable on the command line with -e.

First read the lab's own template. Then change the team in your practice playbook.

Read the lab's template

Command
cat templates/welcome.txt.j2

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

Run it with another team

Command
ansible-playbook practice.yml -e team=Support

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

Dry run: what would change back?

Command
ansible-playbook practice.yml --check --diff

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

Read the note

Command
cat /tmp/notes-demo/note.txt

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

Going further

Write a second playbook, shell.yml, with one task that uses ansible.builtin.shell to run date > /tmp/notes-demo/stamp.txt. Run it twice and read the recap each time.

Then add args: with creates: /tmp/notes-demo/stamp.txt under the task and run it twice again. Explain what changed in the recap, and why.

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.