“Restarting Httpd Service is not idempotent, how to make the Service Idempotent.”

Deepak Sharma
3 min readJan 25, 2021

--

What is Ansible??

Ansible is an simple It Automation tools that makes the work easier for it admins . system admins and almost for all the guys that somehow work for technology. It makes your systems to deploy and maintain easier , it is very easy to learn. Can Do multiple Tasks in one Single Click.

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. wikipedia

Challenge to restart keyword in Ansible:-

As We Know that restarting any kind of services is not a good practice if there is no configuration changed so Using Service module of ansible we do restart service of apache httpd program .

Problem Statement:-

As we run our playbook after making some changes in configuration file than it would be worth to restart server but if No changes are made in Configuration file of Httpd Server that it would not be a better practice to restart services the number of time you run playbook.

Given Playbook Always restart HTTPD Service:-

- hosts: all
vars:
httport:8080
documentroot: /var/www/vikweb
tasks:
- package:
name: "httpd"
state: present
become: true

- copy:
src: /root/vikas/lw.conf
dest: "/etc/httpd/conf.d/
become: true - service:
name: httpd
state: started
become: true - service:
name: "httpd"
state: restarted
become: true

Sometimes you want a task to run only when some changes happend. 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.

Use of Handlers in Playbook:-

Handlers- let’s understand the handlers as function so function run only when you call them so it creates ansible module function and run only when certain conditions are satisfied otherwise not .

- hosts: all
vars:
httport: 8080
documentroot: /var/www/vikweb
tasks:
- package:
name: "httpd"
state: present
become: true

- copy:
src: /root/vikas/lw.conf
dest: "/etc/httpd/conf.d/
become: true
notify:
- restart web-server - service:
name: httpd
state: started handlers:
- name: restart web-server
service:
name: "httpd"
state: restarted
become: true

you can see I have used Two keyword one is handler and other one is notify.

handler:

handlers takes the responsibility of the tasks means when it should execute or not .

notify:

notify keyword tells the handler to run the task only when some changes are made in the configuration file.

If I make some changes in the configuration file then run this playbook you will get output.

SO , finally handler work with the notify to make service idempotant for restarting httpd service.

Thanks for reading…

--

--

No responses yet