Combine lists of dicts (#32686)

* allow combine to take lists of dicts

* updated as per feedback
This commit is contained in:
Brian Coca 2017-11-09 10:02:38 -05:00 committed by GitHub
parent 9c629f8a1c
commit ee80215fa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -322,14 +322,19 @@ def combine(*terms, **kwargs):
if len(kwargs) > 1 or (len(kwargs) == 1 and 'recursive' not in kwargs):
raise errors.AnsibleFilterError("'recursive' is the only valid keyword argument")
dicts = []
for t in terms:
if not isinstance(t, dict):
if isinstance(t, MutableMapping):
dicts.append(t)
elif isinstance(t, list):
dicts.append(combine(*t, **kwargs))
else:
raise errors.AnsibleFilterError("|combine expects dictionaries, got " + repr(t))
if recursive:
return reduce(merge_hash, terms)
return reduce(merge_hash, dicts)
else:
return dict(itertools.chain(*map(iteritems, terms)))
return dict(itertools.chain(*map(iteritems, dicts)))
def comment(text, style='plain', **kw):