qubsup
qubsup

Reputation: 1391

get a particular host name's ip with ansible

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!"}

Upvotes: 8

Views: 22734

Answers (2)

Kishore Venkataramanan
Kishore Venkataramanan

Reputation: 7139

You can use pre-loaded ansible variable to get values for both ipv4, ipv6 & hostname

IPV4 --> {{ ansible_eth0.ipv4.address }}

IPV6 --> {{ ansible_eth0.ipv6.address }}

Hostname --> {{ ansible_hostname }

Upvotes: 3

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68319

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.

Upvotes: 8

Related Questions