Restarting httpd Service is not idempotence in nature and also consume more resources to suggest a way to rectify this challenge in Ansible playbook
1 min readDec 1, 2020
Solution 🔥
Here we have to use the handlers
Code:
- hosts: HOST_NAMEvars:
- port: "4444"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: "myhttpd.conf"
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: startedhandlers:
- name: changed
service:
name: "httpd"
state: restarted
enabled: yes
- handlers have the capability if u changed something, first, you need to notify it and after use in the name of a handler.