# Extracting Single IP from launched EC2 instances

**URL:** <https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887>\
**Category:** Ansible Project\
**Created:** [April 1, 2015, 8:20pm UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887 "2015-04-01T20:20:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeA](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@MikeA](https://forum.ansible.com/u/MikeA)\
**Post date:** [April 1, 2015, 8:20pm UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887/1 "2015-04-01T20:20:24Z")

</div>

I’m launching a cluster of ec2 instances and would like one (don’t care which one) of them to become a head node and pass it’s IP to the other cluster members. I’ve been looking for an easy way to do this but could not fine one so would appreciate any assistance.

Here is what I’ve got.

later on the main playbook I’d like to pull one of the IP’s and feed it into a startup script to be called on all the hosts.

I could not find a way to extract element 0 from the group\_name var array.I tried this: set\_fact: headnode={{ groups[[{{ group\_name }}][0]] }} but it did not work. I had to use the actual group\_var value and then I got the IP but it was not a clean string rather it looked like this: [u’10.77.0.211’,

Any idea how to get a clean IP from the group\_name set? BDW in the sub playbook where I launch the instances I register the private ip’s from the instances: set\_fact: private\_ips: “{{ ec2.tagged\_instances|join(', ', attribute=‘private\_ip’) }}” but I cannot refer to this variable back in the main playbook.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [April 3, 2015, 6:26pm UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887/2 "2015-04-03T18:26:54Z")

</div>

Mike,

This may give you some insight:

[https://github.com/basho-labs/ansible-riak/blob/develop/examples/form\_cluster.yml](https://github.com/basho-labs/ansible-riak/blob/develop/examples/form_cluster.yml)

Particularly:

`  
primary\_node: “{{ hostvars[groups[‘riak\_cluster’][0]][‘riak\_outputs’][‘node\_name’] }}”

- James  
`

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan1](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tzach\_Livyatan1](https://forum.ansible.com/u/Tzach_Livyatan1)\
**Post date:** [April 12, 2015, 9:29am UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887/3 "2015-04-12T09:29:03Z")

</div>

Here is a similar example from my project

Creating the instance on EC2:

`

---

<div class="post-metadata">

**Author:** ![MikeA](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@MikeA](https://forum.ansible.com/u/MikeA)\
**Post date:** [April 30, 2015, 5:08pm UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887/4 "2015-04-30T17:08:37Z")

</div>

That worked. Thanks!

---

<div class="post-metadata">

**Author:** ![jbryner](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jbryner](https://forum.ansible.com/u/jbryner)\
**Post date:** [April 30, 2015, 6:10pm UTC](https://forum.ansible.com/t/extracting-single-ip-from-launched-ec2-instances/17887/5 "2015-04-30T18:10:28Z")

</div>

Anyone know if there is a way to do this same sort of discovery based on EC2 tags? i.e. Gather the IP associated with tag role: master and make use of it?
