get a particular host name's ip with ansible

qubsup picture qubsup · Jun 6, 2017 · Viewed 14.5k times · Source

I have a host file that looks roughly like this:

[kibanamaster]
efk host_ip host user passwd
[elasticnode]
esnode host_ip user passwd

and I am trying something in the style of

- name: get ip address node1
  debug: var=hostvars[inventory_host]['esnode']['ansible_default_ipv4']['address'] 
  register: es_node1

But I get variable not defined. Anyone outthere able to help?

EDIT: If I do

debug: var=hostvars[LOG1]['esnode']['ansible_default_ipv4']['address']
register: node_1 

I get

{"hostvars[LOG1]['ansible_default_ipv4']['address']": "VARIABLE IS NOT DEFINED!"}

Answer

Konstantin Suvorov picture Konstantin Suvorov · Jun 6, 2017

hostvars magic variable is a dictionary with keys named after hosts in your inventory.

So you may want to try:

hostvars['esnode']['ansible_default_ipv4']['address']

to get ip address of esnode host.