0

I need to display the current java version using the loop in ansible. Currently i am able to fetch and display the same on the host server but when i need to pass the same values in another play it does not works there. Below is the playbook - Question is what exactly needs to be changed in the localhost play so that we can get the values of of java version , and in case in future if we add or remove any host server in that case no coding changes must be required as it must be in loop.

Host inventory  -

[testserver]
zlp12037 ansible_ssh_host=zlp12037.vci.att.com ansible_ssh_user=abc
zlp12036 ansible_ssh_host=zlp12036.vci.att.com ansible_ssh_user=abc

---
- hosts: testserver
  tasks:
- name: Fetch Java Version
  shell: java -version 2>&1 | grep    version | awk '{print $3}' | sed 's/"//g'
  register: result
- debug: msg="{{item}}:{{result.stdout}}"
  with_items: "{{ inventory_hostname }}"

- hosts: localhost
  tasks:
- debug: var=hostvars['item']['result']['stdout']
  with_items: groups['inventory_hostname']

Result: 

 TASK [Fetch Java Version]  ******************************************************
 changed: [zlp12037]
 changed: [zlp12036]

TASK [debug] *******************************************************************
ok: [zlp12037] => (item=zlp12037) => {
  "item": "zlp12037",
  "msg": "zlp12037:1.8.0_66"
}
ok: [zlp12036] => (item=zlp12036) => {
  "item": "zlp12036",
  "msg": "zlp12036:1.8.0_66"
}


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

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => (item=groups['inventory_hostname']) => {
    "hostvars['item']['result']['stdout']": "VARIABLE IS NOT DEFINED!",
    "item": "groups['inventory_hostname']"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0        failed=0
zlp12036                   : ok=3    changed=1    unreachable=0    failed=0
zlp12037                   : ok=3    changed=1    unreachable=0    failed=0

2 Answers 2

1
  1. with_items must be templated
  2. object['item'] fetch item with literal name item, while object[item] fetch item with name from variable named item
  3. groups[inventory_hostname] will try to find group named localhost in your case

I guess, you want:

- hosts: localhost
  tasks:
    - debug: var=hostvars[item]['result']['stdout']
      with_items: "{{ groups['testserver'] }}"
Sign up to request clarification or add additional context in comments.

Comments

0

You can run a bash script to execute and store the Java version value in a file which you can read in the ansible playbook.

https://stackoverflow.com/a/7335524/7328096

            if type -p java; then
                echo found java executable in PATH
                _java=java
            elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  
 then
                echo found java executable in JAVA_HOME     
                _java="$JAVA_HOME/bin/java"
            else
                echo "no java"
            fi

            if [[ "$_java" ]]; then
                version=$("$_java" -version 2>&1 | awk -F '"' '/version/ 
  {print $2}')
                echo version "$version"
                if [[ "$version" > "1.5" ]]; then
                    echo version is more than 1.5
                else         
                    echo version is less than 1.5
                fi
            fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.