有没有一种好的方法可以使用Ansible获取JAVA_HOME的实际路径?

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月21日 12:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定