# Combining 2 lists of strings with a separator

**URL:** <https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612>\
**Category:** Ansible Project\
**Created:** [September 27, 2017, 2:30pm UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612 "2017-09-27T14:30:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [September 27, 2017, 2:30pm UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612/1 "2017-09-27T14:30:54Z")

</div>

hello guys,  
I need to build filenames based on 3 parts: basename, version and trailing ‘.x86\_64.rpm’.  
Let’s focus on the first 2 parts:  
The first one is:

“installed\_mtx\_packages\_name”: [  
“mtx-device”,  
“mtx-grpc-agent”,  
“mtx-infra”,  
…  
]

The second one is:

“installed\_mtx\_packages\_version”: [  
“1.0.0-r1705191346”,  
“1.0.1-r1705191346”,  
“1.0.0-r1705170158”,  
…  
]

the goal is to obtain this:  
“installed\_mtx\_packages\_partialname”: [  
“mtx-device-1.0.0-r1705191346”,  
“mtx-grpc-agent-1.0.0-r1705191346”,  
“mtx-infra-1.0.0-r1705170158”,  
…  
]

I’ve tried several strategies, including:

- name: Building partial name of installed MTX packages  
set\_fact:  
installed\_mtx\_packages\_partialname: “{{ installed\_mtx\_packages\_name | map(‘regex\_replace’, ‘(.\*)’, ‘\1-item’) | list }}”  
with\_items: installed\_mtx\_packages\_version

But of course the item is considered as a string here instead of a variable.

If I move the ’ just before item, I get the error:  
"template error while templating string: expected token ‘,’, got ‘string’ "

Any suggestion?

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [September 27, 2017, 3:56pm UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612/2 "2017-09-27T15:56:34Z")

</div>

> hello guys,  
> I need to build filenames based on 3 parts: basename, version and  
> trailing '.x86\_64.rpm'.  
> Let's focus on the first 2 parts:  
> The first one is:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"installed\_mtx\_packages\_name": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-device",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-grpc-agent",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-infra",  
> ...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]
> 
> The second one is:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"installed\_mtx\_packages\_version": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"1.0.0-r1705191346",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"1.0.1-r1705191346",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"1.0.0-r1705170158",  
> ...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]
> 
> the goal is to obtain this:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"installed\_mtx\_packages\_partialname": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-device-1.0.0-r1705191346",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-grpc-agent-1.0.0-r1705191346",

Should this line be "mtx-grpc-agent-1.0.1-r1705191346" by any chance?  
Just checking if the example might be a typo 🙂

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtx-infra-1.0.0-r1705170158",  
> ...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]

This is not an answer for your question, but maybe a nudge to rethink  
it if possible - do you absolutely have to form these as two separate  
lists? Where do the installed\_mtx\_packages\_name and  
installed\_mtx\_packages\_version variables come from? Command, some  
external file?

Otherwise, you may want to look at this (if I understood your  
requirements well):

[https://docs.ansible.com/ansible/latest/playbooks\_loops.html#looping-over-parallel-sets-of-data](https://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-parallel-sets-of-data)

> I've tried several strategies, including:  
> - name: Building partial name of installed MTX packages  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;installed\_mtx\_packages\_partialname:  
> "{{ installed\_mtx\_packages\_name | map('regex\_replace', '(.\*)',  
> '\\1-item') | list }}" with\_items: installed\_mtx\_packages\_version
> 
> But of course the item is considered as a string here instead of a  
> variable.
> 
> If I move the ' just before item, I get the error:  
> "template error while templating string: expected token ',', got  
> 'string' "

Not 100% sure what you mean by "just before item", but I'm assuming  
you'd want (e.g. note the "'\\1-' + item" part - you can concatenate  
strings this way):

"{{ installed\_mtx\_packages\_name | map('regex\_replace', '(.\*)', '\\1-' + item) | list }}"

Additionally, you should switch to using (I think without moustaches  
this would iterate over an item literal  
"installed\_mtx\_packages\_version" otherwise):

with\_items: "{{ installed\_mtx\_packages\_version }}"

To top it off - I'm pretty sure this particular loop would not produce  
the list you desire.

Best regards

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [September 28, 2017, 10:14am UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612/3 "2017-09-28T10:14:58Z")

</div>

@branko

- you’re right about the typo.
- your “‘\1-’ + item” solution works beautifully 🙂

The solution hence is:

- name: Building partial name of installed MTX packages  
set\_fact:  
installed\_mtx\_packages\_partial\_name: “{{ installed\_mtx\_packages\_name | map(‘regex\_replace’, ‘(.\*)’, ‘\1-’ + item) | list }}”  
with\_items: “{{ installed\_mtx\_packages\_version }}”

The result is:

“installed\_mtx\_packages\_partial\_name”: [  
“mtx-device-1.0.1-r1705191346”,  
“mtx-grpc-agent-1.0.1-r1705191346”,  
“mtx-infra-1.0.1-r1705191346”,  
…  
]

Thanks!

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [September 29, 2017, 1:00pm UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612/4 "2017-09-29T13:00:54Z")

</div>

@branko

Sorry for the false positive, **actually your solution does not work** : it loops over all versions as expected, but it puts each item at the end of each name for each iteration, so the final list is (as shown in previous post):

“installed\_mtx\_packages\_partial\_name”: [  
“mtx-device-1.0.1-r1705191346”,  
“mtx-grpc-agent-1.0.1-r1705191346”,  
“mtx-infra-1.0.1-r1705191346”,

…

]

As you can see (and I missed it the first time), all the elements have the same version, instead of its own.

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [September 29, 2017, 1:24pm UTC](https://forum.ansible.com/t/combining-2-lists-of-strings-with-a-separator/25612/5 "2017-09-29T13:24:55Z")

</div>

The true solution is tricky:

- name: Building partial name of installed MTX packages  
set\_fact:  
installed\_mtx\_packages\_partial\_name\_item: “{{ item.0 }}-{{ item.1 }}”  
with\_together:

- “{{ installed\_mtx\_packages\_name }}”

- “{{ installed\_mtx\_packages\_version }}”  
register: installed\_mtx\_packages\_partial\_name\_result

- name: Building partial list of name of installed MTX packages  
set\_fact:  
installed\_mtx\_packages\_partial\_name: “{{ installed\_mtx\_packages\_partial\_name\_result.results | map(attribute=‘ansible\_facts.installed\_mtx\_packages\_partial\_name\_item’) | list }}”

leads to the true list:

“installed\_mtx\_packages\_partial\_name”: [  
“mtx-infra-1.0.0-r1705191346”,  
“mtx-device-1.0.0-r1705191346”,  
“mtx-grpc-agent-1.0.1-r1705191346”,

…

]

There might be a more elegant solution.
