Restarting HTTPD Service in Idempotence Nature using Ansible Handlers !!

Restarting HTTPD Service in Idempotence Nature using Ansible Handlers !!

Task Description??

?? Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook.

What is Ansible??

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, etc. Ansible is agentless, temporarily connecting remotely via SSH to do its tasks.

What is idempotence?

An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions. Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra and functional programming. for example When we multiply any number with 1 but end result always be the same.

What is Handler?

Handlers are just like a normal task. Handlers will perform an action when listens for a notify event. If nothing notifies a handler, it will not run. Regardless of how many tasks notify a handler, it will run only once, after all of the tasks completed in a particular play.

Pre-requisite -> Two RHEL OS (one is controller node and second is managed node).

Let’s Start Practical Part -

Step 1: In controller Node Ansible should be configured. check that ansible is configured in controller node.

ansible --version
No alt text provided for this image

Step 2: Now check the connectivity of controller node with managed node.

ansible all --list-hosts
ansible all -m ping
No alt text provided for this image

Step 3: Now write Ansible playbook to configure yum, webserver , httpd conf file for document root and port number and to configure web page in Managed node (192.168.43.248).

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
Here notify and handlers keyword are used to make “Restart Httpd” Idempotance.

Step 4: Now store all variables in var file.

No alt text provided for this image

Step 5: Create a configuration file for webserver with .conf extension.

No alt text provided for this image

Step 6: Now run the playbook.

ansible-playbook server.yml
No alt text provided for this image
No alt text provided for this image

Here, we can see that all tasks are running including handler.

Step 7: In Managed node check that httpd package is installed or not.

rpm -q httpd
No alt text provided for this image

Now let’s check in managed node that web page is copied or not.

No alt text provided for this image

Step 8: Now when we wil run this playbook again, it will not execute handlers.

No alt text provided for this image
No alt text provided for this image

Here, we cans we can see that handler is not executed because, there is no change in copy and template module.

Output on port(8081)-

No alt text provided for this image

Step 9: Now change the port no. in var file.

No alt text provided for this image

Now again run the playbook.

No alt text provided for this image
No alt text provided for this image

Here, we can see that Handler task is running because of changes made in template module.

Output on port(8080)-

No alt text provided for this image

Here, in this way, Task Completed !!

Thank You Reading the Article !!

Jayant Kumar Pathak

DevOps Engineer - TCS | RedHat Certified Specialist | Terraform | AWS | Jenkins | Ansible | Python | OpenShift

3 年

Awesome explanation ?

回复

要查看或添加评论,请登录

Sheetal Agarwal的更多文章

社区洞察

其他会员也浏览了