使用Ansible检查是否已安装Java,并在RHEL机器上如未安装则进行安装。

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

Ansible to check for Java and install if not already on RHEL machines

问题

- name: 获取Java版本
  shell: java -version 2>&1 | grep version | awk  '{print $3}' | sed 's/" //g'
  changed_when: False
  register: java_result
  failed_when: False

- name: 打印Java版本
  debug:
    msg: " {{ java_result.stdout }} "
  when: java_result.rc==0

- name: 安装Java版本
  yum:
    name: java-1.8.0-openjdk.x86_64
    state: present
  when: java_result.rc!=0

最终生效的结果:

- name: 获取Java版本
  shell: java -version
  changed_when: False
  register: java_result
  failed_when: False

- name: 安装Java版本
  yum:
    name: java
    state: latest       
  when: java_result.rc!=0
  become: yes
  become_user: root

谢谢。

英文:

Trying to create a job that will check for java and install if it comes back as not installed. All works with the exception of the install part. Ansible is telling me a conditional has not been met so it skips the install.

- name: fetch java version
  shell: java -version 2>&1 | grep version | awk  '{print $3}' | sed 's/" //g'
  changed_when: False
  register: java_result
  failed_when: False

- name: print java version
  debug:
    msg: " {{ java_result.stdout }} "
  when: java_result.rc==0

- name: install java version
  yum: 
    name: java-1.8.0-openjdk.x86_64
    present: yes
  when: java_result.rc!=0

The end result that works:

- name: fetch java version
  shell: java -version
  changed_when: False
  register: java_result
  failed_when: False

- name: install java version
  yum:
    name: java
    state: latest       
  when: java_result.rc!=0
  become: yes
  become_user: root

thanks.

答案1

得分: 2

问题在于您的 shell 命令是一个管道,管道的结果值(默认情况下)是管道中最后一个命令的结果。在这种情况下,最后一个命令是 sed,它将正常关闭,因此返回的 rc 值是 0

有多种方法可以解决这个问题;我首先想到的方法是将您的 shell 命令更改为仅运行 java -version。然后您可以检查该命令的 rc 值:如果它是非零的,那么安装 Java,如果它是零,那么您可以使用一些复杂的正则表达式来提取版本字符串并打印它。

或者,您可以直接使用 stat 模块检查 Java 可执行文件的存在性。

或者,您可以在不先检查 java 的情况下运行 yum 块 - 如果该软件包已经安装,它不会执行任何操作。这可能是最符合 Ansible 风格的方式。

英文:

The problem is that your shell command is a pipeline, and the result value of a pipeline is (by default) the result of the last command in the pipeline. In this case it is sed, which will be closing normally, so will have a rc of 0.

There are a number of ways you could solve this problem; the first one that comes to mind is to change your shell command to only run java -version. Then you can check the rc value of that command: if it is non-zero then install java, if it is zero then you can do some fancy regex to extract the version string and print it.

Or, you could check directly for the existence of the java executable with the stat module.

Or, you could just run the yum block without checking for java first - if the package is already installed, it won't do anything. That's probably the most Ansibley way to do it.

huangapple
  • 本文由 发表于 2020年9月10日 02:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63817939.html
匿名

发表评论

匿名网友

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

确定