In this video we will start using Ansible Playbooks for the first time.

The outcome of this video should be exactly the same as that which we achieved in the Ad Hoc Commands video, only this time we move the procedure from the command line to a reusable, more maintainable file - our playbook.

The format of a playbook is quite straightforward, but strict in terms of spacing and layout.

We always start a playbook with the three dashes: ---, and then the structure of the file differs depending on what your playbook is aiming to achieve.

In this video, as mentioned, we are going to repeat the process we completed via the ansible command. As such, our playbook will look like this:

- hosts: local
    - name: Install NTP
      apt: pkg=ntp state=installed updated_cache=true

I'd encourage you to use a decent text editor - Sublime Text is my personal preference as it highlights spaces and tabs in a way that makes mistakes easier to spot.

Once you have saved the file - in our case, as our-playbook.yml - we are ready to run the ansible-playbook command.

ansible-playbook our-playbook.yml -k -K -s

Again, this is very similar to the ad hoc commands we have already seen, using the same arguments.

This time, we pass in the playbook which contains the target hosts so we don't have to specify which host or hosts to run this command against.

Again, we are asked for our SSH password, which when provided, will proceed with executing the commands as defined in the playbook file.

After a short period, we should get our result output. Assuming you are using a fresh server, as per the video, the first time we run this playbook, NTP should be installed. We can see this from the output, which says that the [Install NTP] task was run, something was changed, and two tasks output as ok.

The two tasks are the gathering of facts (more on this in a future video), and the installation of NTP. That's why we get two ok's.

The changed task is that the status of something on the system changed. In this case, NTP wasn't installed and now it is. Therefore, something on the system changed.

If we immediately re-run the task, we will see that the next time, both tasks completed successfully again (two ok's), but nothing changed. NTP was already installed, so Ansible is clever enough not to attempt to install it again. This makes our tasks idempotent, which simply means we can run them over and over again, without adverse effect to our system. The Ansible docs put it another way:

Modules are idempotent, meaning they will seek to avoid changes to the system unless a change needs to be made. When using Ansible playbooks, these modules can trigger ‘change events’ in the form of notifying ‘handlers’ to run additional tasks.

We can change the playbook to remove the NTP server by doing the following:

- hosts: local
    - name: Remove NTP
      apt: pkg=ntp state=absent update_cache=true

And now when we re-run the command (the same command as before), the NTP service will be removed. Again, the first time through this will have two ok's and one changed outcome. And if we were to re-run the command, the second time through would show two ok's and nothing changed.

This task is nice and straightforward.

As we proceed, you will see that some tasks can become quite involved, and that potentially, can introduce problems. As such, it's a really good idea - especially in our training environment - to take regular snapshots. That way, should something go wrong, rather than having to log on to the server and manually clean up the mess, we can simply roll back to our snapshot, make the necessary changes to our playbook, and then try again.

Code For This Course

Get the code for this course.

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.

Episodes in this series

# Title Duration
1 How To Install Ubuntu Server in Oracle VirtualBox 10:21
2 How to Rename our Ubuntu Server 02:00
3 Installing Ansible on Ubuntu Server 00:33
4 Safety First, Safety Second - Snapshots are like Ctrl+Z 00:11
5 Managing the Ansible Inventory Hosts File 02:16
6 Ansible Ad Hoc Commands 04:27
7 Introduction to Ansible Playbooks 02:14
8 Ansible Handlers 01:38
9 Ansible Variables 03:16
10 Git Your Deploy Just Right 05:34
11 Ansible Roles 05:51
12 Looping in Ansible with_items 04:34
13 Ansible Files For Beginners 06:14
14 Variable Precedence - Where To Put Your Role Vars? 04:13
15 Ansible Templates 05:51
16 Ansible Inventory With Our Own Hosts Files 06:57
17 How to Manage Users with Ansible 08:32
18 Ansible Vault Tutorial 03:48
19 Ansible Galaxy Tutorial 10:03
20 Real World Ansible - Common Role Walkthrough 06:19
21 Ansible MySQL Tutorial 13:44
22 Ansible Symfony and nginx 09:37