Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5
Tasks to Be Performed:
1. Create a new deployment of an Ansible cluster of 5 nodes
2. Label 2 nodes as test and the other 2 as prod
3. Install Java on test nodes
4. Install MySQL server on prod nodes
Use Ansible roles for the above and group the hosts under test and prod.
Check the Git Hub Repository for this Assignment to Copy the Commands:
Check these Assignments Before checking Assignment 5 Solution:
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
Problem (1) Solution: Create a new deployment of an Ansible cluster of 5 nodes
Follow these steps:
A. Launch Two Instances as “Slave3” & “Slave4”
Step 1: We have three instances launched, which are named “Master”, “Slave1” & “Slave2”. Now, we will launch another two instances as well.
Step 2: Click on “Launch instances”.
Step 3: Write “Name” as “Slave” in “Name and tags”.
Step 4: Choose “AMI” as “Ubuntu” & its version as “Ubuntu Server 22.04 LTS (HVM), SSD Volume Type”.
Step 5: Choose “Instance type” as “t2.micro” & “Key pair (login)” as “Demo”.
Step 6: In the “Network Settings”, choose “Firewall (security groups)” as “Select existing security group”. While “Common security groups” as “launch-wizard-9”.
Step 7: In “Summary”, choose “Number of instances” as “2” & click on “Launch instance”.
Step 8: The instance will be successfully launched, click on “Instances”.
Step 9: Rename both the “Slave” instances as “Slave3” & “Slave4”.
B. Paste the Master Public IP Content to “Slave3” & “Slave4”
Step 1: First, click on “Connect” & the “Master” instance will be opened.
Step 2: Again, click on “Connect”.
Step 3: Run the below-given command to update the machine:
sudo apt-get update
Step 4: Run this command to land inside the .ssh directory:
cd .ssh/
Run the below-given command to check the files & directory present here:
ls
Step 5: Run the below-given command to view the content of “id_rsa.pub”.
sudo cat id_rsa.pub
Copy this content from here.
Step 6: Go to “Slave3” & click on “Connect”.
Step 7: Again, click on “Connect”.
Step 8: Run the below-given command here to update the machine:
sudo apt-get update
Step 9:Run this command to land inside the .ssh directory:
cd .ssh/
Run the below-given command to check the files & directory present here:
ls
Step 10: Run the below-given command to open the “authorized_keys” file:
sudo nano authorized_keys
Step 11: Paste the content here. Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Step 12: Go to the “Slave4” & click on “Connect”.
Step 13: Again, click on “Connect”.
Step 14: Update the machine using the below-given command:
sudo apt-get update
Step 15: Go to the “.ssh” directory using the below-given command:
cd .ssh/
To view whether the authorized_keys are present here or not, run the below-given command:
ls
Step 16: Run the below-given command to edit the “authorized_keys”.
sudo nano authorized_keys
Step 17: Paste the content in the “authorized_keys” file.
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Problem (2) Solution: Label 2 nodes as test and the other 2 as prod
Step 1: Open the hosts file using the below-given command:
sudo nano /etc/ansible/hosts
Step 2: Put “Slave1” & “Slave2” in the “test” group. While putting “Slave3” & “Slave4” in the “prod” group.
[test]
Slave1 ansible_host=172.31.41.48
Slave2 ansible_host=172.31.43.238
[prod]
Slave3 ansible_host=172.31.43.185
Slave4 ansible_host=172.31.41.169
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Problem (3) & (4) Integrated Solution:
Install Java on test nodes
Install MySQL server on prod nodes
Follow these steps:
A. Create Two Roles “test” & “prod”
Step 1: Go to the “Roles” using the below-given command:
cd /etc/ansible/roles
To view the roles, use the below-given command:
ls
Step 2: Create two roles here named “test” & “prod” using the below-given commands:
sudo ansible-galaxy init test
sudo ansible-galaxy init prod
Both the “test” & “prod” roles will be successfully created.
Run the below-given command to check all the roles:
ls
B. Write the “install.yaml” file for “test” and “prod” & Include it in “main.yml”
Step 1: First, go to the “tasks” in “Test”. First, change the directory using the below-given command:
cd test
Run the below-given command to check all the directories here:
ls
The “tasks” directory will be shown here.
Step 2: Run the below-given command to change the directory to tasks:
cd tasks
Run the below given command:
ls
The “main.yml” file will be shown here.
Run this command to create an “install.yaml” file to install the “Java”.
sudo nano install.yaml
Step 3: Paste these commands here:
- name: installing java
apt: name=openjdk-18-jdk state=latest
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Step 4: Now, include this “install.yaml” file in the “main.yml” file. Open the “main.yml” file using the command:
sudo nano main.yml
Step 5: Paste the below-given command here:
- include_tasks: install.yaml
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Step 6: Run the below-given command to go to “tasks” in the “prod” role:
cd prod/tasks
Run the below-given command here:
ls
Only “main.yml” will be present here.
Step 7: Run the below-given command to create the “install.yaml” file to install “MySQL”.
sudo nano install.yaml
Step 8: Paste these below-given commands here:
- name: installing mysql
apt: name=mysql-server state=latest
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Step 9: Now, include this “install.yaml” file in the “main.yml” file. Open the “main.yml” file using the below-given command:
sudo nano main.yml
Step 10: Paste the below-given command here:
- include_tasks: install.yaml
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
C. Create a “play5.yaml” file & Run This File to Execute the Installation on Nodes
Step 1: Go to the /etc/ansible directory using the below-given command:
cd /etc/ansible
Step 2: Create a “play5.yaml” using the below-given command:
sudo nano play5.yaml
Step 3: Paste this code into the “play5.yaml” file.
---
- name: installing java on test group
hosts: test
become: true
roles:
- test
- name: installing mysql on prod group
hosts: prod
become: true
roles:
- prod
Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.
Step 4: Check the syntax using the below-given command:
ansible-playbook play5.yaml --syntax-check
Step 5: Run the below-given command to do the dry run of the given play5.yaml code:
ansible-playbook play5.yaml --check
Step 6: Type “Yes” to execute the commands on “Slave3” & “Slave4”.
Step 7: All the Slaves statuses are “OK”.
Step 8: Run the below-given command to execute the “play5.yaml” file:
ansible-playbook play5.yaml
Step 9: The play will be successfully run. All “Slaves” statuses are “OK”.
Step 10: On “Slave1” & “Slave2”, “Java” will be successfully installed.
Type the below-given command to check the Java installation on both “Slave1” & “Slave2”.
java --version
Step 11: On “Slave3” & “Slave4”, “MySQL” will be successfully installed.
Type the below-given command to check the Java installation on both “Slave3” & “Slave4”.
mysql --version
Ansible Case Study Solution: