From ee80215fa9a750e464826a7990b6a75cd7bf5410 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 9 Nov 2017 10:02:38 -0500 Subject: [PATCH] Combine lists of dicts (#32686) * allow combine to take lists of dicts * updated as per feedback --- lib/ansible/plugins/filter/core.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index e3e01c9d67..731a111fed 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -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):