How to Deploy Java Application Using Ansible on AWS EC2 Instnace

How to Deploy Java Application Using Ansible on AWS EC2 Instnace

Using Ansible Playbook Deploy Java Application on AWS EC2 Instance

Hello Everyone, I am assuming you are aware of ansible and its terminologies. if you don't know it. Don't worry in my upcoming articles I am focussing on Ansible and Docker's concepts & terminologies. Let's get Started...

For this, I am using the Game-Of-Life project. This time I am keeping things simple that's why not using Jenkins or any CI/CD tool but the next article with Jenkins.

User Creation

Launch two AWS Instances, For Ansible I prefer to create a new user called ron

useradd ron
passwd ron #to set password

Because I am running ansible using this user I want to grant root permission to this user. Also, I want passwordless authentication.

visudo #to grant root permission and password less auth

#below the root ALL=(ALL) ALL
ron ALL=(ALL) NOPASSWD: ALL
#save the file

SSH Configuration

To do ssh key-based authentication edit the ssh configuration.

vi /etc/ssh/sshd_confg
#uncommet
permitrootlogin
#uncommet
PasswordAuthentication yes
#comment
#PasswordAuthentication no

#Save the file

Now Restart the ssh service

service sshd restart

Repeat all these steps in all your Ansible hosts as well.

Passwordless Authentication using SSH key

Now we are config. Ansible Workstation/Master

Now Let's Switch the user which we created(ron) and Generate the Key

su ron
ssh-keygen #press enter 3-4 times

Now let's connect with other users

ssh-copy-id ron@[private-ip] #of ansible hosts ec2 instance
#reprat it with all instance

It will ask you password one last time.

Ansible installation

yum install ansible -y
#if it is not isntall automatically Amezon linux give you the hint just copy nd past that command of amezon-linux-extra

Let's configure the Ansible

cd /etc/ansible/
vi ansible.cfg
#uncommet 
sudo_user root
#save it

Defining Ansible hosts

vi hosts
#mention hosts private IP of Instance
[web-app]
192.168.2.33
189.47.65.13
213.24.120.3

Compiling the App

Game of life is Java Based project that has the following dependencies

yum install java-1.8.0-openjdk.x86_64 -y
yum install git -y
yum install maven -y

cd /mnt
#cloning the project from git 
git clone https://github.com/rohaanuv/game-of-life.git
cd game-of-life/

#compiling, installing depencies, packing the project to the war
mvn clean install

Creating Ansible Playbook

Let's write Ansible Playbook to deploy the application

vi game-of-life-ansb-conf.yml
--- #my playbook
- hosts: dev
  user: ron
  become: yes
  gather_facts: yes
  connection: ssh
  vars:
    tomcat_url: https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.72/bin/apache-tomcat-9.0.72.zip
    src_path: /mnt/game-of-life/gameoflife-web/target/gameoflife.war
    des_path: /mnt/apache-tomcat-9.0.72/webapps/
    pkgname: java-1.8.0-openjdk.x86_64
    temp_cmd: unzip /mnt/apache-tomcat-9.0.72.zip
    destiny: /mnt/

  tasks:
    - name: install java
      action: yum pkg="{{pkgname}}" state=present

    - name: Download tomcat
      action: get_url url="{{tomcat_url}}" dest="{{destiny}}"

    - name: unzip tomact
      ansible.builtin.shell:
        chdir: "{{destiny}}"
        cmd: unzip apache-tomcat-9.0.72.zip

    - name: deploy war file
      action: copy src="{{src_path}}" dest="{{des_path}}"

    - name: deleting zip file
      ansible.builtin.shell:
        chdir: "{{destiny}}"
        cmd: "rm -rf *.zip"

    - name: exc permission
      ansible.builtin.shell:
        chdir: "{{destiny}}"
        cmd: "sudo chmod -R 777 /mnt/"
      notify: start service

  handlers:
    - name: start service
      ansible.builtin.shell:
        chdir: /mnt/apache-tomcat-9.0.72/bin/
        cmd: "sudo ./startup.sh"

To run Ansible Playbook

ansible-playbook game-of-life-ansb-conf.yml

Did you find this article valuable?

Support Rohaan Joshi by becoming a sponsor. Any amount is appreciated!