ansible.builtin.flatten filter – flatten lists within a list

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 flatten. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.flatten for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • For a given list, take any elements that are lists and insert their elements into the parent list directly.

Input

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

Parameter

Comments

Input

dictionary / required

First dictionary to combine.

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

Parameter

Comments

levels

integer

Number of recursive list depths to flatten.

skip_nulls

boolean

Skip null/None elements when inserting into the top list.

Choices:

  • false

  • true ← (default)

Examples

# [1,2,3,4,5,6]
flat: "{{ [1 , 2, [3, [4, 5]], 6] | flatten }}"

# [1,2,3,[4,5],6]
flatone: "{{ [1, 2, [3, [4, 5]], 6] | flatten(1) }}"

Return Value

Key

Description

Return value

list / elements=string

The flattened list.

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.