Home > Software design >  Java version check using ansible is getting skipped for no apparent reason
Java version check using ansible is getting skipped for no apparent reason

Time:05-05

As part of my ansible setup I'm currently checking in the VM what is the Java version and if that's not the one expected then download and install that version. I have given the standard regex to find the Java version but that step is getting skipped

- name: "[install] Check for Java install"
  shell: "java -version 2>&1 | grep version | awk '{print $3}'"
  changed_when: False
  register: java_installed
  ignore_errors: True

- when: java_installed.stdout != "17.0.2"
  block:
    - debug:
        msg: "Java is not installed"
    - name: "[install] Installing Java 17"
      become: true
      yum:
        name: /var/tmp/jdk-17_linux-x64_bin.rpm
        state: present

But these steps are getting skipped while executing

TASK [java : [install] Check for Java install] *****************************************************************************************************************************
skipping: [VM name hidden by me ]
TASK [java : debug] ********************************************************************************************************************************************************
skipping: [VM name hidden by me]
TASK [java : [install] Installing Java 17] *********************************************************************************************************************************
skipping: [VM name hidden by me]

when I execute

java -version 2>&1 | grep version | awk '{print $3}'

This is what I get

"1.8.0_312"

Does anyone know why it's getting skipped. Thanks

CodePudding user response:

Note: below is an answer to your direct problem. Meanwhile if java was installed as a system package, I strongly suggest you have a look at the answer by @Jaay to get the version directly from package facts rather than using shell/command


This is what I get

"1.8.0_312"

As you can see, the quotes are part of the output. Hence if you debug java_installed.stdout you get (ran on my local machine with java 11):

TASK [debug] ********************************************************************************************************************
ok: [localhost] => {
    "java_installed.stdout": "\"11.0.15\""
}

A simple workaround is to read the incoming value as json. The following does the job (once again customized for my local machine to test and using the version test as good practice)

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: "[install] Check for Java install"
      shell: "java -version 2>&1 | grep version | awk '{print $3}'"
      changed_when: false
      failed_when: false
      register: java_installed

    - name: show the raw and refactored captured var
      vars:
        my_msg:
          - "Raw value for version is: {{ java_installed.stdout }}"
          - "Refactored value for version is: {{ java_installed.stdout | from_json }}"
      debug:
        msg: "{{ my_msg }}"


    - when: java_installed.stdout | from_json is version("11.0.15", "==")
      debug:
        msg: "Java 11 is installed"

    - when: java_installed.stdout | from_json is not version("17.0.2", "==")
      debug:
        msg: "Java 17 is not installed"

and gives

PLAY [localhost] ****************************************************************************************************************

TASK [[install] Check for Java install] *****************************************************************************************
ok: [localhost]

TASK [show the raw and refactored captured var] *********************************************************************************
ok: [localhost] => {
    "msg": [
        "Raw value for version is: \"11.0.15\"",
        "Refactored value for version is: 11.0.15"
    ]
}

TASK [debug] ********************************************************************************************************************
ok: [localhost] => {
    "msg": "Java 11 is installed"
}

TASK [debug] ********************************************************************************************************************
ok: [localhost] => {
    "msg": "Java 17 is not installed"
}

PLAY RECAP **********************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

CodePudding user response:

Not really a big fan of shell command in Ansible, you can use the package facts core plugin to retrieve the installed Java version. This way you should get rid of the outputs problem using shell command

- name: get the rpm or apt package facts
  package_facts:
    manager: "auto"

- name: show Java version
  debug: var=ansible_facts.packages.jdk[0].version

PS: This will work only if java is installed with a package manager (not just copied in your system)

  • Related