1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/filter/dict.py
Felix Fontein 0ba06f8d82
Document all filter and test plugins (#4597) (#4604)
* Fix/improve docs.

* Document the a_module test.

* Document the dict filter.

* Linting.

* Add more filter docs.

* More filters.

* Update BOTMETA.

* Add another plugin.

* Fix typos.

* Add explicit entries.

* Fix lookup documentation.

(cherry picked from commit f055f47161)
2022-05-02 07:49:11 +02:00

78 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
name: dict
short_description: Convert a list of tuples into a dictionary
version_added: 3.0.0
author: Felix Fontein (@felixfontein)
description:
- Convert a list of tuples into a dictionary. This is a filter version of the C(dict) function.
options:
_input:
description: A list of tuples (with exactly two elements).
type: list
elements: tuple
required: true
'''
EXAMPLES = '''
- name: Convert list of tuples into dictionary
ansible.builtin.set_fact:
dictionary: "{{ [[1, 2], ['a', 'b']] | community.general.dict }}"
# 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
# Produces the following list of dictionaries:
# {
# "k1": "foo",
# "k2": 23,
# "k3": "a"
# },
# {
# "k1": "bar",
# "k2": 42,
# "k3": "b"
# }
'''
RETURN = '''
_value:
description: Whether the module or action plugin denoted by the input exists.
type: boolean
'''
def dict_filter(sequence):
'''Convert a list of tuples to a dictionary.
Example: ``[[1, 2], ['a', 'b']] | community.general.dict`` results in ``{1: 2, 'a': 'b'}``
'''
return dict(sequence)
class FilterModule(object):
'''Ansible jinja2 filters'''
def filters(self):
return {
'dict': dict_filter,
}