Install Java using Ansible Playbook on CentOS/RedHat/AlmaLinux/Rocky Debian/Ubuntu Linux

This post will show you the installation of Java through the automation tool Ansible. We will write a playbook to use roles or simple methods to java on multiple managed host machines. Use different flavors RPM-based distribution or Debian-based system. Here I am going to try the RPM-based Linux distribution and the Debian-based system Ubuntu. The playbook can work AlmaLinux, Rocky, Debian, and Red Hat Operating systems.

Prerequisites

You should have an ansible setup. In our environment, the control node user can automatically log in to managed host through the user and the user have 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 role to java on different 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 Ansible Galaxy 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/

[[email protected] project]$ mkdir roles
[[email protected] project]$ ansible-galaxy install geerlingguy.java -p roles/
downloading role ‘java’, owned by geerlingguy
downloading role from https://github.com/geerlingguy/ansible-role-java/archive/2.0.1.tar.gz
extracting geerlingguy.java to /home/user/ansible/roles/geerlingguy.java
geerlingguy.java (2.0.1) was installed successfully
[[email protected] project]$ ls -l roles/
total 0
drwxrwxr-x. 9 user user 195 Apr 16 12:50 geerlingguy.java

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.

Creating Playbook to install Openjdk on Managed Host

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

For /RedHat//Rocky Debian/Ubuntu

---
- 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.

About Sachin Gupta

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009. rocks!

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *