Last Updated on 2 weeks by Sachin G

Installing Java is a fundamental step for many Java developers, whether they’re deploying applications on a web server, running background Java programs, or setting up Cloud Servers. Using an Ansible Playbook, administrators can automate the installation of Java across various Linux distributions like CentOS, Red Hat, AlmaLinux, Rocky, Debian, and Ubuntu systems.

This guide covers the easiest way to install both Java Runtime Environment (JRE) and Java Development Kit (JDK) using Ansible.

Why Use Ansible for Java Installation?

Manually installing Java on each server can lead to configuration inconsistencies. Ansible solves this by enabling real-time conversations between nodes in a single location using repeatable, version-controlled tasks. This ensures that the installed Java version is consistent across all systems.

Prerequisites

You should have an Ansible setup. In our environment, the control node user can automatically log in to the managed host through the user, and the user has sudo privileges without a password. You can verify through an Ansible ad-hoc command environment setup.

ansible all -m command -a ‘id’

Here I have two machines, one is Debian based and one is RedHat OS Family which is almalinux.

Here I am explaining two methods.

1. First Method : Using Ansible Galaxy Role

We can use Ansible Roles, Ansible Galaxy is a public repository, which contains thousands of roles.

The ansible-galaxy command can manage roles from the galaxy website. The ansible-galaxy command-line utility to search, initialize, and installed roles.

The command ansible-galaxy search –author geerlingguy will display all roles submitted by the user geerlingguy. Here I am going to use this author’s role to install Java on different Linux operating systems.

ansible-galaxy search 'Java'  --author geerlingguy

The ansible-galaxy info subcommand shows more information related to the role.

# ansible-galaxy info geerlingguy.java

Now we are downloading a role from the Ansible Galaxy install command. That will install the local project folder or you can specify the path through the -p option. roles folder should exist on the project or current path.

 # ansible-galaxy install geerlingguy.java -p roles/

After downloading, the roles, your ansible.cfg should configure for roles configuration list like below. In ansible. cfg the roles_path should configure, where the roles have been downloaded.

Ansible Playbook to Install Java for CentOS, RedHat, AlmaLinux, Rocky, Debian, Ubuntu Linux

Create a playbook file in YML format import the downloaded role and define the version-specific variable, that you want to install.

---
- hosts: all
  roles:
          - role: geerlingguy.java
            when: "ansible_os_family == 'RedHat'"
            java_packages:
                    - java-17-openjdk
- hosts: all
  roles:
          - role: geerlingguy.java
            when: "ansible_os_family == 'Debian'"
            java_packages:
                    - default-jre
...

Verify OpenJDK Installation on host machines.

If you have any views, let me know through comments.