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