-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
dict_kv.py
104 lines (87 loc) · 2.47 KB
/
dict_kv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Stanislav German-Evtushenko (@giner) <[email protected]>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
name: dict_kv
short_description: Convert a value to a dictionary with a single key-value pair
version_added: 1.3.0
author: Stanislav German-Evtushenko (@giner)
description:
- Convert a value to a dictionary with a single key-value pair.
positional: key
options:
_input:
description: The value for the single key-value pair.
type: any
required: true
key:
description: The key for the single key-value pair.
type: any
required: true
'''
EXAMPLES = '''
- name: Create a one-element dictionary from a value
ansible.builtin.debug:
msg: "{{ 'myvalue' | dict_kv('mykey') }}"
# Produces the dictionary {'mykey': 'myvalue'}
'''
RETURN = '''
_value:
description: A dictionary with a single key-value pair.
type: dictionary
'''
def dict_kv(value, key):
'''Return a dictionary with a single key-value pair
Example:
- hosts: localhost
gather_facts: false
vars:
myvar: myvalue
tasks:
- debug:
msg: "{{ myvar | dict_kv('thatsmyvar') }}"
produces:
ok: [localhost] => {
"msg": {
"thatsmyvar": "myvalue"
}
}
Example 2:
- hosts: localhost
gather_facts: false
vars:
common_config:
type: host
database: all
myservers:
- server1
- server2
tasks:
- debug:
msg: "{{ myservers | map('dict_kv', 'server') | map('combine', common_config) }}"
produces:
ok: [localhost] => {
"msg": [
{
"database": "all",
"server": "server1",
"type": "host"
},
{
"database": "all",
"server": "server2",
"type": "host"
}
]
}
'''
return {key: value}
class FilterModule(object):
''' Query filter '''
def filters(self):
return {
'dict_kv': dict_kv
}