Ansible get IP address remote server

#Introduction

In this tutorial, How to use Ansible get IP address remote server. There are many methods to obtain the IP address the current of remote hosts. so let us learn how to use Ansible to get an IP address to remote hosts’ servers.

Structure file and folder Ansible get IP address remote server

[vagrant@ansible_controller ~]$ pwd
/home/vagrant
[vagrant@ansible_controller ~]$ tree .
.
├── ansible
│   ├── ansible.cfg
│   └── hosts
└── getIP.yml

1 directory, 3 files

Option 1: use hostvars

To retrieve the IP address of a remote server using Ansible, you can use the ansible_default_ipv4 variable. This variable provides information about the default IPv4 address of the target host.

For example the content file getIP.yml as below

[vagrant@ansible_controller ~]$ cat getIP.yml
---
- hosts: servers
  tasks:
  - name: Test hosts list
    debug:
      msg: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

The output on terminal

[vagrant@ansible_controller ~]$ ansible-playbook getIP.yml -i .ansible/hosts

PLAY [servers] *********************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [server1]

TASK [Test hosts list] *************************************************************************************************
ok: [server1] => {
    "msg": "10.0.2.15"
}

PLAY RECAP *************************************************************************************************************
server1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Here’s an example of how you can access the IP address:

Ansible get IP address remote server

Option 2: Other

For example the content file getIP.yml as below

[vagrant@ansible_controller ~]$ cat getIP.yml
---
- hosts: servers
  gather_facts: yes
  tasks:
  - name: Get IP address remote server
    debug:
      msg: "{{ hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2] }}"

The output on terminal

[vagrant@ansible_controller ~]$ ansible-playbook getIP.yml -i .ansible/hosts

PLAY [servers] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [server1]

TASK [Get IP address remote server] ************************************************************************************
ok: [server1] => {
    "msg": "192.168.3.11"
}

PLAY RECAP *************************************************************************************************************
server1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Option 3: lookup and dig

For example the content file getIP.yml as below

[vagrant@ansible_controller ~]$ cat getIP.yml
---
- hosts: servers
  tasks:
  - name: Test hosts list
    debug:
      msg: "{{ lookup('dig', ansible_host) }}"

Make sure you have proper SSH access to the remote server and the necessary permissions to gather facts from it.

Conclusion

You have to use Ansible get IP address remote server. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.