AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible split Filter: Split Strings into Lists (Complete Guide)

By Luca Berton · Published 2026-04-03 · Category: troubleshooting

How to use Ansible split filter to break strings into lists. Split by delimiter, regex, newlines, and combine with other Jinja2 filters with examples.

The split filter in Ansible (Jinja2) divides a string into a list of substrings based on a delimiter. It's essential for parsing command output, configuration files, and structured text.

Basic Split

Split with Different Delimiters

Limit Number of Splits

Split Command Output

Combine split with Other Filters

Split and select

Split and join (replace)

Split and filter empty strings

Parse Key-Value Pairs

Split vs regex_findall

For complex patterns, use regex_findall instead:

Practical Examples

Parse /etc/hosts

Parse CSV data

FAQ

What happens if the delimiter isn't found?

The split filter returns a list with the original string as the only element: "hello" | split(",") → ["hello"].

How do I split by whitespace (any amount)?

Use split() without arguments — it splits on any whitespace and removes empty strings: " a b c " | split() → ["a", "b", "c"].

Can I split into a fixed number of parts?

Yes, use the maxsplit parameter: "a-b-c-d" | split("-", 2) → ["a", "b", "c-d"].

Basic Split

Split with Filter Syntax

Split Command Output

Split Multi-Line Text

Parse CSV Data

Simpler approach:

Split with Limit

Related String Operations

| Operation | Syntax | Result | |-----------|--------|--------| | Split | 'a,b,c' | split(',') | ['a','b','c'] | | Join | ['a','b'] | join(',') | 'a,b' | | Replace | 'hello' | replace('l','r') | 'herro' | | Regex split | 'a1b2c' | regex_findall('[a-z]+') | ['a','b','c'] | | First element | 'a/b/c'.split('/') | first | 'a' | | Last element | 'a/b/c'.split('/') | last | 'c' |

Extract from Structured Strings

FAQ

split vs regex_findall?

split divides by a fixed delimiter. regex_findall extracts matches by pattern:

How do I split and pick the Nth element?

Can I split by multiple delimiters?

Use regex: "a,b;c" | regex_findall('[^,;]+')

Basic Split

Split by Different Delimiters

Split with Limit

Split Command Output

Split and Select

Split + Loop

Split + Filter

Split Multiline Output

Parse Key=Value Pairs

Join (Reverse of Split)

Related Filters

| Filter | Purpose | |--------|---------| | split() | String → List | | join() | List → String | | regex_replace | Pattern replacement | | trim | Remove whitespace | | map('split') | Split each item in list | | select | Filter empty items |

FAQ

split vs splitlines?

split('\n') and splitlines() are similar, but splitlines() handles \r\n and other line endings. For command output, use stdout_lines (already split).

How to split by regex?

Use regex_findall instead:

Can I split and get the Nth element in one expression?

Yes: "{{ mystring.split(',')[2] }}" gets the third element.

Related ArticlesAnsible Template GuideAnsible Multiline Strings GuideAnsible set_fact GuideAnsible Loops GuideAnsible Split Filter Guide

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home