# Collecting a list of internal IPs from EC2

**URL:** <https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262>\
**Category:** Ansible Project\
**Created:** [January 14, 2015, 9:47am UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262 "2015-01-14T09:47:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 14, 2015, 9:47am UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262/1 "2015-01-14T09:47:21Z")

</div>

Another newbie questions (second and last for today, I promise)  
I have two groups for EC2 instances:

- Test servers
- Loader servers  
each with a different tag.  
I would like to get the list of the internal\_ip of the Test group, and use it to run a command on the Loaders group.  
Any pointer will be appreciated!

Thanks

---

<div class="post-metadata">

**Author:** ![Alex\_Clifford](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Alex\_Clifford](https://forum.ansible.com/u/Alex_Clifford)\
**Post date:** [January 14, 2015, 1:26pm UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262/2 "2015-01-14T13:26:32Z")

</div>

The following AWS CLI command will give you a list of all internal IPs (which you could run only against the Test host group):

`aws ec2 describe-instances --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text`

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 15, 2015, 1:22pm UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262/3 "2015-01-15T13:22:11Z")

</div>

Thanks Alex  
This works, but my end goal is to use the list in a playbook.

A similar example I’m struggling with:  
Terminate all EC2 instances with a specific name.  
I come up with the following:

`

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 15, 2015, 2:00pm UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262/4 "2015-01-15T14:00:48Z")

</div>

Hi Tzach

I usually approach instance termination by matching the instances with ec2 dynamic inventory, then add them to a group and terminate them in another play targeted at the newly created group, based on their instance id (which is automatically added as an inventory variable by the ec2 inventory script).

Note that I specify `gather_facts: false` and do not reference the `hostvars` dictionary. That way, Ansible does not attempt or need to reach the instances via ssh before it is able to terminate them.

```auto
- name: Find sandbox instance(s) with matching name
  hosts: tag_Name_Test
  gather_facts: false
  tasks:

    - name: Create group
      group_by: key=legacy

- hosts: legacy
  connection: local
  gather_facts: false
  tasks:

    - name: Terminate instance(s)
      ec2:
        instance_ids: "{{ ec2_id }}"
        state: absent

```

Hope this helps.

Tom

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 15, 2015, 3:00pm UTC](https://forum.ansible.com/t/collecting-a-list-of-internal-ips-from-ec2/17262/5 "2015-01-15T15:00:05Z")

</div>

Thanks Tom!  
It works, but I do not understand how or why 🙂

> Hi Tzach
> 
> I usually approach instance termination by matching the instances with ec2 dynamic inventory, then add them to a group and terminate them in another play targeted at the newly created group, based on their instance id (which is automatically added as an inventory variable by the ec2 inventory script).
> 
> Note that I specify `gather_facts: false` and do not reference the `hostvars` dictionary. That way, Ansible does not attempt or need to reach the instances via ssh before it is able to terminate them.
> 
> ```auto
> - name: Find sandbox instance(s) with matching name
> hosts: tag_Name_Test
> gather_facts: false
> tasks:
> 
> - name: Create group
> group_by: key=legacy
> 
> ```

This create a host group called Legacy

> ```auto
> 
> - hosts: legacy
> 
> ```

This use the legacy group, so why cant I use the tag\_Name\_Test directly?

> ```auto
> 
> connection: local
> 
> ```

Why this play have connection: local and first does not?

> ```auto
> 
> gather_facts: false
> tasks:
> 
> - name: Terminate instance(s)
> ec2:
> instance_ids: "{{ ec2_id }}"
> 
> ```

region: {{ region }} is also required

> ```auto
> 
> state: absent
> 
> ```
> 
> Hope this helps.

And last: can I use two different tag name, and merge them somehow?
