Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4
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.
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”.
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
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
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 source in a separate notepad.
Step 4: Create an “index.html” file using the below-given command:
sudo nano index.html
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>
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
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
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
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
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
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
All the “YAML” files are shown here.
Step 12: Create a “play4.yaml” file using the below-given command:
sudo nano play4.yaml
Step 13: Paste this content in the file here:
---
- name: installing nginx on Slave2
hosts: Slave2
become: true
roles:
- nginx
Step 14: Now, check the syntax of ansible-playbook using the below-given command:
ansible-playbook play4.yaml --syntax-check
Step 15: Execute this command to dry run the play4.yaml file:
ansible-playbook play4.yaml --check
The play will be successfully dry run.
Step 16: Now, we will run the playbook using the below-given command:
ansible-playbook play4.yaml
The play will be successfully run.
Step 17: Copy the “Slave2” IP Address from the “Instances” section in “AWS”.
Step 18: Paste the “IP Address (13.232.149.158) in the “browser address bar”. Press “enter” from the keyboard.
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