Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3
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:
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
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
The role “apache2” has been successfully created.
Step 3: Create another role “nginx” using the below-given command:
sudo ansible-galaxy init nginx
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
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
Step 3: For installing apache2, we will create an “install.yaml” file using the below-given command:
sudo nano install.yaml
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
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
Step 8: Write the below-given command here:
–include_tasks: install.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 ..
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
Step 12: Create an “install.yaml” file using the command:
sudo nano install.yaml
Step 13: Paste this command here:
- name: install nginx
apt: name=nginx update-cache=yes state=latest
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
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
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
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
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
The dry run will be successful.
Step 5: Now, we will run “play3.yaml” using the below-given command:
ansible-playbook play3.yaml
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.
Step 7: Copy the Slave2 Public IP address (3.108.227.113) into the browser address bar. NGINX will be successfully 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