Skip to content

extract

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 2.1.

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 C(list[index]) and C(dictionary[key]) but useful as a filter to combine with C(map).

Parameters

Parameter Defaults / Choices Comments
_input
raw
required
Index or key to extract.
container
raw
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 Values

Key Data Type Description Returned
_value dict Resulting merge of supplied dictionaries.