# concatenate multiple results

**URL:** <https://forum.ansible.com/t/concatenate-multiple-results/27635>\
**Category:** Ansible Project\
**Created:** [May 26, 2018, 2:32am UTC](https://forum.ansible.com/t/concatenate-multiple-results/27635 "2018-05-26T02:32:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 26, 2018, 2:32am UTC](https://forum.ansible.com/t/concatenate-multiple-results/27635/1 "2018-05-26T02:32:10Z")

</div>

Hellos guys,

I have a result from ec2\_instance\_facts, and i can get the ip addresses like:  
`

- name: Get IP addresses from results  
set\_fact:  
inject\_ips: “{{ facts\_output | json\_query(‘instances.public\_ip\_address’) }}”

`

and that gives me something like:

`  
ok: [localhost] =\> {  
“ansible\_facts”: {  
“inject\_ips”: [  
“1.2.3.4”,  
“1.2.3.4”,  
“1.2.3.4”  
]

},  
“changed”: false  
}  
`

but i need the list to include the name, like:

`  
ok: [localhost] =\> {  
“ansible\_facts”: {  
“inject\_ips”: [  
“1.2.3.4,name1”,  
“1.2.3.4,name2”,  
“1.2.3.4,name3”  
]

},  
“changed”: false  
}

`

I can also get the name with:

`

- name: Get IP addresses from results  
set\_fact:  
inject\_ips: “{{ facts\_output | json\_query(‘instances.tags.Name’) }}”  
`

But i don’t know how to concatenate them, i tried:

`

- name: Get IP addresses from results  
set\_fact:  
inject\_ips: “{{ facts\_output | json\_query(‘instances.public\_ip\_address’) }},{{ facts\_output | json\_query(‘instances.tags.Name’) }}”

`

which gives me:

`  
ok: [localhost] =\> {  
“ansible\_facts”: {  
“inject\_ips”: [  
[  
“1.2.3.4”,  
“1.2.3.4”,  
“1.2.3.4”,  
“1.2.3.4”  
],  
[  
“name1”,  
“name2”,  
“name3”,  
“name4”  
]  
]  
},  
“changed”: false  
}

`

The point is to then use it like:

`

- name: execute to add the ips  
command: “exec item[ip] item[name]”  
run\_once: true  
with\_items:
- “{{ inject\_ips }}”  
`

Right now I’m splitting them by comma “,”

Any suggestion is welcomed!

Help is very appreciated!

Thanks

David.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 26, 2018, 6:47pm UTC](https://forum.ansible.com/t/concatenate-multiple-results/27635/2 "2018-05-26T18:47:47Z")

</div>

> Hellos guys,
> 
> I have a result from ec2\_instance\_facts, and i can get the ip addresses  
> like:  
> &nbsp;&nbsp;- name: Get IP addresses from results  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject\_ips: “{{ facts\_output | json\_query(‘instances.  
> public\_ip\_address’) }}”

\<snip /\>

> I can also get the name with:
> 
> &nbsp;&nbsp;- name: Get IP addresses from results  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject\_ips: “{{ facts\_output | json\_query(‘instances.tags.Name’)  
> }}”
> 
> But i don't know how to concatenate them, i tried:
> 
> &nbsp;&nbsp;- name: Get IP addresses from results  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject\_ips: “{{ facts\_output | json\_query(‘instances.  
> public\_ip\_address’) }},{{ facts\_output | json\_query(‘instances.tags.Name  
> ’) }}”

\<snip /\>

> The point is to then use it like:
> 
> &nbsp;&nbsp;- name: execute to add the ips  
> &nbsp;&nbsp;&nbsp;&nbsp;command: “exec item[ip] item[name]”  
> &nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- “{{ inject\_ips }}”
> 
> Right now I'm splitting them by comma ","
> 
> Any suggestion is welcomed!

Why split them up, why not just use facts\_output directly.

&nbsp;&nbsp;&nbsp;- name: execute to add the ips  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: “exec item.public\_ip\_address item.tags.Name”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- “{{ facts\_output.instances }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 26, 2018, 6:51pm UTC](https://forum.ansible.com/t/concatenate-multiple-results/27635/3 "2018-05-26T18:51:21Z")

</div>

A little correction

&nbsp;&nbsp;&nbsp;- name: execute to add the ips  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: “exec {{ item.public\_ip\_address }} {{ item.tags.Name }}”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- “{{ facts\_output.instances }}”
