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 = """
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