Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1

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

Tasks to Be Performed:

1. Setup Ansible cluster with 3 nodes

2. On slave 1 install Java

3. On slave 2 install MySQL server Do the above tasks using Ansible Playbooks

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

Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1
Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1

Problem (1) Solution: Setup Ansible cluster with 3 nodes

Follow these steps to set up the Ansible Cluster with 3 Nodes

A. Create Three EC2 Instances Named as “Master”, “Slave1”, & “Slave2”

Step 1: Go to the “Services” section & search “EC2” here. Put Cursor over “EC2” & click on “Instances”.

Search “EC2” service here
Search “EC2” service here

Step 2: Click on “Launch instances”.

Launch an Instance
Launch an Instance

Step 3: Put “Name” as “Ansible-“ in the “Name and tags” section.

Choose the Instance Name
Choose the Instance Name

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

Choose the “Ubuntu” AMI
Choose the “Ubuntu” AMI

Step 5: Choose “Instance type” as “t2.micro”.

Choose the instance type here
Choose the instance type here

Step 6: Choose “key pair (login)” as “Demo”.

Choose the key pair name
Choose the key pair name

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

Select the existing security group
Select the existing security group

Step 8: In the “Summary” section, choose “Number of Instances” as “3” & click on “Launch instances”.

Launch the Instance
Launch the Instance

Step 9: The instances will be successfully launched. Click on “Instances” in “Breadcrumb”.

Instances Successfully Launched
Instances Successfully Launched

Step 10: You will notice that all “3” instances are in the “Running” state.

Instance in the “Running” State
Instance in the “Running” State

Step 11: Rename all three instances as “Ansible-Master”, “Ansible-Slave1”, & “Ansible-Slave2”.

Instance Renamed
Instance Renamed

B. Install “Ansible” on “Ansible-Master”

Step 1: Choose the “Ansible-Master” & click on “Connect”.

Select the “Ansible-Master” Instance
Select the “Ansible-Master” Instance

Step 2: Again, click on “Connect”.

Connect the “Ansible-Master” Instance
Connect the “Ansible-Master” Instance

Step 3: Create an “ansible-install.sh” file using the below-given command:

sudo nano ansible-install.sh
Create an install.sh file
Create an install.sh file

Step 4: Paste the below-given commands into the “ansible-install.sh” file.

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Paste the Ansible Installation Commands
Paste the Ansible Installation Commands

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

Step 5: Run the below-given command to install the ansible:

bash ansible-install.sh

Press “enter” from the keyboard.

Run the install.sh script using the bash command
Run the install.sh script using the bash command

Step 6: It will ask you to continue, Press “Y” from the keyboard to execute the installation.

It will ask two times “Y” or “N” here. Put “Y” every time here.

Continue through “Yes”
Continue through “Yes”

Step 7: Now, check that “Ansible” is successfully installed or not using the below-given command:

ansible --version

The “Ansible” tool is successfully installed.

Ansible Version
Ansible Version

Step 8: To check if “Python” is successfully installed or not use the below-given command:

python3 --version
Python Version
Python Version

The “Python3” has been installed automatically with “Ubuntu” machines.

C. Setup a Connection Between “Ansible-Master” & “Ansible Slaves”

Step 1: Go to the “.ssh” directory using the below-given command:

cd .ssh/
Go to the .ssh directory
Go to the .ssh directory

Step 2: Create the public & private keys using the below-given command:

ssh-keygen

Press “Enter” from the keyboard three times.

The Public & Private keys will be successfully installed.

Public & Private Key Generated
Public & Private Key Generated

Step 3: Run the below-given command to copy the public key content:

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

Copy the content from here.

Step 4: Go to the “Instances” in “AWS”. Select the “Ansible-Slave1” machine & click on “Connect”.

Select the First Slave Machine
Select the First Slave Machine

Step 5: Again, click on “Connect”.

Connect the First Slave Machine
Connect the First Slave Machine

Step 6: First, update the machine using the below-given command:

sudo apt update
Update the “Slave1” Machine
Update the “Slave1” Machine

Step 7: The instance will be successfully updated.

Slave Machine Updated
Slave Machine Updated

Step 8: Go to the .ssh directory using the below-given command:

cd .ssh/

And, run the below-given command to check the files present here:

ls
authorized_keys
authorized_keys

The authorized_keys file will be shown here.

Step 9: Open the authorized_keys file using the below-given command:

sudo nano authorized_keys
Open the authorized_keys file
Open the authorized_keys file

Step 10: Paste the public key content here.

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

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

Step 11: Go to the “Instances” in “AWS”. Select the “Ansible-Slave2” machine & click on “Connect”.

Select the “Ansible-Slave2” Here
Select the “Ansible-Slave2” Here

Step 12: Again, click on “Connect”.

Connect the “Ansible-Slave2” Machine
Connect the “Ansible-Slave2” Machine

Step 13: First, update the machine using the below-given command:

sudo apt update
Update the “Ansible-Slave2” Machine
Update the “Ansible-Slave2” Machine

Step 14: The instance will be successfully updated.

Machine Updated Successfully
Machine Updated Successfully

Step 15: Go to the .ssh directory using the below-given command:

cd .ssh/

And, run the below-given command to check the files present here:

ls
Go to the .ssh directory
Go to the .ssh directory

The authorized_keys file will be shown here.

Step 16: Open the authorized_keys file using the below-given command:

sudo nano authorized_keys
Open the authorized_keys file
Open the authorized_keys file

Step 17: Paste the public key content here.

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

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

The Public Key has been successfully installed on both slaves “Slave1” & “Slave2”.

D. Paste Both Slave IP Addresses in the Inventory File.

Step 1: Go to the “Ansible-Master” machine & run the below-given command to go inside the “ansible” directory.

cd /etc/ansible/
Go to the “ansible” directory
Go to the “ansible” directory

Step 2: Open the hosts file using the below-given command:

sudo nano hosts
Open the “hosts” file
Open the “hosts” file

Step 3: Paste the “Slaves IP” in the beginning with the Instance Name.

Slave1 ansible_host=172.31.33.191
Slave2 ansible_host=172.31.33.94
Paste the “Ansible Hosts” IP Addresses Here
Paste the “Ansible Hosts” IP Addresses Here

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

Step 4: Now, we will ping the slave machines using these below-given commands from the “Master”.

ansible –m ping all

or we can write like this:

ansible –m ping Slave1
ansible –m ping Slave2
Ping both slaves machines
Ping both slaves machines

Both “Slaves” will be successfully connected. For the first time, it will ask you to take the fingerprint. Type “Yes” & “Slaves” will be successfully connected.

Problem (2) & (3) Integrated Solution:

A. On slave 1 install Java

B. On slave 2 install MySQL server Do the above tasks using Ansible Playbooks

Follow these steps to install Java and MySQL using Ansible Playbooks:

1. Create a Playbook to Install Java & MySQL on “Slave1” & “Slave2”

Step 1: Create a play1.yaml file using the below-given command:

sudo nano play1.yaml
Create the play1.yaml file
Create the play1.yaml file

Step 2: Now, we will write the YAML file syntax to install “Java” & “MySQL” on “Slave1” & “Slave2”.

---
- name: installing java
hosts: Slave1
become: true
tasks:
- name: installing java on slave1
apt: name=openjdk-18-jdk update-cache=yes state=latest
- name: installing mysql
hosts: Slave2
become: true
tasks:
- name: installing mysql on slave2
apt: name=mysql-server update-cache=yes state=latest
Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1
Paste the Script Here

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

Step 3: Now, we will do the syntax check using the below-given command:

ansible-playbook play1.yaml --syntax-check
Syntax Check
Syntax Check

Step 4: First, dry run the play1.yaml file using the below-given command:

ansible-playbook play1.yaml --check
Dry Run the Command
Dry Run the Command
Command Run Successfully
Command Run Successfully

Step 5: Now, run the playbook using the below-given command:

ansible-playbook play1.yaml
Run the Ansible Playbook
Run the Ansible Playbook
Playbook Run Successfully
Playbook Run Successfully

So, the Ansible playbook has been successfully run. Now, we have to check whether “Java” is installed on “Slave1” & “MySQL” is installed on “Slave2” or not.

2. Check “Java” Or “MySQL” Successfully Installed on “Slave1” & “Slave2” Respectively or Not

Step 1: Go to the “Slave1” & type the below-given command:

java --version
Check the “Java” Version
Check the “Java” Version

You will notice that openjdk18 has been successfully installed on “Slave1”.

Step 2: Go to the “Slave2” machine & type the below-given command:

mysql --version
Check the “MySQL” Version
Check the “MySQL” Version

You will notice that “MySQL” has been successfully installed on “Slave2”.

Check these Ansible Assignments Here:

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

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

--

--