In this guide, we will explore Ansible handlers in more detail and learn how to use handlers in Ansible playbooks.
What Are Handlers in Ansible?
In Ansible, a handler functions like any other task, but it only runs when triggered or notified. Handlers are executed when a change occurs on the managed host. They are typically used to initiate secondary actions, such as starting or restarting a service after installation or reloading a service following modifications to configuration files.
Why Use Handlers in Ansible?
Handlers provide several advantages:
- Efficiency: They only execute when necessary, saving time and resources.
- Automation: They seamlessly automate secondary tasks like service restarts or configuration reloads.
- Flexibility: You can define multiple handlers to respond to different types of changes.
Use Handlers in Ansible Playbook
To better understand how Handlers work, we will take an example of a playbook file – install_apache.yml – that installs the Apache webserver and later restarts the Apache service. In the example below, the handler is notified to restart the Apache service soon after installation. This is achieved using the notify module as shown. Note that the ‘notify’ name should coincide with the handler name as pointed out, otherwise you will encounter errors in your playbook file.
$ vi install_apache.yml --- - hosts: staging name: Install Apache2 become: yes tasks: - name: Install Apache2 on Ubuntu server apt: name: apache2 state: present update_cache: yes notify: - Restart apache2 handlers: - name: Restart apache2 service: name: apache2 state: restarted
Save & close the file.
Now, run the playbook using the following command.
$ ansible-playbook install_apache.yml
From the output, you can see the Handler being executed right after the task.
Multiple tasks with multiple handlers
Additionally, we can have several tasks calling multiple handlers. Consider the playbook file below.
Here are have 2 tasks to run:
- Installing Apache webserver
- Allowing HTTP traffic on the UFW firewall.
After the tasks are successfully executed, I have called each of the handlers with the ‘notify’ module as shown below. The first handler restarts Apache and the second one reloads the UFW firewall.
$ vi multiple-task-handlers.yml --- - hosts: staging name: Install Apache2 and Configure Firewall become: yes tasks: - name: Install Apache2 on Ubuntu server apt: name: apache2 state: present update_cache: yes - name: Allow HTTP traffic on UFW firewall ufw: rule: allow port: http proto: tcp notify: - Restart apache2 - Reload ufw firewall handlers: - name: Restart apache2 service: name: apache2 state: restarted - name: Reload ufw firewall ufw: state: enabled
When the playbook file is executed, both handlers are executed by Ansible right after Apache is installed and HTTP traffic is allowed on the firewall.
The secondary actions executed by the handlers here are:
- Restarting Apache
- Enabling and reloading the firewall for the changes made to be effected.
Conclusion
As you have seen, handlers are just like regular tasks, only that they are referenced using a globally unique module called ‘notify’. If a handler is no notified, it fails to run. Remember that all handlers run after all the tasks have been completed.
That’s all from this guide, I believe you have found it useful. Feel free to post your queries and feedback in below comments section.
Also Read : How to Create Ansible Roles and Use them in Playbook
echo “autocmd FileType yaml setlocal ai ts=2 sw=2 et” > .vimrc