⚜ CONFIGURATION OF APACHE WEBSERVER USING ANSIBLE ⚜
🔰 What is Ansible?
📃 Ansible is a configuration management system written in Python using a declarative markup language to describe configurations. It is used to automate software configuration and deployment.
⭐ Let’s Begin….. ⭐
♦️ Task Description:-
◾Restarting HTTPD Service is not idempotence in nature and also consume more resources.. Suggest a way to rectify this challenge in Ansible Playbook.
♦️ ANSWER :-
◾Automate the Ansible Playbook with Handlers..
♦️ What is Handlers in Ansible?
◾Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something.
⭐ In Controller Node,
👉🏻 Variable File :
port_no: "8080"
dvd_dir: "/dvd1"
doc_root: "/var/www/first"
👉🏻 Ansible Play Book :
- hosts: <ip of webserver>
vars_files:
- vars.yml tasks:- name: "Creating Directory For Mounting"
file:
state: directory
path: "{{ dvd_dir }}"- name: "Mounting...."
mount:
src: "/dev/cdrom"
fstype: iso9660
state: mounted
path: "{{ dvd_dir }}"- name: "Yum repo AppStream"
yum_repository:
name: "dvd1"
description: "AppStream"
baseurl: "{{ dvd_dir }}/AppStream"
gpgcheck: no- name: "Yum repo BaseOS"
yum_repository:
name: "dvd2"
description: "BaseOS"
baseurl: "{{ dvd_dir }}/BaseOS"
gpgcheck: no- name: "Install httpd"
package:
name: "httpd"
state: present- name: "Start httpd services"
service:
name: "httpd"
state: started- name: "Copying Configuration File"
template:
src: "/root/ansible-ws/httpd/httpc.conf"
dest: "/etc/httpd/conf.d/httpc.conf"
notify: Restart Service- name: "Enabling Firewall"
firewalld:
port: "{{ port_no }}/tcp"
state: enabled
permanent: yes
immediate: yes- name: "New Document Root Directory"
file:
state: directory
path: "{{ doc_root }}"- name: "Copying content to document Root"
copy:
content: "TASK 11.3 COMPLETED SUCCESSFULLY"
dest: "{{ doc_root }}/index.html"
👉🏻 Configuration file of webserver :
#Configuration file of webserver#Listen {{ port_no }}<virtualhost {{ ansible_facts['default_ipv4']['address'] }}:{{ port_no }}>
DocumentRoot {{ doc_root }}
</virtualhost>
👉🏻 Answer :
Adding Handlers to Play Book
***********************************
#Adding Handlers#handlers:
- name: "Restart Service"
service:
name: "httpd"
state: restarted ***********************************
👉🏻 Now running Play-Book :
ansible-playbook -v <playbook_name>
⭐ Here, you can see in Handlers it restarts service only when there is changes in Configuration File. ⭐
👉🏻 Let’s Check in Target Node :
- Configuration File is copied
- Content is written in configuration file
- Changed the Document root from “/var/www/html” to “/var/www/first”
- Works on port no. 8080