2014-10-02 17:07:05 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-10-15 23:22:54 +00:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
|
|
|
|
from six import with_metaclass
|
|
|
|
|
2015-07-23 14:24:50 +00:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2015-01-08 16:51:54 +00:00
|
|
|
__all__ = ['LookupBase']
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
class LookupBase(with_metaclass(ABCMeta, object)):
|
2015-08-04 16:10:23 +00:00
|
|
|
def __init__(self, loader=None, templar=None, **kwargs):
|
2015-01-09 15:37:31 +00:00
|
|
|
self._loader = loader
|
2015-08-04 16:10:23 +00:00
|
|
|
self._templar = templar
|
2015-07-23 14:24:50 +00:00
|
|
|
self._display = display
|
2015-01-08 16:51:54 +00:00
|
|
|
|
2015-07-27 14:41:28 +00:00
|
|
|
def get_basedir(self, variables):
|
|
|
|
if 'role_path' in variables:
|
|
|
|
return variables['role_path']
|
|
|
|
else:
|
|
|
|
return self._loader.get_basedir()
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
@staticmethod
|
2015-09-23 06:26:19 +00:00
|
|
|
def _flatten(terms):
|
2015-01-08 16:51:54 +00:00
|
|
|
ret = []
|
|
|
|
for term in terms:
|
|
|
|
if isinstance(term, (list, tuple)):
|
|
|
|
ret.extend(term)
|
|
|
|
else:
|
|
|
|
ret.append(term)
|
|
|
|
return ret
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def _combine(a, b):
|
2015-01-08 16:51:54 +00:00
|
|
|
results = []
|
|
|
|
for x in a:
|
|
|
|
for y in b:
|
|
|
|
results.append(self._flatten([x,y]))
|
|
|
|
return results
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def _flatten_hash_to_list(terms):
|
2015-01-09 15:37:31 +00:00
|
|
|
ret = []
|
|
|
|
for key in terms:
|
|
|
|
ret.append({'key': key, 'value': terms[key]})
|
|
|
|
return ret
|
|
|
|
|
2015-09-22 23:17:35 +00:00
|
|
|
@abstractmethod
|
|
|
|
def run(self, terms, variables=None, **kwargs):
|
|
|
|
"""
|
|
|
|
When the playbook specifies a lookup, this method is run. The
|
|
|
|
arguments to the lookup become the arguments to this method. One
|
|
|
|
additional keyword argument named ``variables`` is added to the method
|
|
|
|
call. It contains the variables available to ansible at the time the
|
|
|
|
lookup is templated. For instance::
|
|
|
|
|
|
|
|
"{{ lookup('url', 'https://toshio.fedorapeople.org/one.txt', validate_certs=True) }}"
|
|
|
|
|
|
|
|
would end up calling the lookup plugin named url's run method like this::
|
|
|
|
run(['https://toshio.fedorapeople.org/one.txt'], variables=available_variables, validate_certs=True)
|
|
|
|
|
|
|
|
Lookup plugins can be used within playbooks for looping. When this
|
|
|
|
happens, the first argument is a list containing the terms. Lookup
|
|
|
|
plugins can also be called from within playbooks to return their
|
|
|
|
values into a variable or parameter. If the user passes a string in
|
|
|
|
this case, it is converted into a list.
|
|
|
|
|
|
|
|
Errors encountered during execution should be returned by raising
|
|
|
|
AnsibleError() with a message describing the error.
|
|
|
|
|
|
|
|
Any strings returned by this method that could ever contain non-ascii
|
|
|
|
must be converted into python's unicode type as the strings will be run
|
|
|
|
through jinja2 which has this requirement. You can use::
|
|
|
|
|
|
|
|
from ansible.module_utils.unicode import to_unicode
|
|
|
|
result_string = to_unicode(result_string)
|
|
|
|
"""
|
|
|
|
pass
|