merging lists

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?

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 }}"

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

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,

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

will add the lists

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

If lista will only ever contain a single item, then listb could be just

listb:

  • “{{ lista[0] }}”
  • { one: ‘str1’, two: ‘str2’ }

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

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

% cat foo.yml

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

Brilliant!

Walter

agreed, brillant. i'll give that a whirl, but it looks like exactly
what i'm after. thanks

I just tested and this also works.

  • debug: msg=“{{ [listb] | sum(start=lista) }}”

Walter

And if you really wanted only unique list items this also works …

  • debug: msg=“{{ [listb] | sum(start=lista) | unique }}”

Walter

Isn't *flatten* simpler?

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