Making HTTPD Service Idempotent using Ansible Playbook
2 min readJan 15, 2021
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.