Introduction

When working with Ansible, automating data manipulation is crucial for dynamic and scalable playbooks. A common scenario involves converting a list of dictionaries into a simple list for further processing. In this guide, we’ll explore how to achieve this transformation effectively using Jinja2 filters in Ansible.

Problem Statement

You may encounter a data structure like this:

data_structure:
  - name: a
  - name: b
  - name: c

Your goal is to extract the values of the name key into a simple list:

['a', 'b', 'c']

Let’s explore the solution step by step.

Ansible Solution with Jinja2 Filters

Ansible’s Jinja2 templating engine provides powerful filters like map and list to simplify this task.

Example Playbook

Here’s a playbook to convert the given data structure into a list:

- name: Convert dictionaries to a list in Ansible
  hosts: localhost
  gather_facts: no
  vars:
    data_structure:
      - name: a
      - name: b
      - name: c
  tasks:
    - name: Extract values into a list
      set_fact:
        extracted_list: "{{ data_structure | map(attribute='name') | list }}"

    - name: Debug the extracted list
      debug:
        var: extracted_list

    - name: Use the extracted list in a loop
      debug:
        msg: "Processing item: {{ item }}"
      loop: "{{ extracted_list }}"

Explanation of Filters

  1. map(attribute='name'): Extracts the value of the name key from each dictionary in the list.
  2. | list: Converts the resulting generator into a proper list.

Debugging Output

The debug task will display the extracted list:

ok: [localhost] => {
    "extracted_list": [
        "a",
        "b",
        "c"
    ]
}

Handling Common Errors

Error: “Invalid data passed to loop”

If Ansible complains about invalid data, the issue may lie in how the data is interpreted. Use these additional filters to ensure the data is processed correctly:

- name: Ensure extracted list is properly formatted
  set_fact:
    parsed_list: "{{ extracted_list | to_json | from_json }}"

This serialization-deserialization process guarantees the data is treated as a list.

Conclusion

Transforming data structures in Ansible is straightforward with the right filters. By leveraging Jinja2’s map and list filters, you can easily convert dictionaries into lists for dynamic playbook operations. Debugging tools and careful handling of data ensure smooth execution and accurate results.

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 real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

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

Patreon Buy me a Pizza