Setup Ansible Cluster & Install Java & MySQL using Playbooks — Ansible Assignment 1
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:
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”.
Step 2: Click on “Launch instances”.
Step 3: Put “Name” as “Ansible-“ in the “Name and tags” section.
Step 4: Choose “AMI” as “Ubuntu” & “AMI Type” as “Ubuntu Server 22.04 LTS (HVM), SSD Volume Type”.
Step 5: Choose “Instance type” as “t2.micro”.
Step 6: Choose “key pair (login)” as “Demo”.
Step 7: In the “Network Settings”, choose “Firewall (security groups)” as “Select existing group”. While choosing “Common security groups” as “launch-wizard-9”.
Step 8: In the “Summary” section, choose “Number of Instances” as “3” & click on “Launch instances”.
Step 9: The instances will be successfully launched. Click on “Instances” in “Breadcrumb”.
Step 10: You will notice that all “3” instances are in the “Running” state.
Step 11: Rename all three instances as “Ansible-Master”, “Ansible-Slave1”, & “Ansible-Slave2”.
B. Install “Ansible” on “Ansible-Master”
Step 1: Choose the “Ansible-Master” & click on “Connect”.
Step 2: Again, click on “Connect”.
Step 3: Create an “ansible-install.sh” file using the below-given command:
sudo nano ansible-install.sh
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
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.
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.
Step 7: Now, check that “Ansible” is successfully installed or not using the below-given command:
ansible --version
The “Ansible” tool is successfully installed.
Step 8: To check if “Python” is successfully installed or not use the below-given command:
python3 --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/
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.
Step 3: Run the below-given command to copy the public key content:
sudo cat id_rsa.pub
Copy the content from here.
Step 4: Go to the “Instances” in “AWS”. Select the “Ansible-Slave1” machine & click on “Connect”.
Step 5: Again, click on “Connect”.
Step 6: First, update the machine using the below-given command:
sudo apt update
Step 7: The instance will be successfully 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
The authorized_keys file will be shown here.
Step 9: Open the authorized_keys file using the below-given command:
sudo nano authorized_keys
Step 10: 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”.
Step 12: Again, click on “Connect”.
Step 13: First, update the machine using the below-given command:
sudo apt update
Step 14: The instance will be successfully updated.
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
The authorized_keys file will be shown here.
Step 16: Open the authorized_keys file using the below-given command:
sudo nano authorized_keys
Step 17: 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/
Step 2: Open the hosts file using the below-given command:
sudo nano hosts
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
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
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
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
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
Step 4: First, dry run the play1.yaml file using the below-given command:
ansible-playbook play1.yaml --check
Step 5: Now, run the playbook using the below-given command:
ansible-playbook play1.yaml
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
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
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