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

Email ID Validation in Spring boot

Using TSQL to send SendGrid email

MECHANISM: FRAGMENTS -> GREEN LAND 🏕

Convert Python Code to EXE

Two Bet or not Two Bet?

What’s new in Windows 11

5 useful bulk management actions on minerstat dashboard

How Giant Swarm Enables a New Workflow

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

Detect unused secrets in your Github repositories

Slimming Docker images with Multistage Dockerfiles and DockerSlim

Automation — let’s sail together. Terraform Hello World on Windows 10