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

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…

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Connect An Existing User Base To XWiki

ROS: mobile robotics for everyone

6 Reasons Why I No Longer Use Bound Forms to Add Records in Microsoft Access

Why I built a bangin’ Wall Street stock and crypto price scroller

AWS::VPC — How to access private subnet machines ?

Don’t have a Constants file.

ETL For Malaysia’s 14th General Election for Selangor State Assembly

Continuous development using Git, Jenkins, Docker and Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Deepak Sharma

Deepak Sharma

More from Medium

BoomZ Review: One Click FREE Content & Traffic That Simply Works

AWS Solutions Architect Professional (SAP-C01) — Myths & Tricks — Part1

Oracle Transportation Management (OTM) vulnerabilities enable remote command execution and data…

Running D&D — Tips for the New and Old