Ansible Pilot

Understanding Split in Ansible: A Powerful Tool for Data Transformation

Mastering Ansible's Split Filter

January 11, 2024
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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.

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) }}

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.

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.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases