Restarting HTTPD service in idempotence nature in Ansible

Ayush Rawat
2 min readDec 25, 2020

By default, the restarted argument in the service module is not idempotent so whenever any change occurs or not occurs it always executes which is extra process consumes RAM and CPU.

So to make it idempotent there is a keyword called handlers and notify in Ansible which helps to make it idempotent.

- hosts: webserver
vars_files:
- "/root/webserverws_ansible/variables.yml"
tasks:
- name: Installing httpd package
package:
name: httpd
state: present
- name: Creating Webserver Directory
file:
path: "{{webdir}}"
state: directory
- name: Copying Website Content
copy:
src: "{{ webcontent }}"
dest: "{{ webdir }}"
- name: Changing Configuration File
template:
src: "{{ confsrc }}"
dest: "{{ confdest }}"
notify: Restarting httpd services
- name: Configuring Firewall
firewalld:
port: "8080/tcp"
state: enabled
permanent: yes
immediate: yes
handlers:
- name: Restarting httpd services
service:
name: httpd
state: restarted

handlers are like functions in other programming languages when they are called they execute in the above playbook we have put the task of Restarting httpd services in the handlers. notify are like a function call here it works like when it detects any changes in the task it executes task inside the handlers else it never executes for this we have to write keyword notify then the name of the task. Here in this playbook we call Restarting httpd services from the module of Changing Configuration File it will detect the changes that occur in the configuration file and will execute handler when it occurs and not execute when changes not occur.

Thanks for Reading

--

--