Making HTTPD Service Idempotent Using Ansible

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

After reading this article, you'll be able to make the httpd service idempotent using Ansible.

To know more about Ansible, you can go through my article:

Introduction to Ansible and How Industries are solving their challenges using Ansible.


Ansible and Idempotence

Ansible’s philosophy is that playbooks (whether for server provisioning, server orchestration, or application deployment) should be declarative. This means that writing a playbook does not require any knowledge of the current state of the server, only its desirable state. And, the principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. 

In general, Idempotence is “the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application”.

Ansible modules are idempotent. For example, if one of the tasks is to create a directory on the server, then the directory will be created if and only if it does not already exist. This sounds pretty simple to implement in the case of filesystem operations, but it’s not as trivial when considering more complex modules. Nonetheless, Ansible guarantees idempotence in all of its core modules.

Let’s say we ran an ansible-playbook previously and did some changes afterward but if some service is already started according to the particular task, then it won’t restart in the next run and hence the changes made won't come into effect. Even if we run ansible-playbook for restarting the service of httpd, it will restart the service each time the playbook is run, however, we want the service to restart only when we make any changes to configuration files to save the compute resources.

To solve this kind of use case we use handlers.

Handlers: running operations on change

Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.

So, in order to make httpd service idempotent using ansible, we make use of handlers.

You can see the ansible-playbook by clicking on the link provided below.

Ansible-playbook/apache_webserver_pb.yml

After running the playbook, it is clearly visible that We can restart the httpd service in an idempotence nature.

No alt text provided for this image

This is how the playbook runs when no change is made in the configuration file.


Now, I'll change the port number in "vars.yml" file.

No alt text provided for this image

You can see in the image above that the task named "Restart httpd" under handler has been notified and run.


Thus, we succeeded in making the httpd service idempotent.

That's it! Thank you for reading.



Yash Raj

Infoscian | Ex-TCSer | Ex-Merkle | Digital Marketing | AEC | Adobe Campaign Classic Developer | ACM | AEP | AJO | Adobe RT-CDP | Adobe CJA | Agile Model | Confluence | Jira | Python | C Prog | Unix | REST API | SQL | ML

4 年

Very useful

回复
Arsh Mishra

Data Scientist & Machine Learning Engineer | Building Scalable Models for Real-World Impact

4 年

Good ??

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

Ishika Sinha的更多文章

社区洞察

其他会员也浏览了