# EC2: collect list of private-ip from on group, use it in a second group

**URL:** <https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327>\
**Category:** Ansible Project\
**Created:** [January 20, 2015, 1:14pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327 "2015-01-20T13:14:57Z")\
**Posts on this page:** 7\
**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 20, 2015, 1:14pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/1 "2015-01-20T13:14:57Z")

</div>

Posted a similar question a few days ago, got the right answer.  
Now I can better explain what I’m looking for.

I’m building a EC2 system from two types of servers:

- cluster nodes
- loader nodes  
Each loader is generating traffic to ALL cluster nodes.

My goal is a playbook which collect private-ip from each the first group, and use it as part of a command to be executed on the second group.  
I assume it should look something like:

`

---

<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:** [January 20, 2015, 3:47pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/2 "2015-01-20T15:47:12Z")

</div>

Here’s a quick sample playbook that should do what you are trying to do. Keep in mind that the ec2.py dynamic inventory script will gather the private IPs for you, you shouldn’t need an explicit task.

`

- hosts: localhost  
tasks:

- debug: msg=“{{ hostvars[item].ec2\_private\_ip\_address }}”  
with\_items: groups.tag\_Name\_ClusterNodes  
register: output

- debug: msg=“{{ output.results|map(attribute=‘msg’)|join(’ ') }}”

`

You could also write a custom jinja2 filter to make this a single task.

- James

---

<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 21, 2015, 8:37am UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/3 "2015-01-21T08:37:09Z")

</div>

Thanks James  
Works like a charm. register is what I was looking for.

What should I use in production instead of the first debug?  
I need the data, not the output to the screen.

---

<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:** [January 21, 2015, 9:26pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/4 "2015-01-21T21:26:06Z")

</div>

Sorry, I’m not sure of a way to suppress that, outside of using a custom filter.

- James

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 21, 2015, 10:04pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/5 "2015-01-21T22:04:09Z")

</div>

look at set\_fact

---

<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:** [January 21, 2015, 10:30pm UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/6 "2015-01-21T22:30:19Z")

</div>

Sorry, was a bit confused by your comment “What should I use in production instead of the first debug?”

If you mean, not to use “debug” in your first task because it’s too verbose, you don’t have to. But using it is not going to harm anything even on production. If you want to do it in a less verbose way:

`

- hosts: localhost  
tasks:

- command: echo “{{ hostvars[item].ec2\_private\_ip\_address }}”  
with\_items: groups.tag\_Name\_ClusterNodes  
register: output  
changed\_when: no

- debug: msg=“{{ output.results|map(attribute=‘stdout’)| join(’ ') }}”

`

as far as the second debug goes, you could, as bcoca pointed out, use set\_fact:

`

- set\_fact: ip\_list=“”{{ output.results|map(attribute=‘stdout’)| join(’ ') }}"  
`

And subsequent tasks could then reference the list as “{{ ip\_list }}”

- James

---

<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 22, 2015, 9:49am UTC](https://forum.ansible.com/t/ec2-collect-list-of-private-ip-from-on-group-use-it-in-a-second-group/17327/7 "2015-01-22T09:49:43Z")

</div>

Thanks James, Brain  
This is precisely what I was looking for!
