Installing Java and MySQL DB using Ansible Playbook

Nipulpatel
5 min readFeb 19, 2024

--

Ansible is an open-source configuration management tool that automates application deployment, orchestration and cloud provisioning.

It automates the task of software installation, and infrastructure provisioning, improves security and compliance, patch systems and share automation across the entire organization.

Features of Ansible:

👉Ansible is agentless and doesn’t need any client software to manage node machines.

👉 It has simple installation steps that have one control machine and you can authenticate to all the remote machines via ssh.

👉Playbooks are written in YAML language and are easy to understand.

👉Ansible has more than 1,000 inbuilt modules that we can use in our project

👉We can customize the modules in any other programming language too.

Components In Ansible:

Playbooks

Playbook is a script which describes the automation jobs that need to be done, written in YAML (Yet Another Markup Language), often used to create configuration files.

Inventory

It has the information about the remote machine. It has a list of hosts/nodes, IP addresses, servers, databases, DNS, etc., which need to be managed and where playbook tasks will be operated. By default inventory file stored in path /etc/Ansible/hosts.

Modules

Ansible modules is a separate units of code can be reused in playbook task. It can control central resources, services, files, or packages, etc. and handle executing system commands. The modules also referred to as task plugins or library plugins.

Plugins

Plugins are pieces of code that augment Ansible’s core functionality. Ansible uses a plugin architecture to enable a rich, flexible, and expandable feature set.

Handler

It rebound the service automatically but are only run if the task contains a notify keyword.

Notify

Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it.

Playbook Structure

---
- name: Example playbook
hosts: all
vars:
my_var: "Hello, World!"
tasks:
- name: Print message
debug:
msg: "{{ my_var }}"

— — — Starts with three indent

Hosts -(provide the list of host machines means where do you want to run the playbook)

Variables -(provides the variables list)

Tasks -(have to give the list of tasks need to execute, give in the order in which you need to proceed)

Handlers -(its also same as tasks but after its task executed will notify)

Creating Simple Ansible Playbook in 3 cluster nodes installing Java on slave nodes and mysql db on slave nodes using the playbook. The steps are as follows:

1. Create control machine

2. Install Ansible in your master node

3. To make cluster, create two more slave nodes

4. Connect them via SSH

5. Add the host machine details in Inventory file

6. Write a playbook to install Java and mysql DB

7. Deployment completed using Ansible

Let’s start with the hands-on step by step process for creating 3 nodes

Step 1: Create 3 EC2 instances with Ubuntu 22.04 operating system and Ansible installed in the master node and connect via SSH .

Step 2: Install the following commands in your master machine and followed by give a “sudo apt update” in all the node machines.

$ sudo apt update
$ sudo apt-get install python-software-properties
$ sudo apt-add-repository -y ppa:ansible/ansible
$ sudo apt-get install -y ansible
$ sudo Ansible –version

Step 3: Once the above commands, check whether the installation done correctly or not, for that run –version to check the installed Ansible version.

Password ess Authentication

Now we have to create a password-less authentication connection between our machines.

In masterNode

$ ssh-keygen

cd .ssh
ls
cat id_rsa.pub

We have to copy the public key from the master Node and paste it to the Authorized_keys of our Slave Nodes.

Open Slave1 Node

cd .ssh
ls
sudo nano authorized_keys

Paste the Public key in authorized_keys

master Node

Now we have to add the privateIP address of our slave1 and slave2 nodes in the master node hosts

cd /etc/ansible
ls
sudo nano hosts

Inside hosts, I have to deleted all the commented commands by using Selection and Ctrl + K

I have added the Private IP address of the server because the Public IP changes every time.

ansible -m ping all

Installing Java and MySQL

master Node

sudo nano playbook.yaml
---
- name: installing java on Slave1
hosts: 10.0.2.240
become: true
tasks:
- name: installing java on Slave1
apt: name=openjdk-11-jdk update-cache=yes state=latest

- name: installing mysql
hosts: 10.0.2.23
become: true
tasks:
- name: installing java on Slave2
apt: name=mysql-server update-cache=yes state=latest

To check syntax error use command

ansible-playbook playbook.yaml --syntax-check

To check execution error we will use a dry run or test drive by using the command with dry run task won’t be executed in real life it will show you only the result of the command

ansible-playbook playbook.yaml --check

Now executing the final real command perform the actions which are mentioned in the playbook

ansible-playbook playbook.yaml

Final Result

Slave1 — Java installation

java --version

Slave2 — MySQL installation

mysql --version

Thank you for reading!!!!!!

--

--

No responses yet