Let us take a closer look at how to use handlers in ansible and the configurations necessary to use it with the support of our Server Management Support Services at Bobcares.
How to Use Ansible Handlers?
A handler in Ansible is similar to any other task, but it only runs when called or informed. When we make a change to the managed host, it takes action.
Handlers can initiate a secondary change, such as starting or restarting a service after installation or reloading a service after changes to the configuration files.
Here are some things to keep in mind when utilizing handlers to get the most out of them.
- Within the playbook, a handler should have a globally unique name.
- If multiple handlers with the same name are defined, it will then only call the first one. And will ignore the remainder of the handlers.
- Handlers always execute in the order we define them in the handlers section, not in the notify section.
- The handler will not run if the state of a task remains unchanged. As a result, handlers aid in the attainment of idempotency. As a result, a handler job only runs if the state changes; otherwise, it does not.
The Notify and handlers directive defines a handler. The notify directive executes the task(s) provided in the handlers section.
Let’s look at how to define and invoke Ansible handlers.
A single task and a handler in Ansible
Consider the playbook below, which installs and starts Apache on a target system.
---
- name: Install Apache on RHEL server
hosts: webserver
tasks:
- name: Install the latest version of Apache
dnf: name:
httpd state: latest
notify:
- Start Apache
handlers:
- name: Start Apache
service:
name: httpd
state: started
A Playbook will contain a regular job and a handler. On the target machine, the normal task installs the Apache HTTP server. When the notify directive is in the system it calls the handler task, which starts the Apache service.
The regular job is completed first during playbook runtime, followed by the handler.
If the playbook is performed again, the handler job will not be executed because the state and settings of the Apache service remain intact.
Multiple Tasks and Handlers in Ansible
In most circumstances, we will be dealing with several tasks that may necessitate the use of multiple handlers. The following playbook is divided into two regular tasks and two handlers.
Regular tasks
- Installing the most recent Apache version.
- Apache Configuration
Handlers
- Activating the Apache Service
- Allowing inbound HTTP traffic over the firewall
---
- name: Install Apache on RHEL server
hosts: webserver
tasks:
- name: Install the latest version of Apache
dnf:
name: httpd
state: latest
- name: Configure Apache copy:
src: /home/cherry/Documents/index.html
dest: /var/www/html
owner: apache
group: apache
mode: 0644
notify:
- Configure Firewall
- Start Apache
handlers:
- name: Start Apache
service:
name: httpd
state: started
- name: Configure
Firewall
firewalld:
permanent: yes
immediate: yes
service: http
state: enabled
The first process installs Apache, while the second configure it by modifying the default index.html file and applying the necessary group, user ownership, and file permissions.
Despite the fact that the latter appears first in the notify section, the ‘Start Apache’ handler will execute first during playbook runtime, followed by the ‘Configure Firewall’ handler. The handler section will govern the sequence of execution rather than the notify section.
[Need assistance with similar queries? We are here to help]
Conclusion
To conclude we have now learned how to use handlers in ansible in a few simple steps. W have also gone through some notes on tasks and Ansible handlers with the assistance of Server Management Support Services.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments