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