Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4

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

Tasks to Be Performed:

1. Use the previous deployment of the Ansible cluster

2. Configure the files folder in the role with index.html which should be replaced with the original index.html

All of the above should only happen on the slave that has NGINX installed using the role.

Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4
Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4

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

Before checking this assignment solution, please check the Ansible-Assignment-3 Solution. When you check the previous assignment, you can easily understand the task we have performed here.

Problem (1) Solution: Use the previous deployment of the Ansible cluster

We are using the previous assignment (Ansible-Assignment-3) configuration here.

Step 1: Go to the “Slave2” & copy the public IP address (13.232.149.158) in the browser address bar. You will notice that by default “nginx” has been installed on “Slave2”.

NGINX Installed
NGINX Installed

Now, we will replace this by default nginx index.html file with another custom index.html file. So, the content will be changed.

Problem (2) Solution: Configure the files folder in the role with index.html which should be replaced with the original index.html

All of the above should only happen on the slave that has NGINX installed using the role.

Step 1: Go to the “Roles” using the below-given command:

cd /etc/ansible/roles

You will land inside the “roles” directory.

To view the roles here, use the below-given command:

ls  
Roles
Roles

You will notice that two roles “apache2” & “nginx” have been present.

Step 2: Go to the “nginx” role using the below-given command:

cd nginx
files directory
files directory

You will notice that the “files” directory is present here.

Step 3: To view in which directory you are inside, run the below-given command:

pwd
Copy the Path of NGINX file
Copy the Path of NGINX file

Copy the source in a separate notepad.

Step 4: Create an “index.html” file using the below-given command:

sudo nano index.html
Create index.html file
Create index.html file

Step 5: Paste this content in the HTML file here:

<title>Hello Intellipaat</title>
<h1>This Is a Demo File</h1>
<p>Custom Index.html</p>
<p>Warm Regards</p>
Paste the Content Here
Paste the Content Here

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

Step 6: Exit from the file to change the directory from “files” to the “tasks” using the below-given command:

cd tasks  

Run the below-given command to check the files in the “tasks” folder:

ls  
Go to the tasks folder
Go to the tasks folder

You will notice that two “YAML” files are present here.

Step 7: Create a copy.yaml file using the below-given command:

sudo nano copy.yaml
Create a copy.yaml file
Create a copy.yaml file

Step 8: Paste the below-given content in the copy.yaml file:

- name: copying new index.html file
copy: src=/etc/ansible/roles/nginx/files/ dest=/var/www/html
Paste the path to the copy.yaml file
Paste the path to the copy.yaml file

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

Step 9: Run the below-given command to check the copy.yaml file created or not:

ls
Check the Created YAML Files
Check the Created YAML Files

The “copy.yaml” file has been successfully created.

Step 10: Include the copy.yaml file in the “main.yml” file using the following command:

-include_tasks: copy.yaml

Open the main.yml file using the below-given command:

sudo nano main.yml
Open the main.yaml file
Open the main.yaml file
Add copy.yaml to main.yml file
Add copy.yaml to main.yml file

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

Step 11: Go to the “ansible” directory, where plays are present using the below-given command:

cd ..

Run the below-given command to check the YAML files:

ls  
Check the Files Here to Exit from the Directories
Check the Files Here to Exit from the Directories

All the “YAML” files are shown here.

Step 12: Create a “play4.yaml” file using the below-given command:

sudo nano play4.yaml
Create the play4.yaml file
Create the play4.yaml file

Step 13: Paste this content in the file here:

---
- name: installing nginx on Slave2
hosts: Slave2
become: true
roles:
- nginx
Paste the Code in play4.yaml file
Paste the Code in play4.yaml file

Step 14: Now, check the syntax of ansible-playbook using the below-given command:

ansible-playbook play4.yaml --syntax-check
Check the Synatx of the File
Check the Synatx of the File

Step 15: Execute this command to dry run the play4.yaml file:

ansible-playbook play4.yaml --check
Dry run the play4.yaml file
Dry run the play4.yaml file

The play will be successfully dry run.

Step 16: Now, we will run the playbook using the below-given command:

ansible-playbook play4.yaml
Run the play4.yaml file
Run the play4.yaml file
Play Run Successfully
Play Run Successfully

The play will be successfully run.

Step 17: Copy the “Slave2” IP Address from the “Instances” section in “AWS”.

Copy the Slave2 IP Address
Copy the Slave2 IP Address

Step 18: Paste the “IP Address (13.232.149.158) in the “browser address bar”. Press “enter” from the keyboard.

Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4
New index.html file

Previous “index.html” file using the new custom “index.html” file.

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

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

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

--

--