2015-01-09 15:37:31 +00:00
|
|
|
# (c) 2014, Kent R. Spillner <kspillner@acm.org>
|
2017-09-19 14:49:07 +00:00
|
|
|
# (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2017-09-19 14:49:07 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
lookup: dict
|
|
|
|
version_added: "1.5"
|
2017-10-24 14:57:00 +00:00
|
|
|
short_description: returns key/value pair items from dictionaries
|
2017-09-19 14:49:07 +00:00
|
|
|
description:
|
2017-10-24 14:57:00 +00:00
|
|
|
- Takes dictionaries as input and returns a list with each item in the list being a dictionary with 'key' and 'value' as
|
2017-09-19 14:49:07 +00:00
|
|
|
keys to the previous dictionary's structure.
|
|
|
|
options:
|
2017-10-24 14:57:00 +00:00
|
|
|
_terms:
|
2017-09-19 14:49:07 +00:00
|
|
|
description:
|
2017-10-24 14:57:00 +00:00
|
|
|
- A list of dictionaries
|
2017-09-19 14:49:07 +00:00
|
|
|
required: True
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
vars:
|
|
|
|
users:
|
|
|
|
alice:
|
|
|
|
name: Alice Appleworth
|
|
|
|
telephone: 123-456-7890
|
|
|
|
bob:
|
|
|
|
name: Bob Bananarama
|
|
|
|
telephone: 987-654-3210
|
|
|
|
tasks:
|
2018-05-23 12:49:30 +00:00
|
|
|
# with predefined vars
|
2017-09-19 14:49:07 +00:00
|
|
|
- name: Print phone records
|
|
|
|
debug:
|
|
|
|
msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
|
2017-10-24 14:57:00 +00:00
|
|
|
loop: "{{ lookup('dict', users) }}"
|
2018-05-23 12:49:30 +00:00
|
|
|
# with inline dictionary
|
|
|
|
- name: show dictionary
|
|
|
|
debug:
|
|
|
|
msg: "{{item.key}}: {{item.value}}"
|
|
|
|
with_dict: {a: 1, b: 2, c: 3}
|
2018-07-19 03:48:08 +00:00
|
|
|
# Items from loop can be used in when: statements
|
|
|
|
- name: set_fact when alice in key
|
|
|
|
set_fact:
|
|
|
|
alice_exists: true
|
|
|
|
loop: "{{ lookup('dict', users) }}"
|
|
|
|
when: "'alice' in item.key"
|
2017-09-19 14:49:07 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_list:
|
|
|
|
description:
|
|
|
|
- list of composed dictonaries with key and value
|
|
|
|
type: list
|
|
|
|
"""
|
2015-08-18 00:11:24 +00:00
|
|
|
|
2015-04-14 21:42:57 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2015-01-09 15:37:31 +00:00
|
|
|
from ansible.plugins.lookup import LookupBase
|
2018-10-05 08:22:25 +00:00
|
|
|
from ansible.module_utils.common._collections_compat import Mapping
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
2015-01-09 15:37:31 +00:00
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
2017-04-28 08:38:22 +00:00
|
|
|
def run(self, terms, variables=None, **kwargs):
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2017-10-24 14:57:00 +00:00
|
|
|
# FIXME: can remove once with_ special case is removed
|
|
|
|
if not isinstance(terms, list):
|
|
|
|
terms = [terms]
|
2015-01-09 15:37:31 +00:00
|
|
|
|
2017-10-24 14:57:00 +00:00
|
|
|
results = []
|
|
|
|
for term in terms:
|
|
|
|
# Expect any type of Mapping, notably hostvars
|
2018-10-05 08:22:25 +00:00
|
|
|
if not isinstance(term, Mapping):
|
2017-10-24 14:57:00 +00:00
|
|
|
raise AnsibleError("with_dict expects a dict")
|
|
|
|
|
|
|
|
results.extend(self._flatten_hash_to_list(term))
|
|
|
|
return results
|