# Get EC2 public IP

**URL:** <https://forum.ansible.com/t/get-ec2-public-ip/16813>\
**Category:** Ansible Project\
**Created:** [November 18, 2014, 4:28pm UTC](https://forum.ansible.com/t/get-ec2-public-ip/16813 "2014-11-18T16:28:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Matthew\_Morgan](https://forum.ansible.com/u/Matthew_Morgan)\
**Post date:** [November 18, 2014, 4:28pm UTC](https://forum.ansible.com/t/get-ec2-public-ip/16813/1 "2014-11-18T16:28:24Z")

</div>

Hello,

I’ve run into a situation where I need to define a variable of EC2 public IPs so I can pass them into my cassandra nodes as seeds.

The problem being, I don’t know a good way to get the public IPs. I currently use static inventory (blank, just for groups) and dynamic inventory (ec2.py).

So Ideally, I’d be able to do something like

cassandra\_seeds: “{{ hostvars[groups[‘tag\_service\_az\_cassandra-a’][ansible\_ec2\_public\_ipv4] }}”

however, this doesn’t work. ‘ansible\_ec2\_public\_ipv4’ is only present when ec2\_facts has been run. ec2\_facts can only be run on the instance ansible is currently provisioning and I need the cassandra\_seeds variable across all my cassandra instances.

Another idea was:

cassandra\_seeds: “{% for host in groups[‘tag\_service\_az\_cassandra-a’] %} {{hostvars[host].ansible\_ec2\_public\_ipv4 }} {% end for %}”

Again, this falls apart because ansible\_ec2\_public\_ipv4 isn’t present. None of the ansible\_ facts gathered have the public ip, only ec2\_facts has it.

I realize I could change ec2.ini to return public ip for vpc instances, but that would break all of my other plays… I need private ips everywhere else, this is the only instance where I need to use public ips.

The only potential solution I have figured out would be to write a record to route53 for this express purpose of recording the public ip, but that seems like a failure on using ansible correctly.

This seems like it should be really easy, What am I missing?

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 18, 2014, 11:49pm UTC](https://forum.ansible.com/t/get-ec2-public-ip/16813/2 "2014-11-18T23:49:59Z")

</div>

Instead of modifying ec2\_facts, perhaps you want to modify the ec2  
dynamic inventory plugin to return the information you need?

-Toshio

---

<div class="post-metadata">

**Author:** ![Matthew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Matthew\_Morgan](https://forum.ansible.com/u/Matthew_Morgan)\
**Post date:** [November 19, 2014, 2:25am UTC](https://forum.ansible.com/t/get-ec2-public-ip/16813/3 "2014-11-19T02:25:37Z")

</div>

While I agree, that does sound like an idea, the problem with it is that I need both, the private IP and public IP. The public IP is used on each node for broadcast\_address, the private IP is used on each node for listen\_address. In addition, each node needs a variable passed to it: cassandra\_seeds  
Cassandra\_seeds is a list of public IPs in the form of ,,.

While it would be trivial to change the vpc\_destination\_variable to ip\_address, then i wouldn’t be able to use the private IPs everywhere I need them.

Perhaps if I used two copies of the EC2 dynamic inventory script? is that even possible?

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 19, 2014, 4:59pm UTC](https://forum.ansible.com/t/get-ec2-public-ip/16813/4 "2014-11-19T16:59:00Z")

</div>

It is possible by specifying a directory as your inventory and then  
placing both inventory scripts into the directory.

However, I was thinking that you could modify the script to return  
both the public and the private IP in separate fields. Is that not  
possible?

The script should be able to even build up the cassandra\_seeds  
variable for you and return it in, for instance, a group variable for  
the hosts that need to have the seed set so that you don't have to do  
as much work with jinja filters to build up the variable inside of  
your playbook.

-Toshio "ec2 newbie" Kuratomi
