How to join a list of strings in Ansible
Ansible is an open-source automation tool that simplifies IT tasks through configuration management, application deployment, and task automation. It uses a declarative approach, which means you specify the desired state of the system, and Ansible takes care of applying the necessary changes to achieve that state.
In Ansible, you may encounter scenarios where you have a list of strings and need to combine them into a single string. This can be useful when:
Creating a filename or path from a list of components.
Generating a list of values to be passed to a command.
Creating a message that contains the results of a task.
There are a few different ways to join a list of strings:
Using the join filter
The first method involves using the join filter, which takes a separator as an argument and concatenates the elements of the list into a single string separated by that separator. The join filter is straightforward and ideal when you want a simple concatenation without any additional modifications to the elements.
Here's how you can do it:
---
- hosts: localhost
gather_facts: false
vars:
my_list_of_strings:
- "Hello"
- "World"
- "Ansible"
tasks:
- name: Join the list of strings
debug:
msg: "{{ my_list_of_strings | join(', ') }}"
Code explanation
Line 1: The playbook starts with
---, which is the YAML document marker indicating the beginning of a YAML document.Line 2: The
- hosts: localhostdeclaration specifies that the tasks defined in this playbook will be executed on thelocalhost.Line 3:
gather_facts: falsedisables fact gathering. Facts are variables containing information about the target hosts. By setting this tofalse, we prevent Ansible from collecting facts about the localhost.Line 4–8: The
varssection defines a variable calledmy_list_of_strings, which is a list containing three string elements:"Hello" "World"and"Ansible."Line 9: The
taskssection contains a list of tasks to be executed on the target host (localhost).Line 10:
- name: Join the list of stringsis the name of the task, which is used for informational purposes when the playbook runs.Line 11: The
debugmodule is used to display debugging output. It allows you to print variables or messages during playbook execution.Line 12: The
msgattribute of thedebugmodule specifies the message to be displayed. Here, we use the{{ my_list_of_strings | join(', ') }}to join the elements of themy_list_of_stringsvariable using a comma and space as the separator (', '). The resulting joined string is printed as the output of thedebugtask.
Using the map filter with regex_replace
The second method involves using the map filter along with the regex_replace filter, offering more flexibility for element manipulation before joining. By applying the regex_replace filter, you can modify each element in the list before the concatenation. This method is useful when you need to apply some transformation to the elements or ensure consistent formatting before joining them.
Here's how you can do it:
---
- hosts: localhost
gather_facts: false
vars:
my_list_of_strings:
- "Hello"
- "World"
- "Ansible"
tasks:
- name: Join the list of strings using a map filter
debug:
msg: "{{ my_list_of_strings | map('regex_replace', '^(.*)$', '\\1') | join(', ') }}"
Code explanation
Line 1–9: These lines are the same as the previous example, setting up the playbook structure, host, variables, and task.
Line 10:
- name: Join the list of strings using a map filteris the name of the task, indicating what the task does.Line 11: The
debugmodule is used again to display debugging output. It allows you to print variables or messages during playbook execution.Line 12: The
msgattribute of thedebugmodule specifies the message to be displayed. Here, we use the{{ my_list_of_strings | map('regex_replace', '^(.*)$', '\\1') | join(', ') }}.In this template, the
my_list_of_stringsvariable is processed through two filters,mapandjoin.The
mapfilter is applied to themy_list_of_stringsvariable with theregex_replacefilter as its argument. Theregex_replacefilter takes three arguments: the regular expression pattern'^(.*)$', the replacement pattern'\\1', and the input variable to be transformed. In this case, the regular expression'^(.*)$'matches the whole string and captures it in a group. The replacement pattern'\\1'puts the captured group back as it is, effectively doing nothing and preserving each element unmodified.After using
mapwithregex_replace, we have a new list where each element is the same as the original list.Finally, the
joinfilter is applied to this modified list, using a comma and space', 'as the separator. This concatenates the elements of the list into a single string with commas and spaces between them
Free Resources