Reduce complexity of Templar._lookup slightly (#73277)

* A little more complexity reduction
* restore logic
* Readability
* Add ran check back
* Add clog
This commit is contained in:
Matt Martz 2021-03-01 15:11:54 -06:00 committed by GitHub
parent 949b2cc74c
commit ba3f84883f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 52 deletions

View file

@ -0,0 +1,3 @@
minor_changes:
- Templar - reduce the complexity of ``Templar._lookup``
(https://github.com/ansible/ansible/pull/73277)

View file

@ -56,6 +56,7 @@ from ansible.template.vars import AnsibleJ2Vars
from ansible.utils.collection_loader import AnsibleCollectionRef from ansible.utils.collection_loader import AnsibleCollectionRef
from ansible.utils.display import Display from ansible.utils.display import Display
from ansible.utils.collection_loader._collection_finder import _get_collection_metadata from ansible.utils.collection_loader._collection_finder import _get_collection_metadata
from ansible.utils.listify import listify_lookup_plugin_terms
from ansible.utils.unsafe_proxy import wrap_var from ansible.utils.unsafe_proxy import wrap_var
display = Display() display = Display()
@ -974,12 +975,13 @@ class Templar:
def _lookup(self, name, *args, **kwargs): def _lookup(self, name, *args, **kwargs):
instance = lookup_loader.get(name, loader=self._loader, templar=self) instance = lookup_loader.get(name, loader=self._loader, templar=self)
if instance is not None: if instance is None:
raise AnsibleError("lookup plugin (%s) not found" % name)
wantlist = kwargs.pop('wantlist', False) wantlist = kwargs.pop('wantlist', False)
allow_unsafe = kwargs.pop('allow_unsafe', C.DEFAULT_ALLOW_UNSAFE_LOOKUPS) allow_unsafe = kwargs.pop('allow_unsafe', C.DEFAULT_ALLOW_UNSAFE_LOOKUPS)
errors = kwargs.pop('errors', 'strict') errors = kwargs.pop('errors', 'strict')
from ansible.utils.listify import listify_lookup_plugin_terms
loop_terms = listify_lookup_plugin_terms(terms=args, templar=self, loader=self._loader, fail_on_undefined=True, convert_bare=False) loop_terms = listify_lookup_plugin_terms(terms=args, templar=self, loader=self._loader, fail_on_undefined=True, convert_bare=False)
# safely catch run failures per #5059 # safely catch run failures per #5059
try: try:
@ -996,12 +998,15 @@ class Templar:
display.display(msg, log_only=True) display.display(msg, log_only=True)
else: else:
raise AnsibleError(to_native(msg)) raise AnsibleError(to_native(msg))
ran = [] if wantlist else None return [] if wantlist else None
if ran and allow_unsafe is False:
if self.cur_context:
self.cur_context.unsafe = True
if ran and not allow_unsafe:
if wantlist: if wantlist:
ran = wrap_var(ran) return wrap_var(ran)
else:
try: try:
if self.jinja2_native and isinstance(ran[0], NativeJinjaText): if self.jinja2_native and isinstance(ran[0], NativeJinjaText):
ran = wrap_var(NativeJinjaText(",".join(ran))) ran = wrap_var(NativeJinjaText(",".join(ran)))
@ -1021,11 +1026,7 @@ class Templar:
else: else:
ran = wrap_var(ran) ran = wrap_var(ran)
if self.cur_context:
self.cur_context.unsafe = True
return ran return ran
else:
raise AnsibleError("lookup plugin (%s) not found" % name)
def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, disable_lookups=False): def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, disable_lookups=False):
if self.jinja2_native and not isinstance(data, string_types): if self.jinja2_native and not isinstance(data, string_types):

View file

@ -21,7 +21,6 @@ __metaclass__ = type
from ansible.module_utils.six import string_types from ansible.module_utils.six import string_types
from ansible.module_utils.common._collections_compat import Iterable from ansible.module_utils.common._collections_compat import Iterable
from ansible.template.safe_eval import safe_eval
__all__ = ['listify_lookup_plugin_terms'] __all__ = ['listify_lookup_plugin_terms']