ansible.builtin.product filter – cartesian product of lists

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name product. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.product for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Combines two lists into one with each element being the product of the elements of the input lists.

  • Creates ‘nested loops’. Looping over listA and listB is the same as looping over listA | product(listB).

Input

This describes the input of the filter, the value before | ansible.builtin.product.

Parameter

Comments

Input

list / elements=string / required

First list.

Positional parameters

This describes positional parameters of the filter. These are the values positional1, positional2 and so on in the following example: input | ansible.builtin.product(positional1, positional2, ...)

Parameter

Comments

_additional_lists

list / elements=string

Additional list for the product.

repeat

integer

Number of times to repeat the product against itself.

Default: 1

Notes

Note

  • This is a passthrough to Python’s itertools.product

Examples

# product => [ [ 1, "a" ], [ 1, "b" ], [ 1, "c" ], [ 2, "a" ], [ 2, "b" ], [ 2, "c" ], [ 3, "a" ], [ 3, "b" ], [ 3, "c" ], [ 4, "a" ], [ 4, "b" ], [ 4, "c" ], [ 5, "a" ], [ 5, "b" ], [ 5, "c" ] ]
product:  "{{ [1,2,3,4,5] | product(['a', 'b', 'c']) }}"

# repeat_original => [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
repeat_original: "{{ [1,2] | product(repeat=2) }}"

# repeat_product => [ [ 1, "a", 1, "a" ], [ 1, "a", 1, "b" ], [ 1, "a", 2, "a" ], [ 1, "a", 2, "b" ], [ 1, "b", 1, "a" ], [ 1, "b", 1, "b" ], [ 1, "b", 2, "a" ], [ 1, "b", 2, "b" ], [ 2, "a", 1, "a" ], [ 2, "a", 1, "b" ], [ 2, "a", 2, "a" ], [ 2, "a", 2, "b" ], [ 2, "b", 1, "a" ], [ 2, "b", 1, "b" ], [ 2, "b", 2, "a" ], [ 2, "b", 2, "b" ] ]
repeat_product:  "{{ [1,2] | product(['a', 'b'], repeat=2) }}"

# domains => [ 'example.com', 'ansible.com', 'redhat.com' ]
domains: "{{ [ 'example', 'ansible', 'redhat'] | product(['com']) | map('join', '.') }}"

Return Value

Key

Description

Return value

list / elements=list

List of lists of combined elements from the input lists.

Returned: success

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.