2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2013, Serge van Ginderachter <serge@vanginderachter.be>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2021-01-12 07:12:03 +01:00
|
|
|
name: flattened
|
2020-09-28 21:21:51 +02:00
|
|
|
author: Serge van Ginderachter (!UNKNOWN) <serge@vanginderachter.be>
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: return single list completely flattened
|
|
|
|
description:
|
2022-11-01 21:58:46 +01:00
|
|
|
- Given one or more lists, this lookup will flatten any list elements found recursively until only 1 list is left.
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
_terms:
|
|
|
|
description: lists to flatten
|
2022-11-01 21:58:46 +01:00
|
|
|
type: list
|
|
|
|
elements: raw
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2023-06-15 19:04:07 +02:00
|
|
|
- Unlike the P(ansible.builtin.items#lookup) lookup which only flattens 1 level,
|
2022-11-01 21:58:46 +01:00
|
|
|
this plugin will continue to flatten until it cannot find lists anymore.
|
|
|
|
- Aka highlander plugin, there can only be one (list).
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: "'unnest' all elements into single list"
|
2020-08-08 22:04:34 +02:00
|
|
|
ansible.builtin.debug:
|
2022-01-09 11:11:56 +01:00
|
|
|
msg: "all in one list {{lookup('community.general.flattened', [1,2,3,[5,6]], ['a','b','c'], [[5,6,1,3], [34,'a','b','c']])}}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_raw:
|
|
|
|
description:
|
|
|
|
- flattened list
|
|
|
|
type: list
|
|
|
|
"""
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.module_utils.six import string_types
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from ansible.utils.listify import listify_lookup_plugin_terms
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
|
|
|
def _check_list_of_one_list(self, term):
|
|
|
|
# make sure term is not a list of one (list of one..) item
|
|
|
|
# return the final non list item if so
|
|
|
|
|
|
|
|
if isinstance(term, list) and len(term) == 1:
|
|
|
|
term = term[0]
|
|
|
|
if isinstance(term, list):
|
|
|
|
term = self._check_list_of_one_list(term)
|
|
|
|
|
|
|
|
return term
|
|
|
|
|
|
|
|
def _do_flatten(self, terms, variables):
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for term in terms:
|
|
|
|
term = self._check_list_of_one_list(term)
|
|
|
|
|
|
|
|
if term == 'None' or term == 'null':
|
|
|
|
# ignore undefined items
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(term, string_types):
|
|
|
|
# convert a variable to a list
|
2023-02-25 15:29:27 +01:00
|
|
|
try:
|
|
|
|
term2 = listify_lookup_plugin_terms(term, templar=self._templar)
|
|
|
|
except TypeError:
|
|
|
|
# The loader argument is deprecated in ansible-core 2.14+. Fall back to
|
|
|
|
# pre-2.14 behavior for older ansible-core versions.
|
|
|
|
term2 = listify_lookup_plugin_terms(term, templar=self._templar, loader=self._loader)
|
2020-03-09 10:11:07 +01:00
|
|
|
# but avoid converting a plain string to a list of one string
|
|
|
|
if term2 != [term]:
|
|
|
|
term = term2
|
|
|
|
|
|
|
|
if isinstance(term, list):
|
|
|
|
# if it's a list, check recursively for items that are a list
|
|
|
|
term = self._do_flatten(term, variables)
|
|
|
|
ret.extend(term)
|
|
|
|
else:
|
|
|
|
ret.append(term)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2022-11-01 21:58:46 +01:00
|
|
|
def run(self, terms, variables=None, **kwargs):
|
2020-03-09 10:11:07 +01:00
|
|
|
if not isinstance(terms, list):
|
|
|
|
raise AnsibleError("with_flattened expects a list")
|
|
|
|
|
2022-11-01 21:58:46 +01:00
|
|
|
self.set_options(var_options=variables, direct=kwargs)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
return self._do_flatten(terms, variables)
|