# How to get the IP Address for a Server with multiple IP Addresses

**URL:** <https://forum.ansible.com/t/how-to-get-the-ip-address-for-a-server-with-multiple-ip-addresses/19635>\
**Category:** Ansible Project\
**Created:** [October 27, 2015, 6:09pm UTC](https://forum.ansible.com/t/how-to-get-the-ip-address-for-a-server-with-multiple-ip-addresses/19635 "2015-10-27T18:09:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manderson](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@manderson](https://forum.ansible.com/u/manderson)\
**Post date:** [October 27, 2015, 6:09pm UTC](https://forum.ansible.com/t/how-to-get-the-ip-address-for-a-server-with-multiple-ip-addresses/19635/1 "2015-10-27T18:09:08Z")

</div>

I’m using ansible to install a cassandra cluster. Cassandra wants ip addresses and not hostnames in it’s config file. I need to get the ip address for each host. The problem is each physical server has multiple ip addresses associated with it. So I can’t just get the ip address using ansible\_eth0[“ipv4”][“address”]. That will not be the correct ip address for the related host. The only var that has the correct ip I need is SSH\_CONNECTION. But that is a string that has the source ip and port and the dest ip and port. The dest ip is the ip I need. Can I parse this string and get the ip I need or is there another way to get the ip address?

I also need to get the ip address for the first host and make it available for use in a template applied to the remaining hosts.

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [October 29, 2015, 6:41am UTC](https://forum.ansible.com/t/how-to-get-the-ip-address-for-a-server-with-multiple-ip-addresses/19635/2 "2015-10-29T06:41:56Z")

</div>

If the IP address you want is the hosts primary address you could access it via the ansible\_default\_ipv4[‘address’] var.  
If you want to make sure the var you are using is the same you are also using to connect via ssh try using the annsible\_ssh\_host.  
Also you should be able to use the SSH\_CONNECTION and just extract the IP like so:

my\_ip = “{{ ansible\_env.ssh\_connection.split(’ ')[0]}}”

(can’t test the syntax right now, but it should work)

---

<div class="post-metadata">

**Author:** ![BostonMoto](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@BostonMoto](https://forum.ansible.com/u/BostonMoto)\
**Post date:** [October 29, 2015, 3:43pm UTC](https://forum.ansible.com/t/how-to-get-the-ip-address-for-a-server-with-multiple-ip-addresses/19635/3 "2015-10-29T15:43:30Z")

</div>

You could run a shell command and then grep out the IP from there and use register to save the IP as a variable to use later.
