Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

Visal Tyagi
DevOps-Guides
Published in
10 min readJun 19, 2024

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:

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study
Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

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”.

Select the Master Instance
Select the Master Instance

Step 2: Again, click on “Connect”.

Connect to the Master Instance
Connect to the Master Instance

Step 3: Go to the “hosts” file using the below-given command:

sudo nano /etc/ansible/hosts
Go to the hosts file
Go to the hosts file

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
Go to the roles directory
Go to the roles directory

Step 2: Run the below-given command to create an “apache” & “nginx” role:

sudo ansible-galaxy init apache
sudo ansible-galaxy init nginx
Create the roles
Create the roles

Step 3: Run the below-given command to view the roles:

ls

Both “apache” and “nginx” roles have been successfully created.

Created Roles
Created Roles

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
Go to the tasks folder
Go to the tasks folder

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
Create an install.yaml file
Create an install.yaml file

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
Apache2 Installation Script
Apache2 Installation Script

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
Create a configure.yaml file
Create a configure.yaml file

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
Paste the code in the configure.yaml file
Paste the code in the configure.yaml file

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
Open the main.yml file
Open the main.yml file

Step 7: Run this command to include “install.yaml” & “configure.yaml” to the “main.yml” file.

- include_tasks: install.yaml
- include_tasks: configure.yaml
Include the install.yaml & configure.yaml file here
Include the install.yaml & configure.yaml file here

Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.

Step 8: Go to the “handlers” section.

Open the handlers file
Open the handlers file

Step 9: Open the “main.yml” file using the command:

sudo nano main.yml
Open the main.yml file in handlers
Open the main.yml file in handlers

Step 10: Paste this code here:

- name: restart the apache web server
service: name=apache2 state=restarted
become: true
Apache Handlers File
Apache Handlers File

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
Create a sample.html file
Create a sample.html file

Step 12: Write this script here:

<h1>Welcome to Apache Website</h1>
Write the HTML Code
Write the HTML Code

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
Create the install.yaml file
Create the install.yaml file

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
NGINX Installation Script

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
configure.yaml in nginx
configure.yaml in nginx

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
NGINX Code
NGINX Code

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
Open the main.yaml file
Open the main.yaml file

Step 7: Run this command to include “install.yaml” & “configure.yaml” to the “main.yml” file.

- include_tasks: install.yaml
- include_tasks: configure.yaml
include install & configure yaml files to main.yml
include install & configure yaml files to main.yml

Do “CTRL+X” to exit & press “Yes” to save the file. Press “enter” from the keyboard.

Step 8: Go to the “handlers” section.

Go to the handlers
Go to the handlers

Step 9: Open the “main.yml” file using the below-given command in the “tasks” section:

sudo nano main.yml
tasks main.yml file
tasks main.yml file

Step 10: Paste this code here:

- name: restart the nginx web server
service: name=nginx state=restarted
become: true
nginx handlers main.yml file
nginx handlers main.yml file

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
Create sample1.html file
Create sample1.html file

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
Java Role Created
Java Role Created

Step 2: Go inside the “tasks” section under the “java” role using the command:

cd java/tasks
Go to the tasks folder
Go to the tasks folder

Step 3: Run this command to check the files here:

ls
Check the present files
Check the present files

The “main.yml” will be shown here.

Step 4: Open the “main.yml” file using the below-given command:

sudo nano main.yml
Open the main.yml file
Open the main.yml file

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
Java Installation Script
Java Installation Script

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 ..
Go to the roles section
Go to the roles section

Step 2: Create a “main.yaml” file using the command:

sudo nano main.yaml
Create a main.yml file
Create a main.yml file

Step 3: Paste this script here:

---
- hosts: apache
roles:
- apache
- hosts: nginx
roles:
- nginx

- hosts: apache
roles:
- java
- hosts: nginx
roles:
- java
main.yml script
main.yml script

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
Run main.yml file
Run main.yml file

All the configurations will be started running properly.

Configuration Run Properly
Configuration Run Properly
Configuration in Running Mode
Configuration in Running Mode

All the configurations have been successfully done now.

Configuration Done Successfully
Configuration Done Successfully

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.

Paste the Slave1 IP Address
Paste the Slave1 IP Address
Paste the Slave2 IP Address
Paste the Slave2 IP Address

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.

Paste the Sample File URL Here on Slave1
Paste the Sample File URL Here of Slave1
Paste the Sample File URL Here on Slave2
Paste the Sample File URL Here of Slave2

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.

Paste the Slave3 IP Address
Paste the Slave3 IP Address
Paste the Slave4 IP Address
Paste the Slave4 IP Address

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.

Paste the Sample1 File URL Here of Slave3
Paste the Sample1 File URL Here of Slave4
Paste the Sample1 File URL Here of Slave4

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
Check the Java Version
Check the Java Version
Java Installed Successfully on All Slave Machines
Java Installed Successfully on All Slave Machines

--

--