In this video we take a look at Handlers.

The easiest way to think about Handlers is that they are fancy Tasks.

Tasks are Ansible's way of doing something and Handlers are our way of calling a Task after some other Task completes.

The best way to think of this is using the example of having a Playbook for installing Apache. As part of that Playbook, we have a variety of Tasks that carry out the essential tasks that make up our Apache install. Everything from installing Apache through to securing our installation, creating our expected directory structure, and ensuring all our plugins (e.g. mod_rewrite) are installed.

Now, as part of some of these Tasks (or steps), we would have to re-start Apache.

We don't want to copy and paste a Task over and over, whenever we need to re-start Apache (or any other service for that matter).

Instead, we want to put a single Restart Apache Task into our Playbook somehow, and then call that Task as required.

That Task we call is a Handler. A task we can re-use multiple times whilst our Playbook runs.

We call Handlers using the notify syntax.

- hosts: all

    - name: Install Nginx
      apt: pkg=nginx state=installed update_cache=true
        - Start Nginx

    - name: Start Nginx
      service: name=nginx state=started

Already you are likely beginning to see how these Playbooks can become large and unwieldy beasts, if not kept in check. We will look at how to properly structure our Playbooks in a forthcoming video, but it is good to know that you can - if you are so inclined - keep everything directly in a single Playbook file.

So in summary, and to quote directly from the manual:

Handlers are just like regular tasks in an Ansible playbook (see Tasks), but are only run if the Task contains a “notify” directive and also indicates that it changed something. For example, if a config file is changed then the task referencing the config file templating operation may notify a service restart handler. This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage.

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