AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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 Nested Loops: Optimize List Processing with loop_control

By Luca Berton · Published 2024-01-01 · Category: modules

How to optimize nested list processing in Ansible playbooks. Use subelements, loop_control, and flattened loops for efficient iteration.

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: Flatten the structure into a single list. Remove empty elements. 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

Dynamic Inventories: Combine nested inventory groups into a single, manageable list. Configuration Management: Normalize and optimize data structures before applying configurations. 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. 🚀

See also: Celebrate SysAdmin Day 2024 with Ansible Pilot Automation Expertise

Related Articles

organizing hosts with Ansible inventorycapturing output with Ansible command

Category: modules

Browse all Ansible tutorials · AnsiblePilot Home