GenZ
GenZ

Reputation: 49

add list to previous list in same line

I have two lists:

 "list1": [
            "xxxx"
            "yyyyy",
            "yyyyy",
            "yyyyy",
        ]
 "list2": [
            "xxxx"
            "yyyyy",
            "yyyyy",
            "yyyyy",
        ]

I do a loop on list2 to add it to list1, it works but adds it in new line like this:

        "xxxx",
        "yyyyy",
        "yyyyy",
        "yyyyy,
        "xxxx",
        "yyyyy",
        "yyyyy",
        "yyyyy",

the loop I used

    set_fact:
      list1: "{{ list1 |default([]) + list2 }}"
    loop: "{{ list2 }}"

meanwhile expected result:

            "xxxxxxxx",
            "yyyyyyyyyy",
            "yyyyyyyyyy",
            "yyyyyyyyyy,

Upvotes: 1

Views: 207

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68044

Given the mre data

  l1: [a, b, c]
  l2: [d, e, f]

Join the items on the lists. The expected result is

  l3: [ad, be, cf]

A: zip the lists and join the items. The expression below gives the expected result

  l3: "{{ l1|zip(l2)|map('join')|list }}"

Example of a complete playbook

- hosts: localhost

  vars:

    l1: [a, b, c]
    l2: [d, e, f]
    l3: "{{ l1|zip(l2)|map('join')|list }}"

  tasks:

    - debug:
        var: l3

Upvotes: 1

Related Questions