Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5

Visal Tyagi
DevOps-Guides
Published in
9 min readJun 16, 2024

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.

Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5
Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5

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.

Master & Slaves Nodes
Master & Slaves Nodes

Step 2: Click on “Launch instances”.

Launch the Instances
Launch the Instances

Step 3: Write “Name” as “Slave” in “Name and tags”.

Write Instance Name
Write Instance Name

Step 4: Choose “AMI” as “Ubuntu” & its version as “Ubuntu Server 22.04 LTS (HVM), SSD Volume Type”.

Ubuntu AMI
Ubuntu AMI

Step 5: Choose “Instance type” as “t2.micro” & “Key pair (login)” as “Demo”.

Choose the Instance type & Key Pair
Choose the Instance type & Key Pair

Step 6: In the “Network Settings”, choose “Firewall (security groups)” as “Select existing security group”. While “Common security groups” as “launch-wizard-9”.

Select the Security Group
Select the Security Group

Step 7: In “Summary”, choose “Number of instances” as “2” & click on “Launch instance”.

Launch the Instance
Launch the Instance

Step 8: The instance will be successfully launched, click on “Instances”.

Instance Successfully Launched
Instance Successfully Launched

Step 9: Rename both the “Slave” instances as “Slave3” & “Slave4”.

Rename Slave Nodes
Rename Slave Nodes
Slave Nodes Renamed
Slave Nodes Renamed

B. Paste the Master Public IP Content to “Slave3” & “Slave4”

Step 1: First, click on “Connect” & the “Master” instance will be opened.

Select the Master Machine
Select the Master Machine

Step 2: Again, click on “Connect”.

Connect to the Master Instance
Connect to the Master Instance

Step 3: Run the below-given command to update the machine:

sudo apt-get update
Update the Master Machine
Update the Master Machine

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  
Go to the .ssh directory
Go to the .ssh directory

Step 5: Run the below-given command to view the content of “id_rsa.pub”.

sudo cat id_rsa.pub
Copy the public key content here
Copy the public key content here

Copy this content from here.

Step 6: Go to “Slave3” & click on “Connect”.

Select the Slave3 Machine
Select the Slave3 Machine

Step 7: Again, click on “Connect”.

Connect to the Slave3 Machine
Connect to the Slave3 Machine

Step 8: Run the below-given command here to update the machine:

sudo apt-get update
Update the Slave3 Machine
Update the Slave3 Machine

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
Go to the .ssh directory
Go to the .ssh directory

Step 10: Run the below-given command to open the “authorized_keys” file:

sudo nano authorized_keys
Open the authorized_keys file
Open the authorized_keys file

Step 11: Paste the content here. Do “CTRL+X” to exit & type “Yes” to save the file. Press “enter” from the keyboard.

Paste the Public Key Content Here
Paste the Public Key Content Here

Step 12: Go to the “Slave4” & click on “Connect”.

Select the Slave4 Machine
Select the Slave4 Machine

Step 13: Again, click on “Connect”.

Connect to the “Slave4” Machine
Connect to the “Slave4” Machine

Step 14: Update the machine using the below-given command:

sudo apt-get update
Update the Slave4 Machine
Update the Slave4 Machine

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
Go to the .ssh directory
Go to the .ssh directory

Step 16: Run the below-given command to edit the “authorized_keys”.

sudo nano authorized_keys
Edit the authorized_keys file
Edit the authorized_keys file

Step 17: Paste the content in the “authorized_keys” file.

Paste the Content Here
Paste the Content Here

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
Open the hosts file
Open the hosts file

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
Paste the Slave Groups Here
Paste the Slave Groups Here

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  
Go to the “roles” directory
Go to the “roles” directory

Step 2: Create two roles here named “test” & “prod” using the below-given commands:

sudo ansible-galaxy init test
sudo ansible-galaxy init prod
Create the Roles
Create the Roles

Both the “test” & “prod” roles will be successfully created.

Run the below-given command to check all the roles:

ls  
Check the Created Roles
Check the Created Roles

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
Go to the test role
Go to the test role

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
Create an install.yaml file
Create an install.yaml file

Step 3: Paste these commands here:

- name: installing java
apt: name=openjdk-18-jdk state=latest
Install the Java
Install the Java

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
Open the main.yml file
Open the main.yml file

Step 5: Paste the below-given command here:

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

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
Go to the tasks
Go to the tasks

Run the below-given command here:

ls
main.yml file
main.yml file

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
Create the install.yaml file
Create the install.yaml file

Step 8: Paste these below-given commands here:

- name: installing mysql
apt: name=mysql-server state=latest
Install MySQL
Install MySQL

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
Open the main.yml file
Open the main.yml file

Step 10: Paste the below-given command here:

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

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
Go to the ansible directory
Go to the ansible directory

Step 2: Create a “play5.yaml” using the below-given command:

sudo nano play5.yaml
Create the play5.yaml file
Create the play5.yaml file

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
Install Java and MySQL on Node Groups
Install Java and MySQL on Node Groups

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
Check the Syntax
Check the Syntax

Step 5: Run the below-given command to do the dry run of the given play5.yaml code:

ansible-playbook play5.yaml --check
Dry Run the play5.yaml file
Dry Run the play5.yaml file

Step 6: Type “Yes” to execute the commands on “Slave3” & “Slave4”.

Step 7: All the Slaves statuses are “OK”.

Type “Yes”
Type “Yes”

Step 8: Run the below-given command to execute the “play5.yaml” file:

ansible-playbook play5.yaml
Execute the play5.yaml file
Execute the play5.yaml file

Step 9: The play will be successfully run. All “Slaves” statuses are “OK”.

Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5
Status “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
Java Version on “Slave1”
Java Version on “Slave1”
Java Version on “Slave2”
Java Version on “Slave2”

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 
MySQL Version on “Slave3”
MySQL Version on “Slave3”
MySQL Version on “Slave4”
MySQL Version on “Slave4”

Ansible Case Study Solution:

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

--

--