🔰 RECTIFICATION : Restarting HTTPD Service is not
idempotence in nature and also consume more
resources suggest a way to rectify this challenge
in Ansible playbook
Lets see how we can rectify this challenge ….
🟥Using when Keyword: Conditional expression, determines if an iteration of a task is run or not…..
🟥Using handlers : Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.
✍While ,we copy webpage to directory /var/www/html(root_document)
we have register the variable : rootdoc which will keep track of changes / updation made in directory
- name : "Copying wepages to server"
tags : rootdoc
copy :
src : index.html
dest : /var/www/html/
register : rootdoc
👉 Now we have rootdoc as notifier , we can use this in the when keyword , that is if we have changes to directory restart the service else leave it untouched if running ……
- name : Service status
service :
name : httpd
state : restarted
when : rootdoc.changed == true
Thus , we can achieve idempotence using WHEN keyword
The entire playbook is wriiten as followed :
- hosts: 192.168.0.126
tasks:
- name : Mounting dvd
mount :
src : "/dev/cdrom"
path : "/dvd"
state : mounted
fstype : "iso9660"- name : "Setting up yum repo"
yum_repository :
name : yum1
description : "Yum repo 1"
baseurl : "/dvd/AppStream"
gpgcheck : 0- name : "Setting yum repo "
yum_repository :
name : yum2
description : "Yum repo 2"
baseurl : "/dvd/BaseOS"
gpgcheck: 0- name : "Installing net-tools for cli "
yum :
name : net-tools
state : present
- name : "Installing httpd package"
yum :
name : httpd
state : present- name : "Copying wepages to server"
tags : rootdoc
copy :
src : index.html
dest : /var/www/html/
register : rootdoc
- debug :
msg : "{{ rootdoc }}"- name : "Starting httpd server"
service :
name : httpd
state : started
enabled : yes
ignore_errors: yes- name : Service status
service :
name : httpd
state : restarted
when : rootdoc.changed == true
🎫Github link : Idempotemce_httpd
Keep learning …..🎁
Keep Motivating 🏆