ansible.builtin.zip_longest filter – combine list elements, with filler

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 zip_longest even without specifying the collections: keyword. However, we recommend you use the FQCN for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

New in Ansible 2.3

Synopsis

  • Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

Input

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

Parameter

Comments

Input

list / elements=any / required

Original 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.zip_longest(positional1, positional2, ...).

Parameter

Comments

_additional_lists

list / elements=any / required

Additional list(s).

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | ansible.builtin.zip_longest(key1=value1, key2=value2, ...).

Parameter

Comments

fillvalue

any

Filler value to add to output when one of the lists does not contain enough elements to match the others.

Notes

Note

  • This is mostly a passhtrough to Python’s itertools.zip_longest function

Examples

# X_fill => [[1, "a", 21], [2, "b", 22], [3, "c", 23], ["X", "d", "X"], ["X", "e", "X"], ["X", "f", "X"]]
X_fill: "{{ [1,2,3] | zip_longest(['a','b','c','d','e','f'], [21, 22, 23], fillvalue='X') }}"

Return Value

Key

Description

Return value

list / elements=list

List of lists made of elements matching the positions of 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.