Reputation: 309
Use Ansible to install Java as
- name: Install Java
yum:
┆ name:
┆ ┆ - java-1.8.0-openjdk
┆ state: present
Then set JAVA_HOME environment variable in ~/.bash_profile
- name: Update .bash_profile
copy:
┆ src: bash_profile
┆ dest: /home/user/.bash_profile
┆ owner: user
bash_profile as
...
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre
But after a long time, when run the playbook to install Java again, its version maybe upgraded. So the real path maybe change to other folder. Can't use Java correctly.
From this article, we can find this method can get Java's path
update-alternatives --config java
Selection Command
-----------------------------------------------
*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java)</code?
Is it possible to find it from Ansible playbook?
Upvotes: 1
Views: 2841
Reputation: 18391
You may use the following task to fetch the JAVA_HOME at runtime.
- name: "Fetch JAVA_HOME"
shell: dirname $(dirname $(readlink -f $(which javac)))
register: java_home
Below URL provided more way of finding JAVA_HOME
:
Eg:
java -XshowSettings:properties -version 2>&1 |grep -oP 'java.home =\s\K.*'
More read: Original artical
Upvotes: 4