Java installation through automation - using Ansible

Automation of Java installation – using Ansible

Last Updated on February 18, 2023 by cscontents


Automation is the key aspect in DevOps methodology. It helps in faster software delivery. Developing an automated solution always takes more time at the initial stage, but we all know how it benefits us in the future.

In this article we will see automation of java installation using ansible.


  • Hands-on knowledge of Ansible
  • Awareness about the steps to install Java manually.

If you want to learn Ansible basics then please check out the below course from KodeKloud which offers one of the best learning material in DevOps world.

Link of Training Course: Ansible for absolute beginners

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Here, at first we will see the manual steps to install Java or JDK, and then we will see the automated solution. This is just for simplification, when you will see manual steps and automated solution (using ansible) side by side, then it will be very clear to you.

  • Manual installation of Java (JDK)
  • Java installation through automation.

Before starting, I just want to point out the various way we can install java –

  • Using package manager (example, yum for RHEL)
  • Archive installation – here we download the JDK binaries and then perform the installation.

In this article we will discuss archive installation and see how we can automate the installation.

Manual installation of Java (JDK)

Below is step-by-step guide to install JDK manually.

Step – 1: Download the JDK binaries

Download the JDK binaries.

[root@RHEL01 ~]# cd /opt/
[root@RHEL01 opt]# curl --output openjdk-
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 173M 100 173M 0 0 219M 0 --:--:-- --:--:-- --:--:-- 218M

Step – 2: Unzip the downloaded file

Here we will use the ‘tar’ command to unzip the downloaded file (since it is a .tar.gz file).

[root@RHEL01 opt]# tar -xvf openjdk-

After unzipping we will see a new folder with name jdk-

[root@RHEL01 opt]# ll
total 178028
drwx--x--x. 4 root root 28 Aug 8 14:05 containerd
drwxr-xr-x. 2 dockeradmin dockeradmin 42 Aug 8 18:40 docker
drwxr-xr-x. 9 root root 136 Sep 4 12:23 jdk-
-rw-r--r--. 1 root root 182299048 Sep 4 06:51 openjdk-

Step – 3: Set the JAVA_HOME in /etc/profile file

In my case the path is /opt/jdk-

Below are the two line we need to add in the /etc/profile file.

  • export JAVA_HOME=”/opt/jdk-″
  • export PATH=$PATH:$JAVA_HOME:/bin
[root@RHEL01 opt]# vi /etc/profile

Step – 4: Reload /etc/profile file

[root@RHEL01 opt]# source /etc/profile

After reloading the /etc/profile file, we can check the JAVA_HOME environment variable by running the below echo command.

[root@RHEL01 opt]# echo $JAVA_HOME

Now, Java installation is completed. For confirmation, we can check the java version by running the below command.

[root@RHEL01 opt]# java -version
java version "" 2022-08-18
Java(TM) SE Runtime Environment (build
Java HotSpot(TM) 64-Bit Server VM (build, mixed mode, sharing)

Automated way of Java installation

Now we will see how we can automate the steps in manual installation using Ansible.

As a prerequisite you must have ansible-controller machine and remote/target machine where you want to perform the installation. For this you can follow this article Basics of automation using Ansible | Automate any task


In step – 1 we have downloaded the JDK binary file, for this we will use get_url module of Ansible.

In step  – 2 we have unzipped the downloaded tar file, for this we will use unarchive module of Ansible.

In step – 3 we have updated the /etc/profile file with “JAVA_HOME”, for this we will use the lineinfile module of Ansible.

In step – 4 we have reloaded the /etc/profile file, for this we will use shell module of Ansible.

Ansible Playbook for installation of Java

Below is the ansible playbook for automated installation of java.

- name: Java installation
  hosts: remote_server
  become: true
    - name: Download the JDK binaries
        dest: /opt/openjdk-

    - name: Unzip the downloaded file
        src: /opt/openjdk-
        dest: /opt
        remote_src: yes

    - name: Set the JAVA_HOME in /etc/profile file
        path: /etc/profile
        state: present
        line: "{{ item }}"
        - 'export JAVA_HOME="/opt/jdk-"'
        - 'export PATH=$PATH:$JAVA_HOME/bin'

    - name: Reload /etc/profile file
        cmd: source /etc/profile

Inventory file

To run the above playbook we need inventory file, where we will have the details about the ‘remote_server’ which is mentioned under the “hosts” field of the above playbook.

Here, inventory.txt file looks like below.

remote_server ansible_host= ansible_ssh_user=remote_ssh_user ansible_ssh_pass=remote_ssh_password

Command to execute the above ansible playbook

To run the above playbook we need to run the below command on the ansible-controller machine.

ansible-playbook java_installation_playbook.yml -i inventory.txt --extra-vars "ansible_sudo_pass=Password" -vv


ansible-playbook java_installation_playbook.yml -i inventory.txt --ask-become-pass -vv

The above command will ask for password, and we will need to enter the password which is required to become root user in the target machine.


Thank You.


If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.