Creating Ansible playbook for customized apache server also solving the problem of service restart do not have idempotence property which is needed.

Shivani Mandloi
3 min readDec 9, 2020

if we normally want to configure apache webserver using ansible playbook

we usually follow three steps

  1. Installing httpd using package module in host computer
  2. coping your pre created html file in var/www/html while /var/www/htmal is a document root how we know this because its by default written in the httpd configuration file.
  3. and third is to start service
  4. lastly to allow port number 80 which is by default port number for httpd service.

but we can change this default document root and the port number as well.

to tell these things to our apache service we need to make these changes in apache configuration file /etc/httpd/conf.d

so we create a file in /etc/httpd/conf.d with the extension .conf and here we need to mention in this .conf file i have created lw.conf

  1. when you open this file you will see this kind of file. here i have changed the port number and document root as per my need.

2. Now for playbook we write a yml code

3. So here we can see when we ran playbook for first time it indicates changes and shows yellow color notification of changed.

when you again run the play book you will observe that it again shows yellow notification of changed that is even if you dont make any changes still it will retart your service again and again every time you run your play book

why?

because in service module restart state do not shows idempotence property so rather than this we can do something so that it only restart when you do some changes

we can solve this issue using handlers

What is handlers?

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 adding handlers in your playbook your can solve this issue.

Now when you change something only then your service will restart

And now if you again run the play book since there would be no changes it notifies green colour that is no changes

Here we successful done setup of customize httpd conf file ,port number and solving issue with restarting service!!

Hope you like it!!

keep learning!!!!

--

--