Ansible Split String: Filter Guide for CSV, Delimiters & Lists
By Luca Berton · Published 2024-01-01 · Category: modules
How to split strings in Ansible with the split filter. Convert CSV to lists, split by delimiter, extract substrings, and transform data with practical examples.

Introduction
Ansible is a powerful automation tool widely used in IT operations for configuration management, application deployment, and task automation. One of the lesser-known but incredibly useful features of Ansible is the "split" filter. This filter allows you to divide strings into smaller components or split lists into sublists, making it a valuable tool for data transformation and manipulation in your playbooks. In this article, we will explore the split filter in Ansible, its syntax, and various use cases.
See also: Simplify Ansible Output with the community.general.dense Callback Plugin
Understanding the Split Filter
The split filter in Ansible allows you to divide a string or list into smaller elements based on a specified delimiter. You can use it to extract specific information from a string or restructure a list into more manageable parts. The syntax for using the split filter is straightforward:
{{ some_variable | split(delimiter) }}
• some_variable: The variable you want to split.
• delimiter: The character or substring used as a separator to split the variable.
Common Use Cases
1. Splitting Strings:
Let's say you have a string containing comma-separated values and you want to convert it into a list. Here's how you can achieve that using the split filter:
---
- name: Splitting a String
hosts: localhost
vars:
csv_values: "apple,banana,grape"
tasks:
- name: Convert CSV to List
debug:
msg: "{{ csv_values | split(',') }}"
In this example, the split filter takes the csv_values string and divides it into a list using a comma as the delimiter.
2. Extracting Substrings:
Another useful application of the split filter is extracting substrings from a larger string. For instance, if you have a filename with an extension, you can split it to get the base filename and the extension separately:
---
- name: Extracting Substrings
hosts: localhost
vars:
file_name: "document.pdf"
tasks:
- name: Extract Base Name and Extension
set_fact:
base_name: "{{ file_name | split('.') | first }}"
extension: "{{ file_name | split('.') | last }}"
- debug:
msg: "Base Name: {{ base_name }}, Extension: {{ extension }}"
In this example, we use the split filter twice to obtain both the base name and extension of the file.
3. Splitting Lists:
The split filter can also be used to divide a list into smaller sublists. Consider a scenario where you have a list of numbers, and you want to split it into groups of three:
---
- name: Splitting Lists
hosts: localhost
vars:
number_list:
- 1
- 2
- 3
- 4
- 5
- 6
tasks:
- name: Split List into Groups of Three
set_fact:
grouped_numbers: "{{ number_list | batch(3) }}"
- debug:
msg: "{{ grouped_numbers }}"
In this example, we use the batch filter in combination with split to create sublists with three elements each from the original list of numbers.
See also: Simplifying Ansible Output with the community.general.unixy Callback Plugin
Conclusion
The split filter in Ansible is a versatile tool for data transformation and manipulation. Whether you need to split strings, extract substrings, or divide lists, the split filter can help you achieve these tasks efficiently. By understanding its syntax and various use cases, you can make your Ansible playbooks more powerful and flexible, allowing you to automate a wide range of tasks with ease.
Related Articles
• Ansible Split Filter Guide • using ansible.builtin.command effectivelyCategory: modules