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

Jatin Lodhi
4 min readJan 13, 2021

Hello guys 😊

Hope you’re doing great !!

Welcome to another article , now again here I come up to show you some more interesting demo on Demanding Technologies 🔥🔥

So , already you have read the Topic Name & it will give idea atleast what you gain from this Article , don’t worry guys if you don’t understand After reading this Article , you will be comfortable with the Idempotency concept and able to perform it.

So let’s start with Idempotence !!

🤷‍♂️ What is Idempotency ?

→ The principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero. Ansible modules are idempotent.

“ Simply if you doing that particular thing once then when you performed same multiple times , has no further effect on its subject and its remain same or unchanged ’’ .

Pre-requisite

Pre-requisite for this practical you should have a configured Ansible Control node. So if you aren’t comfortable with the configuration of the Ansible controller node then you can visit my below-mentioned article.

To remove this idempotency nature , here we use “handlers” module in the playbooks . For 🤷‍♂ ️How to 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.

I hope you read above article atleast you know Idempotency ,handlers .

Now , time to move on Practical part !!

So Guy’s here you will get step by step idea how to rectify idempotency nature while starting httpd service .

First you create a Configuration file for Webserver with .conf extension because without .conf extension they didn’t work .

Create a .conf extension file for webserver

Listen {{ http_port }} <Virtualhost {{ ansible_facts[‘default_ipv4’][‘address’] }}:{{ http_port }}>DocumentRoot {{ doc_root }} </Virtualhost>

Now .conf file is ready and time to move on next part →

Create a ansible-playbook with .yml extension

- hosts: web
vars_prompt:
- name: "doc_root"
prompt: "Enter your document root "
private: no
- name: "Enter_port.no"
prompt: "Enter port no."
private: no
tasks: - name: Install httpd service
package:
name: "httpd"
state: "present"
register: x - name: Directory for Webserver
file:
path: "doc_root"
state: directory
- name: copy the configuration
template:
dest: "/etc/httpd/conf.d/webserver.conf"
src: "webserver.conf"
when: x.rc == 0
notify: "Service Start" - name: "Copy html into doc root "
copy:
dest: "doc_root"
src: "index.html"
- name: Starting httpd service
service :
name: "httpd"
state: "started"
- firewalld:
port: "8080/tcp"
permanent: yes
state: enabled
immediate: yes
handlers: - name: Service Start
service:
name: "httpd"
state: "restarted"

Now the requirement is ready {configuration and .yml file is ready}

Now Run the Playbook :-

ansible-playbook file_name.yml
video of first document root

Here I have run the code and provide Document root /var/www/jatin and port no. 8080 .

So now our handler will only run when any change is made on config file which we have used in template module and notify keyword used with template module will notify to handler if any if change is made or not.

✔️Let’s try running our playbook without changing anything to config file:

Here you can see that “Handler’s” did’nt run .

Now , again run the Ansible-Playbook and this time give different Document root {port no. same 8080}

Again , you can see that our handler will only run when any change is made on config file which we have used in template module and notify keyword used with template module will notify to handler if any if change is made or not.

✔️Let’s try running our playbook without changing anything to config file:

Here also you can see that “Handler’s” did’nt run .

Ohh yesss 😊😉

You clearly see that Playbook has been run with Different Document root .

so let’s see that webpage working or not .

Conclusion

Buddies here you can see or learn that how to use handler’s to remove the idempotence nature of ansible .

So , I hope you enjoy my this article and you learn some new concepts

I tried to explain as much as possible . Feel free to check out my Linkedin profile mentioned below and obviously feel free to comment or follow me on LinkedIn or medium .

Linkedin :- https://www.linkedin.com/in/jatin-lodhi-9230571a7/

Medium :- https://jatinlodhi.medium.com/

THANKS FOR EVERYONE TO READ THIS ARTICLE !!🤗

--

--

Jatin Lodhi

I am an IT Enthusiast, who is passionate about exploring/learn all the latest technologies from research perspective.