# Create string from list

**URL:** <https://forum.ansible.com/t/create-string-from-list/27334>\
**Category:** Ansible Project\
**Tags:** azure\
**Created:** [April 20, 2018, 10:33am UTC](https://forum.ansible.com/t/create-string-from-list/27334 "2018-04-20T10:33:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbmprivat](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nbmprivat](https://forum.ansible.com/u/nbmprivat)\
**Post date:** [April 20, 2018, 10:33am UTC](https://forum.ansible.com/t/create-string-from-list/27334/1 "2018-04-20T10:33:29Z")

</div>

Hi there,

I simply cannot figure out how to create a string from a list.

What output I want in a variable:

`"{{10.0.0.1:8080,10.0.0.2:8080}}"`

I am retrieving a dynamic list of private IPs from Azure Scaleset.

`

tasks:

- name: Connect  
command: az account set -s {{ subscription\_id }}

- name: Get private IPs  
command: az vmss nic list --resource-group {{ customer\_group }} --vmss-name {{ customer }}-vmss  
register: result

- name: Filter result  
set\_fact:  
ip\_list: “{{ result.stdout | from\_json | json\_query(‘[_].ipConfigurations[_].privateIpAddress’) }}”

- set\_fact:  
foo: “{{ item }}:8080”  
with\_items: “{{ ip\_list }}”

- debug: var=foo

`

Output so far:

`

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=10.1.1.7)  
ok: [localhost] =\> (item=10.1.1.4)  
ok: [localhost] =\> (item=10.1.1.5)

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“foo”: “10.1.1.5:8080”  
}

`

I need to create a loop that puts the list into a string.

`

- set\_fact:  
foo: “{{ item }}:8080”  
with\_items: “{{ ip\_list }}”

`

Does anybody know how to do this?

---

<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:** [April 20, 2018, 11:00am UTC](https://forum.ansible.com/t/create-string-from-list/27334/2 "2018-04-20T11:00:15Z")

</div>

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo: '{% for i in ip %}{{ i }}:8080{% if not loop.last %},{% endif %}{% endfor %}'

---

<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:** [April 20, 2018, 11:02am UTC](https://forum.ansible.com/t/create-string-from-list/27334/3 "2018-04-20T11:02:46Z")

</div>

It should be {% for i in ip\_list %}

---

<div class="post-metadata">

**Author:** ![nbmprivat](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nbmprivat](https://forum.ansible.com/u/nbmprivat)\
**Post date:** [April 20, 2018, 11:03am UTC](https://forum.ansible.com/t/create-string-from-list/27334/4 "2018-04-20T11:03:09Z")

</div>

Hi Kai,

Thank you so much. I would never have figured that out. The result is however still a bit off:

`

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“foo”: “[u’10.1.1.7’]:8080,[u’10.1.1.4’]:8080,[u’10.1.1.5’]:8080”  
}

`

fredag den 20. april 2018 kl. 13.00.32 UTC+2 skrev Kai Stian Olstad:

---

<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:** [April 20, 2018, 1:14pm UTC](https://forum.ansible.com/t/create-string-from-list/27334/5 "2018-04-20T13:14:00Z")

</div>

There is noting wrong with the output, but with the input.  
Looks like you ip\_list is a list in a list or does contain more that just the IPs.

What does

- debug: var=ip\_list

say?

---

<div class="post-metadata">

**Author:** ![nbmprivat](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nbmprivat](https://forum.ansible.com/u/nbmprivat)\
**Post date:** [April 20, 2018, 1:22pm UTC](https://forum.ansible.com/t/create-string-from-list/27334/6 "2018-04-20T13:22:13Z")

</div>

Result:

`

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“ip\_list”: [  
[  
“10.1.1.7”  
],  
[  
“10.1.1.4”  
],  
[  
“10.1.1.5”  
]  
]  
}

`

fredag den 20. april 2018 kl. 15.14.18 UTC+2 skrev Kai Stian Olstad:

---

<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:** [April 20, 2018, 1:31pm UTC](https://forum.ansible.com/t/create-string-from-list/27334/7 "2018-04-20T13:31:55Z")

</div>

You have a list in a list, you need to fix your set\_fact ip\_list to just create a list.

If that is not an option this will also work for foo at least.

&nbsp;&nbsp;&nbsp;foo: '{% for i in ip %}{{ i.0 }}:8080{% if not loop.last %},{% endif %}{% endfor %}'

---

<div class="post-metadata">

**Author:** ![nbmprivat](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nbmprivat](https://forum.ansible.com/u/nbmprivat)\
**Post date:** [April 20, 2018, 1:34pm UTC](https://forum.ansible.com/t/create-string-from-list/27334/8 "2018-04-20T13:34:43Z")

</div>

AWESOME!!!

The last line worked perfectly. Many many thanks Kai!

fredag den 20. april 2018 kl. 15.32.04 UTC+2 skrev Kai Stian Olstad:
