Create string from list

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] *********************************************************************************************************************
ok: [localhost]

TASK [Connect] **********************************************************************************************************************
changed: [localhost]

TASK [Get private IPs] **************************************************************************************************************
changed: [localhost]

TASK [Filter result] ****************************************************************************************************************
ok: [localhost]

TASK [set_fact] *********************************************************************************************************************
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?

- set_fact:
       foo: '{% for i in ip %}{{ i }}:8080{% if not loop.last %},{% endif %}{% endfor %}'

It should be {% for i in ip_list %}

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:

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?

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:

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.

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

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: