Ansible: Add a prefix or suffix to all items of a list
Recently, in one of my Ansible playbooks I had to prefix all items of a list with a chosen string.
Namely, from the following list:
[ "bar", "bat", "baz" ]
I want to have:
[ "foobar", "foobat", "foobaz" ]
The recipe I used to add a prefix to all items of a list is:
- debug:
var: result
vars:
prefix: foo
a_list: [ "bar", "bat", "baz" ]
result: "{{ [prefix] | product(a_list) | map('join') | list }}"
If you need to add a suffix to all items of a list instead, you can use:
- debug:
var: result
vars:
suffix: foo
a_list: [ "bar", "bat", "baz" ]
result: "{{ a_list | product([suffix]) | map('join') | list }}"