dict2items filter

This commit is contained in:
Brian Coca 2017-12-15 14:52:54 -05:00 committed by Brian Coca
parent 2e852fcd6d
commit 399cba1c84

View file

@ -488,6 +488,19 @@ def flatten(mylist, levels=None):
return ret
def dict_to_list_of_dict_key_value_elements(mydict):
''' 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 '''
if not isinstance(mydict, MutableMapping):
raise AnsibleFilterError("dict2items requires a dictionary, got %s instead." % type(mydict))
ret = []
for key in mydict:
ret.append({'key': key, 'value': mydict[key]})
return ret
class FilterModule(object):
''' Ansible core jinja2 filters '''
@ -574,4 +587,5 @@ class FilterModule(object):
'combine': combine,
'extract': extract,
'flatten': flatten,
'dict2items': dict_to_list_of_dict_key_value_elements,
}