mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add dict filter (#2171)
* Add dict and list_to_dict filters. * Remove list_to_dict filter. * Add myself as maintainer.
This commit is contained in:
parent
4b6722d938
commit
b6ae47c455
5 changed files with 38 additions and 0 deletions
2
.github/BOTMETA.yml
vendored
2
.github/BOTMETA.yml
vendored
|
@ -53,6 +53,8 @@ files:
|
||||||
$doc_fragments/xenserver.py:
|
$doc_fragments/xenserver.py:
|
||||||
maintainers: bvitnik
|
maintainers: bvitnik
|
||||||
labels: xenserver
|
labels: xenserver
|
||||||
|
$filters/dict.py:
|
||||||
|
maintainers: felixfontein
|
||||||
$filters/dict_kv.py:
|
$filters/dict_kv.py:
|
||||||
maintainers: giner
|
maintainers: giner
|
||||||
$filters/jc.py:
|
$filters/jc.py:
|
||||||
|
|
3
changelogs/fragments/dict-filter.yml
Normal file
3
changelogs/fragments/dict-filter.yml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
add plugin.filter:
|
||||||
|
- name: dict
|
||||||
|
description: "The ``dict`` function as a filter: converts a list of tuples to a dictionary"
|
24
plugins/filter/dict.py
Normal file
24
plugins/filter/dict.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# -*- 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
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
2
tests/integration/targets/filter_dict/aliases
Normal file
2
tests/integration/targets/filter_dict/aliases
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
shippable/posix/group3
|
||||||
|
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller
|
7
tests/integration/targets/filter_dict/tasks/main.yml
Normal file
7
tests/integration/targets/filter_dict/tasks/main.yml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
- name: "Test dict filter"
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "[['a', 'b']] | community.general.dict == dict([['a', 'b']])"
|
||||||
|
- "[['a', 'b'], [1, 2]] | community.general.dict == dict([['a', 'b'], [1, 2]])"
|
||||||
|
- "[] | community.general.dict == dict([])"
|
Loading…
Reference in a new issue