Ansible command to check the java version in different servers Ansible command to check the java version in different servers unix unix

Ansible command to check the java version in different servers


Ansible has a version_compare filter since 1.6. But since Ansible doesn't know about your Java version you first need to fetch it in a separate task and register the output, so you can compare it.

- name: Fetch Java version  shell: java -version 2>&1 | grep version | awk '{print $3}' | sed 's/"//g'  register: java_version- assert:    that:       - java_version.stdout | version_compare('1.7', '>=') 

On a sidenote, if your main use case for Ansible is to validate the server state you might want to have a look at using an infrastructure test tool instead: serverspec, goss, inspec, testinfra.


Altough in your question you havn't specified what have you tried, but still

You can run a commands like this

ansible your_host -m command -a 'java -version'

If you need to parse the output of java -version there is a very good script from Glenn Jackman here adapt it to your needs and use it.

If you are still looking for help, be more specific and show what you tried to do.


Since 2.0 you can make this

   - name: Check if java is installed        command: java -version        become_user: '{{ global_vars.user_session }}' // your user session        register: java_result        ignore_errors: True      - debug:          msg: "Failed - Java is not installed"        when: java_result is failed      - debug:          msg: "Success - Java is installed"        when:  java_result is success