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: localhost declaration specifies that the tasks defined in this playbook will be executed on the localhost.

  • Line 3: gather_facts: false disables fact gathering. Facts are variables containing information about the target hosts. By setting this to false, we prevent Ansible from collecting facts about the localhost.

  • Line 48: The vars section defines a variable called my_list_of_strings, which is a list containing three string elements: "Hello" "World" and "Ansible."

  • Line 9: The tasks section contains a list of tasks to be executed on the target host (localhost).

  • Line 10: - name: Join the list of strings is the name of the task, which is used for informational purposes when the playbook runs.

  • Line 11: The debug module is used to display debugging output. It allows you to print variables or messages during playbook execution.

  • Line 12: The msg attribute of the debug module specifies the message to be displayed. Here, we use the {{ my_list_of_strings | join(', ') }} to join the elements of the my_list_of_strings variable using a comma and space as the separator (', '). The resulting joined string is printed as the output of the debug task.

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 filter is the name of the task, indicating what the task does.

  • Line 11: The debug module is used again to display debugging output. It allows you to print variables or messages during playbook execution.

  • Line 12: The msg attribute of the debug module 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_strings variable is processed through two filters, map and join.

    • The map filter is applied to the my_list_of_strings variable with the regex_replace filter as its argument. The regex_replace filter 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 map with regex_replace, we have a new list where each element is the same as the original list.

    • Finally, the join filter 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

Copyright ©2024 Educative, Inc. All rights reserved