From e70c0afc5eb19da351e873e89acca5437950533d Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 24 Oct 2017 10:57:00 -0400 Subject: [PATCH] now with_dict allows for direct 'lookup' usage fixes #32067 --- lib/ansible/plugins/lookup/dict.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/ansible/plugins/lookup/dict.py b/lib/ansible/plugins/lookup/dict.py index 6d9bc327bd..dce1159ae3 100644 --- a/lib/ansible/plugins/lookup/dict.py +++ b/lib/ansible/plugins/lookup/dict.py @@ -7,14 +7,14 @@ __metaclass__ = type DOCUMENTATION = """ lookup: dict version_added: "1.5" - short_description: returns key/value pair items from a dictionary + short_description: returns key/value pair items from dictionaries description: - - Takes a dictionary as input and returns a list with each item in the list being a dictionary with 'key' and 'value' as + - Takes dictionaries as input and returns a list with each item in the list being a dictionary with 'key' and 'value' as keys to the previous dictionary's structure. options: - _raw: + _terms: description: - - A dictionary + - A list of dictionaries required: True """ @@ -37,7 +37,7 @@ tasks: - name: Print phone records debug: msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" - with_dict: "{{ users }}" + loop: "{{ lookup('dict', users) }}" """ RETURN = """ @@ -56,8 +56,15 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - # Expect any type of Mapping, notably hostvars - if not isinstance(terms, collections.Mapping): - raise AnsibleError("with_dict expects a dict") + # FIXME: can remove once with_ special case is removed + if not isinstance(terms, list): + terms = [terms] - return self._flatten_hash_to_list(terms) + results = [] + for term in terms: + # Expect any type of Mapping, notably hostvars + if not isinstance(term, collections.Mapping): + raise AnsibleError("with_dict expects a dict") + + results.extend(self._flatten_hash_to_list(term)) + return results