# How to display the current java version and host name using loop in ansible.

**URL:** <https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493>\
**Category:** Ansible Project\
**Created:** [May 16, 2017, 1:16pm UTC](https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493 "2017-05-16T13:16:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [May 16, 2017, 1:16pm UTC](https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493/1 "2017-05-16T13:16:06Z")

</div>

I need to show the current java version and the host name of the host server using Loop in my playbook. Currently i am able to fetch the hostname but not able o append the result which i got in my registered variable. Also, output which i am getting is not the expected one.  
There are some queries i have -

1. Why we are getting 4 outputs as we have only two entries in hosts file. I need only two results

2. i need to append the output of “register” variable with the {{item}}, so the output must look like -  
i) host is zlp12037 with java version - 1.7  
ii) host is zlp12036 with java version - 1.6  
in future if i add any entry of the hosts, then it must work in loop and provide one more output in the same manner.

3. I need to pass this output to the localhost which will be my another play, so how can i achieve this result in other play with hosts= localhost.

Any help will be really appreciated as i am new to this.

Below are the details -

Host inventory -

[testserver]  
zlp12037 ansible\_ssh\_host=[zlp12037.vci.att.com](http://zlp12037.vci.att.com) ansible\_ssh\_user=abc  
zlp12036 ansible\_ssh\_host=[zlp12036.vci.att.com](http://zlp12036.vci.att.com) ansible\_ssh\_user=abc

Playbook:

- hosts: testserver  
tasks:

- name: Get the Host Name  
shell: host $(hostname -i) | awk ‘{print $NF }’|sed ‘s/.$//’  
register: hosts

- name: Fetch Java Version  
shell: java -version 2\>&1 | grep version | awk ‘{print $3}’ | sed ‘s/"//g’  
register: result

- debug: msg=“host is {{item}}”  
with\_items: “{{ groups[‘testserver’] }}”

Result of playbook -

PLAY [testserver] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [zlp12037]  
ok: [zlp12036]

TASK [Get the Host Name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [zlp12037]  
changed: [zlp12036]

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

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [zlp12037] =\> (item=zlp12037) =\> {  
“item”: “zlp12037”,  
“msg”: “host is zlp12037”  
}  
ok: [zlp12037] =\> (item=zlp12036) =\> {  
“item”: “zlp12036”,  
“msg”: “host is zlp12036”  
}  
ok: [zlp12036] =\> (item=zlp12036) =\> {  
“item”: “zlp12036”,  
“msg”: “host is zlp12036”  
}  
ok: [zlp12036] =\> (item=zlp12037) =\> {  
“item”: “zlp12037”,  
“msg”: “host is zlp12037”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
zlp12036 : ok=4 changed=2 unreachable=0 failed=0  
zlp12037 : ok=4 changed=2 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Raj\_Martha](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@Raj\_Martha](https://forum.ansible.com/u/Raj_Martha)\
**Post date:** [May 16, 2017, 5:45pm UTC](https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493/2 "2017-05-16T17:45:15Z")

</div>

Try this one

---

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [May 17, 2017, 4:42pm UTC](https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493/3 "2017-05-17T16:42:14Z")

</div>

I dont want in this way… i needed it using the loop. The same is mentioned in the question

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 17, 2017, 5:48pm UTC](https://forum.ansible.com/t/how-to-display-the-current-java-version-and-host-name-using-loop-in-ansible/24493/4 "2017-05-17T17:48:44Z")

</div>

> I need to show the current java version and the host name of the host  
> server using Loop in my playbook. Currently i am able to fetch the hostname  
> but not able o append the result which i got in my registered variable.  
> Also, output which i am getting is not the expected one.  
> There are some queries i have -  
> 1. Why we are getting 4 outputs as we have only two entries in hosts file.  
> I need only two results

Testserver contain two host, both host run the loop for both of them, 2\*2 = 4.  
Add "run\_once: true" to only run the loop one time.

> 2. i need to append the output of "register" variable with the {{item}}, so  
> the output must look like -  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i) host is zlp12037 with java version - 1.7  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii) host is zlp12036 with java version - 1.6  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in future if i add any entry of the hosts, then it must work in loop  
> and provide one more output in the same manner.
> 
> 3. I need to pass this output to the localhost which will be my another  
> play, so how can i achieve this result in other play with hosts= localhost.
> 
> Any help will be really appreciated as i am new to this.
> 
> Below are the details -
> 
> 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
> 
> Playbook:
> 
> - hosts: testserver  
> &nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;- name: Get the Host Name  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: host $(hostname -i) | awk '{print $NF }'|sed 's/\.$//'  
> &nbsp;&nbsp;&nbsp;&nbsp;register: hosts
> 
> &nbsp;&nbsp;- name: Fetch Java Version  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: java -version 2\>&1 | grep version | awk '{print $3}' | sed  
> 's/"//g'  
> &nbsp;&nbsp;&nbsp;&nbsp;register: result  
> &nbsp;&nbsp;- debug: msg="host is {{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups['testserver'] }}"

Maybe this will help you forward.

- debug: msg="Host is {{ item }} with java version - {{ hostvars[item].result.stdout }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ ansible\_play\_batch }}"  
&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;delegate\_to: localhost
