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

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.

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)

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.