Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study
Problem Statement:
You are a DevOps Engineer and the organization you are working on needs to set up two configuration management server groups. One for Apache, and another for NGINX. Being a DevOps Engineer it is your task to deal with this configuration management issue.
Let us see the tasks that you need to perform using Ansible:
1. Create two server groups. One for Apache and another for NGINX
2. Push two HTML files with their server information
Make sure that you don’t forget to start the services once the installation is done. Also, send post-installation messages for both server groups.
Using Ansible roles accomplish the above tasks. Also, once the Apache server configuration is done you need to install Java on that server group using Ansible role in a Playbook.
Note: We have already Four Slave Machines & One Master Machine, where we have installed Ansible properly in previous assignments. So we will start with creating the testing group.
Before checking this case study, kindly check these assignments for more clarity:
Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1
Create a Custom Script & Add the file on Slaves Machine via Playbook — Ansible Assignment 2
Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3
Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4
Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5
Check the Git Hub Repository for this Assignment to Copy the Commands:
A. Create two server groups. One for Apache and another for NGINX
Step 1: Go to the “Master” machine & click on “Connect” in “EC2 Instances”.
Step 2: Again, click on “Connect”.
Step 3: Go to the “hosts” file using the below-given command:
sudo nano /etc/ansible/hosts
Step 4: Put the first two slaves (Slave1 & Slave2) machines in “apache” & the last two slaves (Slave3 & Slave4) machines in the “nginx” group.
[apache]
Slave1 ansible_host=172.31.47.187
Slave2 ansible_host=172.31.45.194
[nginx]
Slave3 ansible_host=172.31.38.118
Slave4 ansible_host=172.31.32.77
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
B. Create Two Roles Named as “apache” & “nginx”
Step 1: First, we will create two roles, one is “apache” & second is “nginx”.
Go to the “roles” directory using the below-given command:
cd /etc/ansible/roles
Step 2: Run the below-given command to create an “apache” & “nginx” role:
sudo ansible-galaxy init apache
sudo ansible-galaxy init nginx
Step 3: Run the below-given command to view the roles:
ls
Both “apache” and “nginx” roles have been successfully created.
C. Create Files Under Apache Roles to Install “apache” & Put a Sample HTML File Here
Step 1: Go to the “tasks” folder under apache role using the below-given command:
cd apache/tasks
Step 2: Here, only the “main.yml” file. Now, we will create an “install.yaml” file to install & restart the apache2.
Run the below-given command:
sudo nano install.yaml
Step 3: Paste this script to install & restart “apache” with the main configuration.
- name: installing apache2 on apache server group
apt: name=apache2 update_cache=yes state=latest
become: true
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 4: Create a “configure.yaml” file to access the “sample.html” file on the apache server.
Create a configure.yaml file using the below-given command:
sudo nano configure.yaml
Step 5: Paste this code to copy the “sample.html” file on the apache server using this script:
- name: copy the html file
become: true
copy: src=sample.html dest=/var/www/html
notify:
- restart the apache web server
Step 6: Now, open the “main.yml” to include the “install.yaml” & “configure.yaml” here.
Use the below-given command to open the main.yml file:
sudo nano main.yml
Step 7: Run this command to include “install.yaml” & “configure.yaml” to the “main.yml” file.
- include_tasks: install.yaml
- include_tasks: configure.yaml
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 8: Go to the “handlers” section.
Step 9: Open the “main.yml” file using the command:
sudo nano main.yml
Step 10: Paste this code here:
- name: restart the apache web server
service: name=apache2 state=restarted
become: true
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 11: Now, go to the “files” section & create an HTML file named “sample.html” using the below-given command:
sudo nano sample.html
Step 12: Write this script here:
<h1>Welcome to Apache Website</h1>
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
C. Create Files Under NGINX Roles to Install “nginx” & Put a Sample HTML File Here
Step 1: Go to the “tasks” folder under the nginx role using the below-given command:
cd nginx/tasks
Step 2: Here, only the “main.yml” file is present. Now, we will create an “install.yaml” file to install & restart the nginx.
Run the below-given command:
sudo nano install.yaml
Step 3: Paste this script to install & restart “nginx” with mail configuration.
- name: installing nginx on nginx server
apt: name=nginx update_cache=yes state=latest
become: true
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 4: Now, create a “configure.yaml” file to access the “sample.html” file on the apache server.
Create a configure.yaml file using the command:
sudo nano configure.yaml
Step 5: Paste this code to copy the “sample1.html” file on the nginx server using this script:
- name: copy the html file
become: true
copy: src=sample.html dest=/var/www/html
notify:
- restart the nginx web server
Step 6: Now, open the “main.yml” to include the “install.yaml” & “configure.yaml” here. Use the below-given command to open the main.yml file:
sudo nano main.yml
Step 7: Run this command to include “install.yaml” & “configure.yaml” to the “main.yml” file.
- include_tasks: install.yaml
- include_tasks: configure.yaml
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 8: Go to the “handlers” section.
Step 9: Open the “main.yml” file using the below-given command in the “tasks” section:
sudo nano main.yml
Step 10: Paste this code here:
- name: restart the nginx web server
service: name=nginx state=restarted
become: true
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 11: Now, go to the “files” section & create an HTML file named “sample1.html” using the below-given command:
sudo nano sample1.html
Step 12: Write this script here:
<h1>Welcome to NGINX Website</h1>
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
D. Create a “Java” Role & Configure “Java” in the “main.yml” file
Step 1: Go to the “roles” section & create a role named “java” using the command:
sudo ansible-galaxy init java
Step 2: Go inside the “tasks” section under the “java” role using the command:
cd java/tasks
Step 3: Run this command to check the files here:
ls
The “main.yml” will be shown here.
Step 4: Open the “main.yml” file using the below-given command:
sudo nano main.yml
Step 5: Paste this script here:
---
# tasks file for java
- name: install java on webserver
apt: name=openjdk-18-jre-headless state=present
become: true
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
E. Create a “main.yaml” File to Run All The Configuration on Both “Slave” Groups
Step 1: First, go to the “roles” section using the below-given command two times:
cd ..
Step 2: Create a “main.yaml” file using the command:
sudo nano main.yaml
Step 3: Paste this script here:
---
- hosts: apache
roles:
- apache
- hosts: nginx
roles:
- nginx
- hosts: apache
roles:
- java
- hosts: nginx
roles:
- java
Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.
Step 5: Run the playbook using the below-given command:
ansible-playbook main.yaml
All the configurations will be started running properly.
All the configurations have been successfully done now.
F. Check that “apache” & “nginx” And “Java” is installed on Slaves Group Successfully or Not
Step 1: First, paste the “Slave1 Public IP Address “ & “Slave2 Public IP Address” on the browser address bar. Press “Enter” from the keyboard.
The Apache Configuration will be done successfully.
Step 2: Now, paste the “sample.html” behind the “Slave1 Public IP Address “& “Slave2 Public IP Address” on the browser address bar. Press “Enter” from the keyboard.
The HTML Configuration will be done successfully.
Step 3: Second, paste the “Slave3 Public IP Address “ & “Slave4 Public IP Address” on the browser address bar. Press “Enter” from the keyboard.
The Nginx Configuration will be done successfully.
Step 4: Now, paste the “sample1.html” behind the “Slave3 Public IP Address “& “Slave4 Public IP Address” on the browser address bar. Press “Enter” from the keyboard.
The HTML Configuration will be done successfully.
Step 5: Go to all slaves & type the below-given command one by one, the Java will be successfully installed like this:
java --version