A way to rectify challenge of “Restarting HTTPD Service is not Idempotence in Nature” using Ansible Playbook.

Gaurav Khore
4 min readJan 24, 2021

What is ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration.

Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency.

What is Apache http Webserver?

Apache HTTPD is an HTTP server daemon produced by the Apache Foundation. It is a piece of software that listens for network requests (which are expressed using the Hypertext Transfer Protocol) and responds to them.

It is open source and many entities use it to host their websites. Other HTTP servers are available (including Apache Tomcat which is designed for running server side programs written in Java (which don’t use CGI)).

What is the problem?

🔰 Restarting HTTPD Service is not
idempotence in nature and also consume more
resources .

lets analyze the problem:-

For example lets take a scenario where we have to launch a httpd server on the target node using the ansible playbook, the playbook will look like for this case,

We will run the ansible playbook using the below mentioned cmd,

ansible-playbook rest.yml

The output of the above cmd will be like,

As we have run the playbook first time or configured the httpd server first time on the target node so their will be changes in all the task,

Lest see what happen if we run the ansible playbook again without making changes in the playbook,

In the output we can see that only the httpd service start has changed which means that the service is restarted when we again run the playbook, this is a problem as when ever we make small change in the code of html and run the playbook to apply the code it will again restart the httpd service which means that it will consume more resource.

Solution:-

For the above problem of restarting httpd service is not idempotence in nature we can make some changes in the playbook. The changes will be that we will use the “when” keyword in the “service” module.

Lets see how and where to use “when” keyword in the playbook,

- hosts: controlnode
tasks:
- name: "Installing httpd"
package:
name: httpd
state: present
register: x
- name: "copying data to the target node"
copy:
dest: /var/www/html/index.html
content: "Restarting is not an Idempotence in nature"
- name: "httpd service start"
service:
name: httpd
state: restarted
enabled: yes
when: x.changed == true

The above code will work in such a way that if the httpd server is installed on the target node than only the http service will restart , this is happening so because the output of the httpd install is save in the variable “x” using “register” keyword ,x will store the changes made by install httpd task, the if their are changes than the httpd service will restart this is managed by the “when: x.changed == true” statement written inside the httpd service start task.

The output of the code will be like,

As we can see that the httpd service start task has been skipped because their is no changes in the installing httpd task.

Thus we have successfully solved the problem of “Restarting HTTPD Service is not Idempotence in Nature” using ansible playbook,

The page will be like,

Thanks for reading……

--

--