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

1,610 views
Skip to first unread message

nishant bawane

unread,
May 16, 2017, 9:16:06 AM5/16/17
to Ansible Project
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 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

----------------------------------------------------------

Raj Martha

unread,
May 16, 2017, 1:45:15 PM5/16/17
to Ansible Project
Try this one
 -
  become: true
  become_user: username
  hosts: all
  tasks:
    -
      ping: ~
    -
      name: "Displaying Hostname for Server"
      register: hostnamefordmgr
      shell: hostname
    -
      debug: var=hostnamefordmgr.stdout_lines


nishant bawane

unread,
May 17, 2017, 12:42:14 PM5/17/17
to Ansible Project
I dont want in this way.. i needed it using the loop. The same is mentioned in the question

Kai Stian Olstad

unread,
May 17, 2017, 1:49:09 PM5/17/17
to ansible...@googlegroups.com
On 16. mai 2017 15:16, nishant bawane wrote:
> 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.
Maybe this will help you forward.

- debug: msg="Host is {{ item }} with java version - {{
hostvars[item].result.stdout }}"
with_items: "{{ ansible_play_batch }}"
run_once: true
delegate_to: localhost

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages