Introduction

In the realm of Ansible automation, managing complex data structures is often required. Two powerful tools, the product and combine filters, play distinct roles in manipulating lists and dictionaries, respectively. Understanding their use cases can significantly enhance your playbook efficiency.

product: Cartesian Product of Lists

The product filter in Ansible generates the Cartesian product of two or more lists, producing all possible combinations of their elements. This is particularly useful when you need to iterate over multiple sets of values to configure systems or environments.

Example: Configuring Hosts Across Environments

- hosts: localhost
  gather_facts: no
  tasks:
    - name: Generate combinations of environments and roles
      ansible.builtin.debug:
        msg: "{{ ['dev', 'test', 'prod'] | product(['web', 'db']) | list }}"

Output:

[
  ["dev", "web"], ["dev", "db"],
  ["test", "web"], ["test", "db"],
  ["prod", "web"], ["prod", "db"]
]

In this example, the product filter creates pairs of environments and roles, enabling scalable and dynamic configuration.

Use Case:

  • Generating matrix-style combinations for testing or deployments.
  • Configuring multiple environments in one go.

combine: Merging Dictionaries

The combine filter allows you to merge multiple dictionaries into one, providing a way to consolidate configurations or override existing values.

Example: Merging Configuration Maps

- hosts: localhost
  gather_facts: no
  tasks:
    - name: Merge default and custom configurations
      ansible.builtin.debug:
        msg: "{{ {'default_key': 'default_value'} | combine({'custom_key': 'custom_value'}) }}"

Output:

{
  "default_key": "default_value",
  "custom_key": "custom_value"
}

You can use the combine filter to dynamically construct configurations or override default values with user-provided data.

Use Case:

  • Building dynamic variables from defaults and user inputs.
  • Consolidating configurations for modular roles or playbooks.

Combining product and combine for Advanced Scenarios

Sometimes, these filters can be used together to manage complex scenarios, such as creating a dictionary of all possible role-environment pairs.

Example:

- hosts: localhost
  gather_facts: no
  tasks:
    - name: Create a dictionary of environment-role pairs
      ansible.builtin.debug:
        msg: >-
          {{
            dict(
              ['dev', 'test', 'prod'] | product(['web', 'db']) |
              map('join', '_') |
              map('extract', 0, ['web', 'db'])
            )
          }}          

This advanced usage combines Cartesian products and dictionary manipulations to build intricate data structures for automation workflows.

Conclusion

Understanding the nuances of the product and combine filters empowers you to handle lists and dictionaries effectively in Ansible. Whether you’re iterating over combinations or merging configurations, these tools are essential for robust and flexible automation playbooks.

For more detailed examples and real-world use cases, explore additional Ansible resources or check out my book Ansible By Examples.

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

Academy

Learn Ansible automation 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 Administrators and DevOps

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

Support the Ansible project and keep it thriving!

Patreon Buy me a Pizza