Ansible get IP address remote server

#Introduction

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

My structure file and folder Ansible

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

1 directory, 3 files

Option 1: use hostvars

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

Option 2: Other

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

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

Conclusion

You have 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 and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. 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 *

Comment moderation is enabled. Your comment may take some time to appear.

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