Playbooks use the YAML format to define one or more plays. A play is a set of ordered tasks that are arranged in a way to automate a process, such as setting up a web server or deploying an application to production.
In a playbook file, plays are defined as a YAML list. A typical play starts off by determining which hosts are the target of that particular setup. This is done with the hosts
directive.
Setting the hosts
directive to all
is a common choice because you can limit the targets of a play at execution time by running the ansible-playbook
command with the -l
parameter. That allows you to run the same playbook on different servers or groups without the need to change the playbook file every time.
Start by creating a new directory on your home folder where you can save your practice playbooks. First, make sure you’re in your Ubuntu user’s home directory. From there, create a directory named ansible-practice
and then navigate into that directory with the cd
command:
- cd ~
- mkdir ansible-practice
- cd ansible-practice
If you followed all prerequisites, you should already have a working inventory file. You can copy that file into your new ansible-practice
directory now. For instance, if you created your test inventory file in an ansible
directory in your home folder, you could copy the file to the new directory with:
- cp ~/ansible/inventory ~/ansible-practice/inventory
Next, create a new playbook file:
- nano playbook-01.yml
The following playbook defines a play targeting all
hosts from a given inventory. It contains a single task to print a debug message.
Note: We’ll learn more about tasks in the next section of this series.
Add the following content to your playbook-01.yml
file:
---
- hosts: all
tasks:
- name: Print message
debug:
msg: Hello Ansible World
Save and close the file when you’re done. If you’re using nano
, you can do that by typing CTRL+X
, then Y
and ENTER
to confirm.
To try this playbook on the server(s) that you set up in your inventory file, run ansible-playbook
with the same connection arguments you used when running a connection test within the introduction of this series. Here, we’ll be using an inventory file named inventory
and the sammy user to connect to the remote server, but be sure to change these details to align with your own inventory file and administrative user:
- ansible-playbook -i inventory playbook-01.yml -u sammy
You’ll see output like this:
OutputPLAY [all] ***********************************************************************************
TASK [Gathering Facts] ***********************************************************************
ok: [203.0.113.10]
TASK [Update apt cache] **********************************************************************
ok: [203.0.113.10] => {
"msg": "Hello Ansible World"
}
PLAY RECAP ***********************************************************************************
203.0.113.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You might have noticed that even though you have defined only one task within your playbook, two tasks were listed in the play output. At the beginning of each play, Ansible executes by default an additional task that gathers information — referred to as facts — about the remote nodes. Because facts can be used on playbooks to better customize the behavior of tasks, the fact-gathering task must happen before any other tasks are executed.
We’ll learn more about Ansible facts in a later section of this series.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes, using only SSH and Python to communicate and execute commands on managed servers. This series will walk you through the main Ansible features that you can use to write playbooks for server automation. At the end, we’ll see a practical example of how to create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
“If you followed all prerequisites” … WHERE are the prerequisites!?