๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—›๐—ฎ๐—ป๐—ฑ๐—น๐—ฒ๐—ฟ๐˜€: ๐—ฃ๐—ฎ๐—ฟ๐˜-2

Athira KK
4 min readNov 24, 2023

--

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โ€ฆ.!!! โญโญโญ

--

--

Athira KK
Athira KK

Written by Athira KK

AWS DevOps Engineer | Calico Big Cats Ambassador | WomenTech Global Ambassador | LinkedIn Top Cloud Computing Voice

No responses yet