merging lists

9 views
Skip to first unread message

Michael DiDomenico

unread,
Apr 17, 2023, 6:22:41 PM4/17/23
to ansible...@googlegroups.com
i want to do this, but i'm not getting exactly what i want

lista:
- { one: 'str1', two: 'str2' }

listb:
- "{{ lookup('vars','lista') }}"
- { one: 'str1', two: 'str2' }

the output i want is

listb:
{ one: 'str1', two: 'str2' },
{ one: 'str1', two: 'str2' }

but what i get is

listb:
[ { one: 'str1', two: 'str2' } ],
{ one: 'str1', two: 'str2' }

is there a way to merge the two lists?

Vladimir Botka

unread,
Apr 17, 2023, 7:05:46 PM4/17/23
to Michael DiDomenico, ansible...@googlegroups.com
On Mon, 17 Apr 2023 18:22:10 -0400
Michael DiDomenico <mdidom...@gmail.com> wrote:

> lista:
> - { one: 'str1', two: 'str2' }
>
> listb:
> - "{{ lookup('vars','lista') }}"
> - { one: 'str1', two: 'str2' }
>
> the output i want is
>
> listb:
> { one: 'str1', two: 'str2' },
> { one: 'str1', two: 'str2' }
>
> but what i get is
>
> listb:
> [ { one: 'str1', two: 'str2' } ],
> { one: 'str1', two: 'str2' }

The lookup *vars* is not necessary. Reference the variable

listb:
- "{{ lista }}"
- {one: str1, two: str2}

You should get

listb:
- - one: str1
two: str2
- one: str1
two: str2

This is correct. The first item on the list *listb* is list *lista*.
If you put two items into the list *lista*

lista:
- one: str1
two: str2
- ccc: str3
ddd: str4

you'll get

listb:
- - one: str1
two: str2
- ccc: str3
ddd: str4
- one: str1
two: str2

You can *flatten* the list

listc: "{{ listb|flatten }}"

gives

listc:
- one: str1
two: str2
- ccc: str3
ddd: str4
- one: str1
two: str2

However, a simpler option is adding lists. For example, given two
lists

lista:
- {one: str1, two: str2}
- {ccc: str3, ddd: str4}
listb:
- {one: str1, two: str2}

Declare the list *listc*. This will merge the two lists

listc: "{{ lista + listb }}"


--
Vladimir Botka

Michael DiDomenico

unread,
Apr 17, 2023, 7:29:29 PM4/17/23
to ansible...@googlegroups.com
thanks for the help, but that's not exactly what i am after. i'm
attempting to avoid creating a third variable. but maybe that's the
only way

Vladimir Botka

unread,
Apr 18, 2023, 1:00:43 AM4/18/23
to Michael DiDomenico, ansible...@googlegroups.com
On Mon, 17 Apr 2023 19:29:02 -0400
Michael DiDomenico <mdidom...@gmail.com> wrote:

> thanks for the help, but that's not exactly what i am after. i'm
> attempting to avoid creating a third variable. but maybe that's the
> only way

You can avoid the third variable if you want to. For example,

a: [1, 2, 3]
b: "{{ a + [4, 5, 6] }}"

will add the lists

b: [1, 2, 3, 4, 5, 6]

--
Vladimir Botka

Dick Visser

unread,
Apr 18, 2023, 4:38:10 AM4/18/23
to ansible...@googlegroups.com
If lista will only ever contain a single item, then listb could be just

listb:
- "{{ lista[0] }}"

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 8:57:12 AM4/18/23
to ansible...@googlegroups.com


---
- name: test lists
  become: false
  gather_facts: false
  hosts: localhost
  vars:

    lista:
      - { one: 'str1', two: 'str2' }

    listb:
     - { one: 'str1', two: 'str2' }
  tasks:
    - debug: var=lista
    - debug: var=listb
    - set_fact:
        listb: [ "{{ lista + listb }}" ]
    - debug: var=listb


% ansible-playbook foo.yml

PLAY [test lists] ******************************************************************************************************

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
    "lista": [
        {

            "one": "str1",
            "two": "str2"
        }
    ]
}

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {

    "listb": [
        {
            "one": "str1",
            "two": "str2"
        }
    ]
}

TASK [set_fact] ********************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {

    "listb": [
        [
            {
                "one": "str1",
                "two": "str2"
            },
            {
                "one": "str1",
                "two": "str2"
            }
        ]
    ]
}

PLAY RECAP *************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   




Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Michael DiDomenico

unread,
Apr 18, 2023, 9:07:57 AM4/18/23
to ansible...@googlegroups.com
On Tue, Apr 18, 2023 at 8:56 AM 'Rowe, Walter P. (Fed)' via Ansible
Project <ansible...@googlegroups.com> wrote:
>
> TASK [debug] ***********************************************************************************************************
> ok: [localhost] => {
> "listb": [
> [
> {
> "one": "str1",
> "two": "str2"
> },
> {
> "one": "str1",
> "two": "str2"
> }
> ]
> ]
> }

i got to this point as well in my testing, but the double list
expansion of listb is a problem.

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 9:09:10 AM4/18/23
to ansible...@googlegroups.com
This isn't a double expansion of listb. lista and listb happen to have the same values. change one of them and run the playbook to prove it.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 9:11:06 AM4/18/23
to ansible...@googlegroups.com


% cat foo.yml 

---

- name: test lists

  become: false

  gather_facts: false

  hosts: localhost

  vars:

    lista:

      - { one: 'str1', two: 'str2' }


    listb:

      - { three: 'str3', four: 'str4' }

  tasks:

    - debug: var=lista

    - debug: var=listb

    - set_fact:

        listb: [ "{{ lista + listb }}" ]

    - debug: var=listb




% ansible-playbook foo.yml


PLAY [test lists] ******************************************************************************************************


TASK [debug] ***********************************************************************************************************

ok: [localhost] => {

    "lista": [

        {

            "one": "str1",

            "two": "str2"

        }

    ]

}


TASK [debug] ***********************************************************************************************************

ok: [localhost] => {

    "listb": [

        {

            "four": "str4",

            "three": "str3"

        }

    ]

}


TASK [set_fact] ********************************************************************************************************

ok: [localhost]


TASK [debug] ***********************************************************************************************************

ok: [localhost] => {

    "listb": [

        [

            {

                "one": "str1",

                "two": "str2"

            },

            {

                "four": "str4",

                "three": "str3"

            }

        ]

    ]

}


PLAY RECAP *************************************************************************************************************

localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
On Apr 18, 2023, at 9:07 AM, Michael DiDomenico <mdidom...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Todd Lewis

unread,
Apr 18, 2023, 10:40:55 AM4/18/23
to ansible...@googlegroups.com, uto...@gmail.com
Also, by using the sum() filter:
[utoddl@tango ansible]$ cat foo.yml
---
- name: test lists
  become: false
  gather_facts: false
  hosts: localhost
  vars:
    list_12:
      - { one: 'str1', two: 'str2' }
    list_34:
      - { three: 'str3', four: 'str4' }
    list_56:
      - { five: 'str5', six: 'str6' }
  tasks:
    - debug: msg="[ {{ list_12, list_34, list_56 }} ]"
    - debug: msg="{{ [list_12, list_34, list_56] | sum(start=[]) }}"

[utoddl@tango ansible]$ ansible-playbook foo.yml

PLAY [test lists] ***************************************************************

TASK [debug] ********************************************************************
ok: [localhost] => {
    "msg": [
        [
            [
                {
                    "one": "str1",
                    "two": "str2"
                }
            ],
            [
                {
                    "four": "str4",
                    "three": "str3"
                }
            ],
            [
                {
                    "five": "str5",
                    "six": "str6"
                }
            ]
        ]
    ]
}

TASK [debug] ********************************************************************
ok: [localhost] => {
    "msg": [
        {
            "one": "str1",
            "two": "str2"
        },
        {
            "four": "str4",
            "three": "str3"
        },
        {
            "five": "str5",
            "six": "str6"
        }
    ]
}

PLAY RECAP **********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 10:43:25 AM4/18/23
to ansible...@googlegroups.com
Brilliant!


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Michael DiDomenico

unread,
Apr 18, 2023, 11:02:07 AM4/18/23
to ansible...@googlegroups.com
agreed, brillant. i'll give that a whirl, but it looks like exactly
what i'm after. thanks
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4ab442a2-1b5a-5565-fc26-07994cb80dfd%40gmail.com.

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 12:44:21 PM4/18/23
to ansible...@googlegroups.com
I just tested and this also works.

    - debug: msg="{{ [listb] | sum(start=lista) }}"

Walter




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.



--
Todd

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Apr 18, 2023, 12:46:50 PM4/18/23
to ansible...@googlegroups.com
And if you really wanted only unique list items this also works ...

    - debug: msg="{{ [listb] | sum(start=lista) | unique }}"


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Vladimir Botka

unread,
Apr 18, 2023, 1:12:32 PM4/18/23
to Todd Lewis, ansible...@googlegroups.com
On Tue, 18 Apr 2023 10:40:40 -0400
Todd Lewis <uto...@gmail.com> wrote:

> - debug: msg="{{ [list_12, list_34, list_56] | sum(start=[]) }}"

Isn't *flatten* simpler?

- debug: msg="{{ [list_12, list_34, list_56] | flatten }}"


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages