Create string from list

20 views
Skip to first unread message

Nico

unread,
Apr 20, 2018, 6:33:30 AM4/20/18
to Ansible Project
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? 


Kai Stian Olstad

unread,
Apr 20, 2018, 7:00:32 AM4/20/18
to ansible...@googlegroups.com
On 20.04.2018 12:33, Nico wrote:
> 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 %}'

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Apr 20, 2018, 7:03:03 AM4/20/18
to ansible...@googlegroups.com
It should be {% for i in ip_list %}

--
Kai Stian Olstad

Nico

unread,
Apr 20, 2018, 7:03:09 AM4/20/18
to Ansible Project
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"
}

Message has been deleted

Kai Stian Olstad

unread,
Apr 20, 2018, 9:14:18 AM4/20/18
to ansible...@googlegroups.com
On 20.04.2018 13:03, Nico wrote:
> 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"
> }

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?


--
Kai Stian Olstad

Nico

unread,
Apr 20, 2018, 9:22:13 AM4/20/18
to Ansible Project
Result:

TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
    "ip_list": [
        [
            "10.1.1.7"
        ], 
        [
            "10.1.1.4"
        ], 
        [
            "10.1.1.5"
        ]
    ]
}

Kai Stian Olstad

unread,
Apr 20, 2018, 9:32:04 AM4/20/18
to ansible...@googlegroups.com
On 20.04.2018 15:22, Nico wrote:
> Result:
>
> TASK [debug]
> ********************************************************************************************************************************************************
> ok: [localhost] => {
> "ip_list": [
> [
> "10.1.1.7"
> ],
> [
> "10.1.1.4"
> ],
> [
> "10.1.1.5"
> ]
> ]
> }

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

Nico

unread,
Apr 20, 2018, 9:34:43 AM4/20/18
to Ansible Project
AWESOME!!!!!

The last line worked perfectly. Many many thanks Kai! 
Reply all
Reply to author
Forward
0 new messages