community.general.dict (8.5.0) — filter

Convert a list of tuples into a dictionary

| "added in version" 3.0.0 of community.general"

Authors: Felix Fontein (@felixfontein)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

Convert a list of tuples into a dictionary. This is a filter version of the C(dict) function.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Convert list of tuples into dictionary
  ansible.builtin.set_fact:
    dictionary: "{{ [[1, 2], ['a', 'b']] | community.general.dict }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
    # Result is {1: 2, 'a': 'b'}

- name: Create a list of dictionaries with map and the community.general.dict filter
  ansible.builtin.debug:
    msg: >-
      {{ values | map('zip', ['k1', 'k2', 'k3'])
                | map('map', 'reverse')
                | map('community.general.dict') }}
  vars:
    values:
      - - foo
        - 23
        - a
      - - bar
        - 42
        - b

Inputs

    
_input:
    description: A list of tuples (with exactly two elements).
    elements: tuple
    required: true
    type: list

Outputs

_value:
  description: The dictionary having the provided key-value pairs.
  type: boolean