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

saurabh kharkate
3 min readMar 21, 2021

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.

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 .

To solve this problem we can use Handlers

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 .

Handlers takes the responsibility of the tasks means when it should execute or not .Handlers are just like regular tasks in an Ansible playbook but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.

This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage.

  • notify : notify keyword tells the handler to run the task only when some changes are made in the configuration file.
  • Handlers in ansible to be triggered only when http configuration file changes and then handler will restart httpd service.

If you run the playbook again then it will shows that Your service is started so no need the restart again this become possible because of the handlers and notify keywords in ansible.

lets do with practical

  • Here we write a playbook to restart the httpd server if we making some changes in configuration file.

Ansible Playbook :

- hosts: webserver
vars_files: var.yml
tasks:
- file:
state: directory
path: "{{ dvd }}"
- mount:
src: "/dev/cdrom"
path: "{{ dvd }}"
state: mounted
fstype: "iso9660"
- yum_repository:
baseurl: "file:///{{ dvd }}/AppStream"
name: "dvd1"
description: "my yum1"
gpgcheck: no
- yum_repository:
baseurl: "file:///{{ dvd }}/BaseOS"
name: "dvd2"
description: "my yum2"
gpgcheck: no
- name: " Installing httpd package"
package:
name: "httpd"
state: present
- name: "Creating directory for web_server"
file:
state: directory
path: "{{ doc_root }}"
register: y
- name: "Copying Configuration file"
template:
dest: "/etc/httpd/conf.d/lw.conf"
src: "lw.conf"
notify:
- Restart service
- name: "Coppying web pages"
copy:
content: "<h1>Task 11 Completed Successfuly </h1>"
dest: "{{ doc_root }}/index.html"
when: y.failed == false
- name: "Starting httpd Service"
service:
name: "httpd"
state: started
- firewalld:
port: "{{ port_no }}/tcp"
state: enabled
immediate: yes
handlers:
- name: Restart service
service:
name: "httpd"
state: "restarted"
  • Here we see that every tasks run properly and httpd idempotence problem get solved.

Finally Task completed Successfully !!!! 😃😃😉

🙏🙏Thanks for Reading 🙏🙏

☘☘Keep Sharing!!! , Keep Learning!!! ☘☘

--

--