ansible.builtin.items2dict filter – Consolidate a list of itemized dictionaries into a dictionary

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 items2dict. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.items2dict 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.7

Synopsis

  • Takes a list of dicts with each having a key and value keys, and transforms the list into a dictionary, effectively as the reverse of dict2items.

Input

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

Parameter

Comments

Input

list / elements=dictionary / required

A list of dictionaries.

Every dictionary must have keys key and value.

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

Parameter

Comments

key_name

string

The name of the key in the element dictionaries that holds the key to use at destination.

Default: "key"

value_name

string

The name of the key in the element dictionaries that holds the value to use at destination.

Default: "value"

See Also

See also

ansible.builtin.dict2items filter plugin

Convert a dictionary into an itemized list of dictionaries.

Examples

# mydict =>  { "hi": "bye", "ciao": "ciao" }
mydict: {{ [{'key': 'hi', 'value': 'bye'}, {'key': 'ciao', 'value': 'ciao'} ]| items2dict}}

# The output is a dictionary with two key/value pairs:
#     Application: payment
#     Environment: dev
vars:
  tags:
    - key: Application
      value: payment
    - key: Environment
      value: dev
  consolidated: "{{ tags | items2dict }}"

Return Value

Key

Description

Return value

dictionary

Dictionary with the consolidated key/values.

Returned: success

Authors

  • Ansible core team

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.