Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3

Visal Tyagi
DevOps-Guides
Published in
5 min readJun 12, 2024

Tasks to Be Performed:

1. Create 2 Ansible roles

2. Install Apache2 on slave1 using one role and NGINX on slave2 using the other role

3. The above should be implemented using different Ansible roles.

Check the Git Hub Repository for this Assignment to Copy the Commands:

Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3
Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3

Problem (1) Solution: Create 2 Ansible Roles

Step 1: Go to the “Master”. Change the directory & go to the “ansible” directory using the below-given command:

cd /etc/ansible

To view the “roles” directory, use the below-given command:

ls
Go to the “roles” directory
Go to the “roles” directory

The “roles” directory will be found here.

Step 2: Go to the “roles” directory using the below-given command:

cd roles

Now, we will create a role “apache2” using the below-given command:

sudo ansible-galaxy init apache2
Create apache2 role
Create apache2 role

The role “apache2” has been successfully created.

Step 3: Create another role “nginx” using the below-given command:

sudo ansible-galaxy init nginx
Create NGINX role
Create NGINX role

Problem (2) Solution: Install Apache2 on slave1 using one role and NGINX on slave2 using the other role

Step 1: Go inside the role using the below-given command:

cd apache2
Go to the “apache2” role
Go to the “apache2” role

You will land inside the role.

Step 2: Run the below-given command to check how many files & the directory are present:

ls

Go to the “tasks” folder using the below-given command:

cd tasks
Go to the “tasks” folder
Go to the “tasks” folder

Step 3: For installing apache2, we will create an “install.yaml” file using the below-given command:

sudo nano install.yaml
Create an install.yaml file
Create an install.yaml file

Step 4: Paste the below content in the “install.yaml” file to install apache2.

- name: installing apache2
apt: name=apache2 update-cache=yes state=latest
Install Apache2
Install Apache2

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

Step 5: Include the “install.yaml” to the “main.yaml” file here.

Run the below-given command to open the “main.yaml” file:

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

Step 8: Write the below-given command here:

–include_tasks: install.yaml
Include the install.yaml file to main.yaml
Include the install.yaml file to main.yaml

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

Step 10: Change the directory by running the below-given command twice:

cd ..
Go to the roles
Go to the roles

You will notice the two roles here.

Step 11: Change the directory to nginx using the below-given command:

cd nginx

Run this command to view the directory here:

ls

You will notice the “tasks” directory.

Go to the“tasks” directory using the below-given command:

cd tasks
Go to the tasks folder
Go to the tasks folder

Step 12: Create an “install.yaml” file using the command:

sudo nano install.yaml
Create an install.yaml file
Create an install.yaml file

Step 13: Paste this command here:

- name: install nginx
apt: name=nginx update-cache=yes state=latest
Install NGINX
Install NGINX

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

Step 14: Now, add this “install.yaml” file to “main.yml”.

Write the below-given command:

-include_tasks: install.yaml
Include the install.yaml file to main.yaml
Include the install.yaml file to main.yaml

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

Problem (3) Solution: The Above should be implemented using different Ansible roles

Step 1: Go to the “/etc/ansible” directory using the below-given command:

cd /etc/ansible

Create a play3.yaml file using the below-given command:

sudo nano play3.yaml
Create the play3.yaml file
Create the play3.yaml file

Step 2: Write the file to run the script to install the given tools using the roles.

---
- name: installing apache2 on slave1
hosts: Slave1
become: true
roles:
- apache2
- name: install nginx on slave2
hosts: Slave2
become: true
roles:
- nginx
install.yaml file
install.yaml file

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

Step 3: Now, we will check the syntax of the “play3.yaml” file using the below-given command:

ansible-playbook play3.yaml --syntax-check
Syntax Check of Install.yaml file
Syntax Check of Install.yaml file

The playbook syntax is running fine.

Step 4: Now, we will dry run of “play3.yaml” using the below-given command:

ansible-playbook play3.yaml --check
Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3
File Dry Run
Dry Run Succesful
Dry Run Succesful

The dry run will be successful.

Step 5: Now, we will run “play3.yaml” using the below-given command:

ansible-playbook play3.yaml
Run the play3.yaml file
Run the play3.yaml file
Play Runned Successfully
Play Runned Successfully

The file will be successfully run.

Step 6: Copy the Slave1 Public IP address (35.154.71.243) into the browser address bar. Apache2 will be successfully installed.

Apache2 Installed
Apache2 Installed

Step 7: Copy the Slave2 Public IP address (3.108.227.113) into the browser address bar. NGINX will be successfully installed.

NGINX Installed
NGINX Installed

Check Other Ansible Assignments Here:

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

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

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

--

--