Impact-Site-Verification: d8d8859d-7548-47cd-9205-db15dd001e65

Last Updated on 10 months by Sachin G

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 install java on multiple managed host machines. Use different Linux flavors RPM-based distribution or Debian-based system. Here I am going to try the RPM-based Linux distribution CentOS 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’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.

Creating Playbook to install OpenJDK on Managed Host

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

For CentOS/RedHat/AlmaLinux/Rocky Debian/Ubuntu Linux

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