now with_dict allows for direct 'lookup' usage

fixes #32067
This commit is contained in:
Brian Coca 2017-10-24 10:57:00 -04:00 committed by Brian Coca
parent 77061f5521
commit e70c0afc5e

View file

@ -7,14 +7,14 @@ __metaclass__ = type
DOCUMENTATION = """ DOCUMENTATION = """
lookup: dict lookup: dict
version_added: "1.5" version_added: "1.5"
short_description: returns key/value pair items from a dictionary short_description: returns key/value pair items from dictionaries
description: 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. keys to the previous dictionary's structure.
options: options:
_raw: _terms:
description: description:
- A dictionary - A list of dictionaries
required: True required: True
""" """
@ -37,7 +37,7 @@ tasks:
- name: Print phone records - name: Print phone records
debug: debug:
msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}" loop: "{{ lookup('dict', users) }}"
""" """
RETURN = """ RETURN = """
@ -56,8 +56,15 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs): def run(self, terms, variables=None, **kwargs):
# Expect any type of Mapping, notably hostvars # FIXME: can remove once with_ special case is removed
if not isinstance(terms, collections.Mapping): if not isinstance(terms, list):
raise AnsibleError("with_dict expects a dict") 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