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]anddictionary[key]but useful as a filter to combine withmap.
Input
This describes the input of the filter, the value before | ansible.builtin.extract.
Parameter |
Comments |
|---|---|
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 |
|---|---|
Dictionary or list from which to extract a value. |
|
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 |
|---|---|
Resulting merge of supplied dictionaries. Returned: success |