Restarting HTTPD Service in Idempotence Nature using Ansible Handlers !!
Sheetal Agarwal
AWS Community Builder | 4X GCP Certified | 3X AWS Certified | 1X Azure Certified | DevOps Engineer | Cloud Engineer | SRE | Kubernetes | Jenkins | Terraform | Ansible
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
Step 2: Now check the connectivity of controller node with managed node.
ansible all --list-hosts ansible all -m ping
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).
Here notify and handlers keyword are used to make “Restart Httpd” Idempotance.
Step 4: Now store all variables in var file.
Step 5: Create a configuration file for webserver with .conf extension.
Step 6: Now run the playbook.
ansible-playbook server.yml
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
Now let’s check in managed node that web page is copied or not.
Step 8: Now when we wil run this playbook again, it will not execute handlers.
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)-
Step 9: Now change the port no. in var file.
Now again run the playbook.
Here, we can see that Handler task is running because of changes made in template module.
Output on port(8080)-
Here, in this way, Task Completed !!
Thank You Reading the Article !!
DevOps Engineer - TCS | RedHat Certified Specialist | Terraform | AWS | Jenkins | Ansible | Python | OpenShift
3 年Awesome explanation ?