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

Omprakash Choudhari
3 min readMar 22, 2021

We know that Service module is idempotent in nature, means if service is already started then Ansible will not start it again. Actually this is good but problem arises when we change something in configuration file and we know once we change the configuration file we need to restart the service, but Ansible will not start the service agian as it is idempotent in nature. So here we can use one trick we can tell Ansible to restart the service always when we run playbook. But in restart also we have one problem that is Ansible will restart the service even if we don’t change anything 😑 .

So what we can do now ?🤔

we want that when we change the configuration file that time only service should restart otherwise no need to restart service. So for this in Ansible we have handlers and notify concept.

Handlers are tasks that only run when notified. It means whatever tasks we write under Handlers will only get run if some other task will notify it. In our case we want if configuration task changes then it should notify httpd restart task.

Now let’s begin !!

Ansible inventory

Ansible Playbook

So we will change configuration file of apache webserver. So we will host our webapp at port number which user will tell and will also change document root(directory where we keep webapp). So for this we are taking input from user :

- hosts: apache
vars_prompt:
- name: doc_root
prompt: "Type Document root"
private: no
- name: http_port
prompt: "Give Port No"
private: no

After this we have to Download httpd software

tasks:
- name: Download Httpd
package:
name: httpd
state: present

Now we have to also create directory(Document root) which we taken input from user.

- name: Creating Document root
file:
path: '{{doc_root}}'
state: directory

This is configuration file of httpd which we will send to remote system where webapp will run. This is in the form of template and we used the input variables.

Now we are uploading the configuration file which we just written. And we also want if something change in this file then it should notify restart Apache task which we will write under handler.

- name: Configuration
template:
src: 'op.conf.j2'
dest: '/etc/httpd/conf.d/op.conf'
notify:
- Restart Apache

Also we have to upload webapp in the document root directory.

- name: Copying App
copy:
src: 'index.html'
dest: '{{doc_root}}/index.html'

This is how we write tasks in handlers here all tasks should have unique name. We know configuration task will notify Restart Apache task.

handlers:
- name: Restart Apache
service:
name: httpd
state: restarted
enabled: yes

All seems ok! but no we have one bug here what if someone/somehow stop httpd service in remote system. So to overcome this bug we want some task which will keep service started.

- name: Starting service
service:
name: "httpd"
state: started

Finally we have completed our Playbook 🤩

Let’s Run the playbook :)

we can see port number is been also changed ;)

Thanks for reading this Article 🤗..

--

--