Ansible Role: Java
Role to install the Java JDK.
Important: since the 8.0.0 release of this role the Oracle JDK is no longer supported, AdoptOpenJDK is used for all Java versions. Due to this, support for Java 7 has been discontinued.
Requirements
-
Ansible >= 2.6
-
Linux Distribution
-
Debian Family
-
Debian
- Jessie (8)
- Stretch (9)
-
Ubuntu
- Trusty (14.04)
- Xenial (16.04)
- Bionic (18.04)
-
-
RedHat Family
-
CentOS
- 6
- 7
-
Fedora
- 28
-
-
SUSE Family
-
openSUSE
- 15.0
-
-
Note: other versions are likely to work but have not been tested.
-
Role Variables
The following variables will change the behavior of this role (default values are shown below):
# Java version number
# Specify '8', '9', '10', '11' or '12' to get the latest patch version of that
# release.
java_version: 'jdk-11.0.3+7'
# Base installation directory for any Java distribution
java_install_dir: '/opt/java'
# Directory to store files downloaded for Java installation on the remote box
java_download_dir: "{{ x_ansible_download_dir | default(ansible_env.HOME + '/.ansible/tmp/downloads') }}"
# Location Java installations packages can be found on the local box
# local packages will be uses in preference to downloading new packages.
java_local_archive_dir: '{{ playbook_dir }}/files'
# Wether to use installation packages in the local archive (if available)
java_use_local_archive: yes
# If this is the default installation, profile scripts will be written to set
# the JAVA_HOME environment variable and add the bin directory to the PATH
# environment variable.
java_is_default_installation: yes
# Name of the group of Ansible facts relating this Java installation.
#
# Override if you want use this role more than once to install multiple versions
# of Java.
#
# e.g. java_fact_group_name: java_8
# would change the Java home fact to:
# ansible_local.java_8.general.home
java_fact_group_name: java
# The SHA-256 for the JDK redistributable
java_redis_sha256sum:
# Mirror location for JDK download (e.g. https://example.com/provisioning/files)
java_redis_mirror:
# File name for the JDK redistributable installation file
java_redis_filename:
# OpenJDK Implementation (hotspot, openj9)
java_implementation: hotspot
# Timeout for JDK download response in seconds
java_download_timeout_seconds: 600
# The timeout for the AdoptOpenJDK API
java_api_timeout_seconds: 30
Example Playbooks
By default this role will install the latest LTS JDK version provided by AdoptOpenJDK that has been tested and is known to work with this role:
- hosts: servers
roles:
- role: gantsign.java
You can install a specific version of the JDK by specifying the java_version
.
- hosts: servers
roles:
- role: gantsign.java
java_version: 'jdk8u212-b03'
Note: with curl and jq you can view the available versions by running the following command:
for ((i = 8; i <= 12; i++)) do (curl --silent http \
"https://api.adoptopenjdk.net/v2/info/releases/openjdk$i?openjdk_impl=hotspot" \
| jq --raw-output '.[].release_name'); done
You can install the multiple versions of the JDK by using this role more than once:
- hosts: servers
roles:
- role: ansible-role-java
java_version: '8'
java_is_default_installation: no
java_fact_group_name: java_8
- role: ansible-role-java
java_version: '11'
java_is_default_installation: yes
java_fact_group_name: java
To perform an offline install, you need to specify a bit more information (i.e.
java_redis_filename
and java_redis_sha256sum
). E.g. to perform an offline
install of jdk-11.0.3+7
:
# Before performing the offline install, download
# `OpenJDK11U-jdk_x64_linux_hotspot_11.0.3_7.tar.gz` to
# `{{ playbook_dir }}/files/` on the local machine.
- hosts: servers
roles:
- role: gantsign.java
java_version: 'jdk-11.0.3+7'
java_redis_filename: 'OpenJDK11U-jdk_x64_linux_hotspot_11.0.3_7.tar.gz'
java_redis_sha256sum: '23cded2b43261016f0f246c85c8948d4a9b7f2d44988f75dad69723a7a526094'
Role Facts
This role exports the following Ansible facts for use by other roles:
-
ansible_local.java.general.version
- e.g.
8u212
- e.g.
-
ansible_local.java.general.home
- e.g.
/opt/java/jdk8u212
- e.g.
Overriding java_fact_group_name
will change the names of the facts e.g.:
java_fact_group_name: java_8
Would change the name of the facts to:
ansible_local.java_8.general.version
ansible_local.java_8.general.home
More Roles From GantSign
You can find more roles from GantSign on Ansible Galaxy.
Development & Testing
This project uses Molecule to aid in the development and testing; the role is unit tested using Testinfra and pytest.
To develop or test you'll need to have installed the following:
Because the above can be tricky to install, this project includes Molecule Wrapper. Molecule Wrapper is a shell script that installs Molecule and it's dependencies (apart from Linux) and then executes Molecule with the command you pass it.
To test this role using Molecule Wrapper run the following command from the project root:
./moleculew test
Note: some of the dependencies need sudo
permission to install.
License
MIT
Author Information
John Freeman
GantSign Ltd. Company No. 06109112 (registered in England)