# Can Ipaddress of the target be fetched by ansible?

**URL:** <https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010>\
**Category:** Ansible Project\
**Created:** [November 11, 2016, 5:30pm UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010 "2016-11-11T17:30:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anirudh\_vasudevan](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@anirudh\_vasudevan](https://forum.ansible.com/u/anirudh_vasudevan)\
**Post date:** [November 11, 2016, 5:30pm UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010/1 "2016-11-11T17:30:16Z")

</div>

I know the conventional way of using Ansible is copying the ssh key on to the target and then adding the ip address to the inventory file. In my case I will not know the Ip address of the target that am testing on, how can ansible get me the ip address? Any workaround

Thanks in advance

Thanks,

Anirudh Vasudevan

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 11, 2016, 9:20pm UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010/2 "2016-11-11T21:20:04Z")

</div>

> I know the conventional way of using Ansible is copying the ssh key on to  
> the target and then adding the ip address to the inventory file.

I don't agree in this statement, I use hostname and username/password.

> In my case  
> I will not know the Ip address of the target that am testing on, how can  
> ansible get me the ip address? Any workaround

DNS?

---

<div class="post-metadata">

**Author:** ![anirudh\_vasudevan](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@anirudh\_vasudevan](https://forum.ansible.com/u/anirudh_vasudevan)\
**Post date:** [November 11, 2016, 10:52pm UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010/3 "2016-11-11T22:52:48Z")

</div>

Thanks for the reply.

No what I meant was how can you make ansible talk to a target host without ansible host file not hard coding with the ip address, am newbie to ansible. Apologizes if this seems straightforward.

Thanks,  
Anirudh Vasudevan

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 11, 2016, 11:04pm UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010/4 "2016-11-11T23:04:39Z")

</div>

Yes, that was what i tried to say.  
You don't have to use the IP in the inventory file, you can use the hostname, Ansible do use DNS to do lookup.

If that do not answer your question you need to provide us with a lot more information about you setup.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [November 12, 2016, 11:15am UTC](https://forum.ansible.com/t/can-ipaddress-of-the-target-be-fetched-by-ansible/23010/5 "2016-11-12T11:15:53Z")

</div>

It's hard to give details because you haven't explained why you don't  
know the IP.

I've used multicast DNS (via avahi ) on DHCP booted Raspberry Pis to  
advertise hostnames, then  
put those hostnames into groups in flat file inventories.

That means the servers can change IP without having to update any  
config on the ansible node.

You're copying this key up so you'll need some way to find the server, right?  
Ansible inventories can be scripts as well as flat files (that's how  
things like it's cloud support  
works). There's lots of information at:

[http://docs.ansible.com/ansible/intro\_dynamic\_inventory.html](http://docs.ansible.com/ansible/intro_dynamic_inventory.html)
