英文:
Is there a good way to get JAVA_HOME real path with Ansible?
问题
使用Ansible安装Java,如下所示:
- name: 安装Java
yum:
name:
- java-1.8.0-openjdk
state: present
然后在~/.bash_profile
中设置JAVA_HOME环境变量:
- name: 更新 .bash_profile
copy:
src: bash_profile
dest: /home/user/.bash_profile
owner: user
.bash_profile
中的内容如下:
...
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre
但是经过一段时间后,当再次运行Playbook安装Java时,其版本可能会升级。因此,实际路径可能更改为其他文件夹。无法正确使用Java。
从这篇文章中,我们可以找到这种方法来获取Java的路径:
update-alternatives --config java
选择 命令
-----------------------------------------------
*+ 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)
是否有可能在Ansible Playbook中找到它呢?
英文:
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?
答案1
得分: 3
你可以使用以下任务在运行时获取JAVA_HOME。
- name: "获取JAVA_HOME"
shell: dirname $(dirname $(readlink -f $(which javac)))
register: java_home
下面的URL提供了更多查找JAVA_HOME
的方法:
例如:
java -XshowSettings:properties -version 2>&1 | grep -oP 'java.home =\s\K.*';
更多阅读:原文链接
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论