Making HTTPD Service Idempotent using Ansible Playbook

Problem:-

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

Solution:-

This problem can easily be rectified using handlers.

A Handler is exactly the same as a Task, but it will run when called by another Task. A Handler will take an action when called by an event it listens for.

This is useful for secondary actions that might be required after running a Task, such as starting a new service after installation or reloading a service after a configuration change.

Code:-

- hosts: <host-name>  vars:
- port: "<port>"

tasks:
- name: "Add EPEl repo"
yum_repository:
name: epel
description: "EPEL YUM repo"
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/

- name: "Install httpd"
package:
name: "httpd"
state: present

- name: "Copying conf file"
template:
dest: "/etc/httpd/conf.d/myhttpd.conf"
src: "<src-file>"
notify:
- changed
- name: "Adding rule for http"
firewalld:
port: "{{ port }}/tcp"
state: enabled
permanent: yes
immediate: yes
- name: "Staring httpd service"
service:
name: httpd
state: started
handlers:
- name: changed
service:
name: "httpd"
state: restarted
enabled: yes

Note: Click here for Github Link.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

OpenStack Overcloud Architecture

METACY(MTC) Listing Event on DigiFinex!

TCMSecurity | Dev | Write-up

Add Simple Authentication On Your Flask-Restful App

What I’ve learned while using Spinnaker for our deployments ?

Gunbot 5th Anniversary Tournament Champions Interview

Gunbot 5th Anniversary Tournament Champions Interview

minerstat mining tutorial #4: Profit switch and mining calculator

How to create a highly performant multi-currency solution in SSAS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Karan Agrawal

Karan Agrawal

More from Medium

CREATE DATABASE MySQL with C# & WinForms

-database-

Apache Guacamole RDP with Docker

Linux Crash Course — Part 3

Check for more than one condition in Ansible