简体   繁体   中英

How to check different JAVA installed in target machine using Ansible Playbook?

I am trying to write a ansible playbook and these are the overview.

  1. Java - Open JDK Installed java -version
  2. Java - Oracle JDK Installed java --fullversion
  3. Java - Not installed.

I need to determine what target machine has what Java installed or NOT installed.

That's Pretty much everything.

This is what i wrote but this once only gives me java version. Thank you for your help.

- hosts: localhost
  gather_facts: False

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

   - debug:
       msg: "{{ java_result.stdout }}"```

When you have no Java, your shell will fail. You can check this.

- hosts: localhost
  gather_facts: False

  tasks:
   - 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

   - debug: msg=No Java or something failed.
     when: java_result.rc!=0

   - debug:
       msg: "{{ java_result.stdout }}"```
     when: java_result.rc==0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM