Restarting HTTPD Service is not idempotent in nature and also consume more resources suggest a way to rectify this challenge in Ansible Playbook
By: Shashwat Singh
Tags: Httpd, Problem Solving, Ansible Playbook, Ansible, Resolve

Restarting HTTPD Service is not idempotent in nature and also consume more resources suggest a way to rectify this challenge in Ansible Playbook Shashwat Singh Follow Dec 27, 2020 · 3 min read

Problem Statement :

Ansible nature follows idempotent but some modules and keywords don’t follow idempotent. In this blog we are about to solve one problem where one module keyword doesn’t follow the idempotent i.e., restarted HTTPD service.

I have two solutions for the above problem statement. I am about to show the first solution by the help of condition.

1> First Method

- hosts: all

tasks: - file:

state: directory

path: "/mount" - mount:

src: "/dev/cdrom"

path: "/mount"

state: mounted

fstype: "iso9660" - yum_repository:

baseurl: "/mount/AppStream"

name: "RepO1"

description: "This is the configuration for the Yum !"

gpgcheck: no - yum_repository:

baseurl: "/mount/BaseOS"

name: "RepO2"

description: "This is the configuration for the Yum !"

gpgcheck: no



- package:

name: "httpd"

state: present



- file:

state: directory

path: "/var/www/NewDir" - copy:

dest: "/etc/httpd/conf.d/new.conf"

src: "new.conf"

register: info



- copy:

dest: "/var/www/NewDir/index.html"

content: "Welcome TO Coding Pathshala

" - name: "Normally Starting the Service of httpd"

service:

name: "httpd"

state: started

enabled: yes when: info.changed == false - firewalld:

port: 8080/tcp

state: enabled

immediate: yes - name: "httpd_service_restart"

service:

name: "httpd"

state: "restarted"

when: info.changed == true

So, with the help of register module I stored the value which is the metadata of that module in a variable . I used the condition the value is stored in the form of a map. It consists of a key named changed if the value of key is false it implies that module has not made any changes and if the value of key is true then the module has made changes.

We will use the register module with the copy module where we can change the configuration of the file (When we make some changes in the configuration file then only we need to restart the service).

The register module will help us to store the metadata of copy module in the variable. The value is stored in the form of a map (key value pair). We will use this variable two times.

At first, we are using this variable when we start the web server by the help of keyword started and where we start the services with the help of keyword restarted.

The started keyword will only run when the condition is there

variable_name.changed==false

the restarted module will run when the condition is there i.e.

variable_name.changed==true

2> Second Method

Second Perfect Soln is that when something changed in the config file, then only run the service as restarted i.e. if that config file is not changed then don’t restart (always follow the Idempotent Concept).

This is known as the Notifies ( notify is also module which exist in Ansible). So, we need to put this module in the Restarted Service task under Handler..

So that if this Module notify that there is some changes in Config then the service which restarted which is called by the Handler ..

- hosts: all

tasks: - file:

state: directory

path: "/appdvd" - mount:

src: "/dev/cdrom"

path: "/mount"

state: mounted

fstype: "iso9660" - yum_repository:

baseurl: "/mount/AppStream"

name: "RepO1"

description: "This is the configuration for the Yum !"

gpgcheck: no - yum_repository:

baseurl: "/mount/BaseOS"

name: "RepO2"

description: "This is the configuration for the Yum !"

gpgcheck: no



- package:

name: "httpd"

state: present



- file:

state: directory

path: "/var/www/NewDir" - copy:

dest: "/etc/httpd/conf.d/new.conf"

src: "new.conf"

notify: httpd_service_restart



- copy:

dest: "/var/www/NewDir/index.html"

content: "Welcome TO Coding Pathshala

" - service:

name: "httpd"

state: started

enabled: yes



- firewalld:

port: 8080/tcp

state: enabled

immediate: yes handlers:

- name: httpd_service_restart

service:

name: "httpd"

state: "restarted"

Thanks for reading this Blog !!

Wants to download the Playbooks then head over to the github →

https://github.com/Shashwatsingh22/Ansible