Ansible get ip address of current host or target

How to get the IP address of the current or remote host in Ansible. That's the question, this article is going to address.

While running a Playbook you might have had a requirement where you need to get the IP address of the connected and current remote host.  There are many different ways to obtain the IP address of the remote host but it has multiple network channel like eth0 and enp0s8  etc.

Sometimes the Default IPv4 address of the remote host is not the one you are looking for or it is not the one you have used to connect.

So the question is How do you get the IP address. In this article, we are going to see various methods to get that Done including the obvious Default IPv4 and eth0 choice.

But the First one is my favourite and proved to be giving the IP address of the remote host I want.

You can make your choice.

So let us learn how ansible get ip address of the remote host.

 

Method1:  Get the IP used by Ansible master to connect

In this method, we are going to use the IP address used by Ansible master to connect to the Remote Host.

Sometimes, we would use the public IP to connect to the remote host in such cases we would want to use this method.

AWS hosts are the best example for this type as this method would use the public IP or the domain_name we have used to connect to the EC2 instance.

This is the method I have been using for a while and It never failed me.  The /etc/hosts update post tagged above has also implemented with this method.

So in this method. to get the remote IP address with ansible  we are relying on the SSH connectivity between the Ansible Master and remote host(target)

If you know already, ansible Gather_facts collects all the information about the remote hosts and it provides a various lot of information about the remote host including the IP address being used for SSH connection.

To know more about Ansible Gather_facts and to see what facts are being collected. Check this post

Now back to the objective.

The following playbook would show how to get the IP address of the remote target or host using the SSH Connection between the ansible master and the host.

---
- hosts: all
  gather_facts: yes
  tasks:
    - debug: var=hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2]

If you refer this post  and look at the Facts being collected you would be able to understand where this information is coming from.

Also, this is the IP being used to ansible master to connect to the target, so this can be used as the Righteous IP in all requirements.

Execution Output

$ ansible-playbook GetIPHosts.yml -i ansible_hosts 

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [mwiapp05]
ok: [mwiapp04]

TASK [debug] *************************************************************************************************************************************************
ok: [mwiapp04] => {
    "hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2]": "192.168.43.15"
}
ok: [mwiapp05] => {
    "hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2]": "192.168.43.16"
}

PLAY RECAP ***************************************************************************************************************************************************
mwiapp04                   : ok=2    changed=0    unreachable=0    failed=0   
mwiapp05                   : ok=2    changed=0    unreachable=0    failed=0

you can see the IP address of the remote host is displayed. ( highlighted with Yellow colour)

Note*: Since All these information are derived from the facts collected. If you set Gather_facts as no in your playbook for some reason. The method would not work.

There is another best example you can refer for this method.  We have written a post about updating the /etc/hosts files of all the hosts in the host group using ansible where we make an entry in /etc/hosts file in each other across the whole host group and multiple hosts.

You can refer that post here.

ansible update /etc/hosts file with IP of all hosts across all hosts

Method2: Use the Default IPv4 address of the remote host

In a Well setup Linux Virtual Machine,  The Default IPv4 and IPV6 address would accept the Connections from the remote and that is the IP address we would also be used in  ansible_hosts file as well.

This method is not suitable when your remote hosts are AWS EC2 machines. as this would return the private IP of the host

If you SSH into the server and say ping hostname you will get this IP address as a return.

From remote, you can also SSH using this IP to the host. In such cases, the following playbook (or) method would help you to get the IP address of the remote host.

---
- hosts: all
  tasks:
    - debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
    - debug: var=hostvars[inventory_hostname]['ansible_default_ipv6']['address']

This is taking the default IPv4 and IPv6 address of the current remote host.

Execution Output

aksarav@middlewareinventory:/apps/vagrant/webinfra/AnsibleWorkSpace$ ansible-playbook GetIPofHosts.yml -i ansible_hosts

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [mwiapp05]
ok: [mwiapp04]

TASK [debug] *************************************************************************************************************************************************
ok: [mwiapp04] => {
    "hostvars[inventory_hostname]['ansible_default_ipv4']['address']": "10.0.2.15"
}
ok: [mwiapp05] => {
    "hostvars[inventory_hostname]['ansible_default_ipv4']['address']": "10.0.2.15"
}

TASK [debug] *************************************************************************************************************************************************
ok: [mwiapp04] => {
    "hostvars[inventory_hostname]['ansible_default_ipv6']['address']": "VARIABLE IS NOT DEFINED!"
}
ok: [mwiapp05] => {
    "hostvars[inventory_hostname]['ansible_default_ipv6']['address']": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP ***************************************************************************************************************************************************
mwiapp04                   : ok=3    changed=0    unreachable=0    failed=0   
mwiapp05                   : ok=3    changed=0    unreachable=0    failed=0

Since my remote hosts do not have default_ipv6 address configured.  The VARIABLE IS NOT DEFINED message have come otherwise the IPv4 address of the remote host is displayed and highlighted in yellow.

If this is the IP address you are looking for you can choose this method.

Method 3:  Get the IP of Remote Network interface name eth0.

Mostly the Linux servers used to have eth0 as the default network interface name but this tradition is becoming an era.

Now some of the Linux systems keep different names to their default network interface such as en3 or enp0s3 etc

This can be used in AWS on certain circumstances but this would reveal only the private IP address not the public ip

In my case, the remote hosts are having a default interface name enp0s3

So, Let's see how to get the target's IP address using the network interface name

---
- hosts: all
  # this is by default YES. So no need mention it anyway
  gather_facts: yes
  tasks:
    # Getting the IP address of enp0s3 interface
    - debug: var=ansible_enp0s3.ipv4.address
    # Getting the IP address of eth0 interface
    - debug: var=ansible_eth0.ipv4.address

You can see we have used the interface names to fetch the IP address of the connected target host.

Execution Output

aksarav@middlewareinventory:/apps/vagrant/webinfra/AnsibleWorkSpace$ ansible-playbook GetIPInterface.yml -i ansible_hosts

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [mwiapp05]
ok: [mwiapp04]

TASK [debug] *************************************************************************************************************************************************
ok: [mwiapp04] => {
    "ansible_enp0s3.ipv4.address": "10.0.2.15"
}
ok: [mwiapp05] => {
    "ansible_enp0s3.ipv4.address": "10.0.2.15"
}

TASK [debug] *************************************************************************************************************************************************
ok: [mwiapp04] => {
    "ansible_eth0.ipv4.address": "192.168.43.15"
}
ok: [mwiapp05] => {
    "ansible_eth0.ipv4.address": "192.168.43.16"
}

PLAY RECAP ***************************************************************************************************************************************************
mwiapp04                   : ok=3    changed=0    unreachable=0    failed=0   
mwiapp05                   : ok=3    changed=0    unreachable=0    failed=0

You can see the different interfaces have different IP addresses. You can choose this method if you are looking to get the IP address based on the interface name. If you are sure that eth0 is your default network interface you can use ansible_eth0.ipv4.address to get the default IP address of the remote target.

So these are the three different methods to get the IP address of the remote hosts. If you feel that there is more feel free to comment and let the readers know.

Thanks for reading.

Rate this article [ratings]

Cheers,
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading