mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
Add the key_name/value_name options to the dict2items filter (#45550)
* Add the key_name/value_name options to dict2items - as with items2dict, allow users to configure the key/value name for dict2items, add "version added" and examples
This commit is contained in:
parent
7092dd7fbf
commit
c41632fad2
2 changed files with 21 additions and 2 deletions
|
@ -200,6 +200,25 @@ into::
|
||||||
- key: Environment
|
- key: Environment
|
||||||
value: dev
|
value: dev
|
||||||
|
|
||||||
|
.. versionadded:: 2.8
|
||||||
|
|
||||||
|
``dict2items`` accepts 2 keyword arguments, ``key_name`` and ``value_name`` that allow configuration of the names of the keys to use for the transformation::
|
||||||
|
|
||||||
|
{{ files | dict2items(key_name='file', value_name='path') }}
|
||||||
|
|
||||||
|
Which turns::
|
||||||
|
|
||||||
|
files:
|
||||||
|
users: /etc/passwd
|
||||||
|
groups: /etc/group
|
||||||
|
|
||||||
|
into::
|
||||||
|
|
||||||
|
- file: users
|
||||||
|
path: /etc/passwd
|
||||||
|
- file: groups
|
||||||
|
path: /etc/group
|
||||||
|
|
||||||
items2dict filter
|
items2dict filter
|
||||||
`````````````````
|
`````````````````
|
||||||
|
|
||||||
|
|
|
@ -494,7 +494,7 @@ def subelements(obj, subelements, skip_missing=False):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def dict_to_list_of_dict_key_value_elements(mydict):
|
def dict_to_list_of_dict_key_value_elements(mydict, key_name='key', value_name='value'):
|
||||||
''' takes a dictionary and transforms it into a list of dictionaries,
|
''' takes a dictionary and transforms it into a list of dictionaries,
|
||||||
with each having a 'key' and 'value' keys that correspond to the keys and values of the original '''
|
with each having a 'key' and 'value' keys that correspond to the keys and values of the original '''
|
||||||
|
|
||||||
|
@ -503,7 +503,7 @@ def dict_to_list_of_dict_key_value_elements(mydict):
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for key in mydict:
|
for key in mydict:
|
||||||
ret.append({'key': key, 'value': mydict[key]})
|
ret.append({key_name: key, value_name: mydict[key]})
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue