Optimizing Nested Lists in Ansible

Ansible, a powerful IT automation tool, often deals with complex data structures. Nested lists are a common challenge, requiring flattening and optimization for effective use. In this article, we explore how to handle and optimize nested lists using Ansible’s powerful filters.


The Challenge: Nested List Structures

Imagine this input structure:

input_data:
  list: 
    [
      [
        { name: "foo" },
        { name: "bar" }
      ],
      [
        { name: "baz" },
        { name: "qux" }
      ],
      [],
      [
        { name: "quux" }
      ]
    ]

This structure contains nested lists, including empty elements. For streamlined automation, we need to:

  1. Flatten the structure into a single list.
  2. Remove empty elements.
  3. Optionally remove duplicates.

Solution: Flattening and Optimizing Lists

Ansible’s flatten filter is a simple and effective tool for flattening nested lists. Let’s dive into an example playbook:

Playbook Example

---
- name: Optimize Nested Lists
  hosts: localhost
  vars:
    input_data:
      list: 
        [
          [
            { name: "foo" },
            { name: "bar" }
          ],
          [
            { name: "baz" },
            { name: "qux" }
          ],
          [],
          [
            { name: "quux" }
          ]
        ]
  tasks:
    - name: Flatten the nested list
      set_fact:
        optimized_list: "{{ input_data.list | flatten }}"

    - name: Display the optimized list
      debug:
        msg: "Optimized List: {{ optimized_list }}"

Resulting Output

After running the playbook, the optimized_list will look like this:

[
  { name: "foo" },
  { name: "bar" },
  { name: "baz" },
  { name: "qux" },
  { name: "quux" }
]

Removing Duplicates

To remove duplicates from the flattened list, use the unique filter:

optimized_list: "{{ (input_data.list | flatten) | unique }}"

This ensures the list contains only unique elements.


Real-World Use Cases

  1. Dynamic Inventories: Combine nested inventory groups into a single, manageable list.
  2. Configuration Management: Normalize and optimize data structures before applying configurations.
  3. Data Processing: Clean up API responses or aggregated data for streamlined automation workflows.

Conclusion

With Ansible’s flatten and unique filters, handling nested lists is straightforward. These tools ensure your playbooks are efficient and maintainable, enabling seamless automation.

Master these techniques to simplify data structures and enhance your automation workflows. 🚀

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

Academy

Discover the power of Ansible automation with real-world examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

Explore my book Ansible By Examples: 200+ Automation Examples for Linux and Windows System Administrators and DevOps:

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

Support this project and keep learning resources alive: Patreon Buy me a Pizza