๐ ๐ฎ๐๐๐ฒ๐ฟ๐ถ๐ป๐ด ๐๐ป๐๐ถ๐ฏ๐น๐ฒ ๐๐ฎ๐ป๐ฑ๐น๐ฒ๐ฟ๐: ๐ฃ๐ฎ๐ฟ๐-2
Hey Techiesโฆ๐
In this blog, weโre going to discuss about Handling Configuration Changes at Scale with Ansible Handlers.
If you missed to read the part-1 , please find the link below:
During this session , we will explore how to create Ansible handlers with example.
In a real-world scenario with a large number of servers, consider the challenge of making configuration changes that require service restarts. If you have 1000 servers and are altering configuration files, itโs impractical to manually restart services on each one. This is where Ansible handlers prove invaluable.
Handlers in Ansible allow you to define tasks that will be executed only if a change has occurred. So, when you modify configuration files, the associated handler will restart the service โ but only on the servers where the configuration change has taken place. This intelligent approach minimizes unnecessary service restarts and efficiently manages changes at scale.
Using handlers ensures that your operations are targeted and responsive, preventing needless disruptions and optimizing the efficiency of your configuration management process.
****************************************************************************
Prerequisites โ 3 Virtual Machines
1. server โ 1 CPU โ 1GB RAM (Python 2.7) โ Ansible Server
2. node1โ1 CPU โ 1GB RAM ( python 2.6 and above) โ Ansible Client 1
3. node2โ1 CPU โ 1GB RAM ( python 2.6 and above) โ Ansible Client 2
From ansible user execute below command:
ansible all -m ping
the above ping command should return with a ping / pong green color.
***************************************************************************
In the provided example playbook, we have two tasks to install packages (httpd and vsftpd) on two nodes:
---
- name: Example for Handler
hosts: all
tasks:
- name: Install httpd
yum:
name: httpd
state: latest
notify:
- restart httpd
- name: Install vsftpd
yum:
name: vsftpd
state: latest
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
The above playbook will install the latest versions of httpd and vsftpd and then notify the handler to restart the httpd service.
****************************************************************************
I would always prefer good practice to ensure that our playbook doesnโt contain any syntax errors before executing it. Please use the below command to ensure that:
ansible-playbook handlers.yaml - syntax-check
Great! If the syntax check for our Ansible playbook (handlers.yaml) passes without any errors, it indicates that the playbook is correctly written in terms of syntax. We can proceed to execute the playbook using the below command.
ansible-playbook handlers.yaml
The output indicates the execution of tasks in our Ansible playbook:
- The โGathering Factsโ task collects information about the nodes and appears to have completed successfully.
- The โInstall httpdโ task resulted in changes on both node1 and node2, indicating that the httpd package is installed.
- The โInstall vsftpdโ task shows that the vsftpd package is installed on both nodes.
***************************************************************************
Certainly! Letโs uninstall the packages (httpd, vsftpd) from both nodes and observe the handlers in action by using below command:
ansible all -m command -a "yum remove httpd vsftpd -y"
The above command is using Ansible to execute the yum remove command on all hosts (all) for the packages httpd and vsftpd. It will forcefully remove these packages (-y flag).
Then execute the same playbook again:
ansible-playbook handlers.yaml
In the output snippet above, the line โRUNNING HANDLER [restart httpd]โ indicates that Ansible is currently executing a handler named โrestart httpd.โ The [node1] and [node2] entries below indicate that the handler is being applied to both node1 and node2.
The term โchangedโ next to each node signifies that changes were detected on these nodes during the execution of the handler. In this context, it implies that the handler is restarting the httpd service on both node1 and node2 due to changes made in the tasks.
This is a typical behavior of handlers in Ansible. Handlers are associated with specific tasks and are executed only if changes occur during the execution of those tasks. In this case, the handler is triggered because changes were detected, and it restarts the httpd service on the specified nodes.
Execute the following command on both node1 and node2 servers to continuously display the contents and run the playbook. This will provide real-time updates on the playbook execution.
tailf /var/log/messages
๐นNext day โ The Significance of Utilizing Tags in Ansible Playbooks.
โญโญโญ Enjoy your learningโฆ.!!! โญโญโญ