mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
- Add filter `dict_kv` which returns a single key-value pair dictionary created from two arguments
Example 1
```
- hosts: localhost
gather_facts: false
vars:
myvar: myvalue
tasks:
- debug:
msg: "{{ myvar | dict_kv('thatsmyvar') }}"
OUTPUT:
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) }}"
OUTPUT:
ok: [localhost] => {
"msg": [
{
"database": "all",
"server": "server1",
"type": "host"
},
{
"database": "all",
"server": "server2",
"type": "host"
}
]
}
```
(cherry picked from commit 44fd157a2b
)
Co-authored-by: Stanislav German-Evtushenko <ginermail@gmail.com>
This commit is contained in:
parent
b86161886f
commit
659ef811a3
4 changed files with 84 additions and 0 deletions
2
changelogs/fragments/1264-dict_kv-new-filter.yaml
Normal file
2
changelogs/fragments/1264-dict_kv-new-filter.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- "Add new filter plugin ``dict_kv`` which returns a single key-value pair from two arguments. Useful for generating complex dictionaries without using loops. For example ``'value' | community.general.dict_kv('key'))`` evaluates to ``{'key': 'value'}`` (https://github.com/ansible-collections/community.general/pull/1264)."
|
70
plugins/filter/dict_kv.py
Normal file
70
plugins/filter/dict_kv.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
# Copyright (C) 2020 Stanislav German-Evtushenko (@giner) <ginermail@gmail.com>
|
||||
# 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_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
|
||||
}
|
2
tests/integration/targets/filter_dict_kv/aliases
Normal file
2
tests/integration/targets/filter_dict_kv/aliases
Normal file
|
@ -0,0 +1,2 @@
|
|||
shippable/posix/group2
|
||||
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller
|
10
tests/integration/targets/filter_dict_kv/tasks/main.yml
Normal file
10
tests/integration/targets/filter_dict_kv/tasks/main.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
####################################################################
|
||||
# WARNING: These are designed specifically for Ansible tests #
|
||||
# and should not be used as examples of how to write Ansible roles #
|
||||
####################################################################
|
||||
|
||||
- name: test dict_kv filter
|
||||
assert:
|
||||
that:
|
||||
- "('value' | community.general.dict_kv('key')) == {'key': 'value'}"
|
Loading…
Reference in a new issue