ansible.builtin.extract filter – extract a value based on an index or key

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

Synopsis

  • Extract a value from a list or dictionary based on an index/key.

  • User must ensure that index or key used matches the type of container.

  • Equivalent of using list[index] and dictionary[key] but useful as a filter to combine with map.

Input

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

Parameter

Comments

Input

any / required

Index or key to extract.

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

Parameter

Comments

container

any / required

Dictionary or list from which to extract a value.

morekeys

list / elements=dictionary / required

Indices or keys to extract from the initial result (subkeys/subindices).

Examples

# extracted => 'b', same as ['a', 'b', 'c'][1]
extracted: "{{ 1 | extract(['a', 'b', 'c']) }}"

# extracted_key => '2', same as {'a': 1, 'b': 2, 'c': 3}['b']
extracted_key: "{{ 'b' | extract({'a': 1, 'b': 2, 'c': 3}) }}"

# extracted_key_r => '2', same as [{'a': 1, 'b': 2, 'c': 3}, {'x': 9, 'y': 10}][0]['b']
extracted_key_r: "{{ 0 | extract([{'a': 1, 'b': 2, 'c': 3}, {'x': 9, 'y': 10}], morekeys='b') }}"

Return Value

Key

Description

Return value

dictionary

Resulting merge of supplied dictionaries.

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.