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 -
-
Why we are getting 4 outputs as we have only two entries in hosts file. I need only two results
-
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. -
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
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