Restarting Webserver (Httpd) Service is not idempotence in nature

Neeraj Singh Negi
2 min readDec 21, 2020

Restarting the webserver consumes more resources. To solve this challenge we use Ansible-Playbook

- hosts: 192.168.43.109
vars:
- port_no: "8012"
- dir: "/var/www/web"
- localyum:
- name: DVD1
description: Local DVD1
baseurl: "file///dvd/AppStream"
gpgcheck: no
enabled: yes
- name: DVD2
description: Local DVD2
baseurl: "file///dvd/BaseOS"
gpgcheck: no
enabled: yes
tasks:
- name: Creating directory for DVD
file:
path: /dvd
state: directory
- name: Mounting DVD
mount:
src: /dev/cdrom
path: /dvd
state: present
fstype: xfs
- name: Configuring local Repo
yum_repository:
name: "{{ item.name }}"
description: "{{ item.description }}"
baseurl: "{{ item.baseurl }}"
gpgcheck: "{{ item.gpgcheck }}"
enabled: "{{ item.enabled }}"
with_items: "{{ localyum }}"- name: Installing httpd webserver
package:
name: "httpd"
state: present
- name: Creating directory
file:
path: "{{ dir }}"
state: directory

- name: Copy webserver content
copy:
src: index.html
dest: "{{ dir }}/index.html"
- name: Checking Webserver Configuration file
template:
dest: "/etc/httpd/conf.d/port-dr.conf"
src: "port-dr.conf.j2"
notify:
- config-changed
- name: Adding rule for http
firewalld:
port: "{{ port_no }}/tcp"
state: enabled
permanent: yes
immediate: yes
- name: starting webserver service
service:
name: "httpd"
state: started
handlers:
- name: config-changed
service:
name: "httpd"
state: restarted
enabled: yes

Webserver (Httpd) Template File (port-dr.conf.j2)

Listen {{ port_no }}
<VirtualHost {{ ansible_facts['default_ipv4']['address'] }}:{{ port_no }}>
DocumentRoot {{ dir }}
</VirtualHost>

Webserver Page (index.html)

Hey ! It works...

Click to see the code Github.

--

--

Neeraj Singh Negi

AWS | Openstack | GCP | Ansible | Redhat 8 Linux | Docker | Jenkins | Kubernetes | Hadoop | Python | Machine Learning