mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Use templar all the way down
Fixes bugs related to creating Templar() objects on the fly, where the shared loader objects (serialized to TaskExecutor) aren't used so information loaded into plugin loaders after forking is lost. Fixes #11815
This commit is contained in:
parent
c3ce140dd2
commit
5266679964
15 changed files with 44 additions and 42 deletions
|
@ -42,6 +42,11 @@ PATH_CACHE = {}
|
|||
PLUGIN_PATH_CACHE = {}
|
||||
_basedirs = []
|
||||
|
||||
# FIXME: the _basedirs code may be dead, and no longer needed, as
|
||||
# we now use add_directory for all plugin types here instead
|
||||
# of relying on this global variable (which also causes problems
|
||||
# with forked processes). See the Playbook() and Role() classes
|
||||
# for how we now ue get_all_plugin_loaders() below.
|
||||
def push_basedir(basedir):
|
||||
# avoid pushing the same absolute dir more than once
|
||||
basedir = to_unicode(os.path.realpath(basedir))
|
||||
|
|
|
@ -28,8 +28,9 @@ except ImportError:
|
|||
__all__ = ['LookupBase']
|
||||
|
||||
class LookupBase:
|
||||
def __init__(self, loader=None, **kwargs):
|
||||
def __init__(self, loader=None, templar=None, **kwargs):
|
||||
self._loader = loader
|
||||
self._templar = templar
|
||||
self._display = display
|
||||
|
||||
def get_basedir(self, variables):
|
||||
|
|
|
@ -29,16 +29,16 @@ class LookupModule(LookupBase):
|
|||
[1, 2, 3], [a, b] -> [1, a], [1, b], [2, a], [2, b], [3, a], [3, b]
|
||||
"""
|
||||
|
||||
def __lookup_variabless(self, terms, variables):
|
||||
def __lookup_variables(self, terms):
|
||||
results = []
|
||||
for x in terms:
|
||||
intermediate = listify_lookup_plugin_terms(x, variables, loader=self._loader)
|
||||
intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader)
|
||||
results.append(intermediate)
|
||||
return results
|
||||
|
||||
def run(self, terms, variables=None, **kwargs):
|
||||
|
||||
terms = self.__lookup_variabless(terms, variables)
|
||||
terms = self.__lookup_variables(terms)
|
||||
|
||||
my_list = terms[:]
|
||||
if len(my_list) == 0:
|
||||
|
|
|
@ -125,7 +125,6 @@ from jinja2.exceptions import UndefinedError
|
|||
|
||||
from ansible.errors import AnsibleLookupError, AnsibleUndefinedVariable
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.template import Templar
|
||||
from ansible.utils.boolean import boolean
|
||||
|
||||
class LookupModule(LookupBase):
|
||||
|
@ -174,11 +173,10 @@ class LookupModule(LookupBase):
|
|||
else:
|
||||
total_search = terms
|
||||
|
||||
templar = Templar(loader=self._loader, variables=variables)
|
||||
roledir = variables.get('roledir')
|
||||
for fn in total_search:
|
||||
try:
|
||||
fn = templar.template(fn)
|
||||
fn = self._templar.template(fn)
|
||||
except (AnsibleUndefinedVariable, UndefinedError) as e:
|
||||
continue
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ class LookupModule(LookupBase):
|
|||
|
||||
if isinstance(term, basestring):
|
||||
# convert a variable to a list
|
||||
term2 = listify_lookup_plugin_terms(term, variables, loader=self._loader)
|
||||
term2 = listify_lookup_plugin_terms(term, templar=self._templar, loader=self._loader)
|
||||
# but avoid converting a plain string to a list of one string
|
||||
if term2 != [ term ]:
|
||||
term = term2
|
||||
|
|
|
@ -31,7 +31,7 @@ class LookupModule(LookupBase):
|
|||
results = []
|
||||
for x in terms:
|
||||
try:
|
||||
intermediate = listify_lookup_plugin_terms(x, variables, loader=self._loader, fail_on_undefined=True)
|
||||
intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader, fail_on_undefined=True)
|
||||
except UndefinedError, e:
|
||||
raise AnsibleUndefinedVariable("One of the nested variables was undefined. The error was: %s" % e)
|
||||
results.append(intermediate)
|
||||
|
|
|
@ -22,7 +22,6 @@ from re import compile as re_compile, IGNORECASE
|
|||
from ansible.errors import *
|
||||
from ansible.parsing.splitter import parse_kv
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.template import Templar
|
||||
|
||||
# shortcut format
|
||||
NUM = "(0?x?[0-9a-f]+)"
|
||||
|
@ -188,13 +187,11 @@ class LookupModule(LookupBase):
|
|||
if isinstance(terms, basestring):
|
||||
terms = [ terms ]
|
||||
|
||||
templar = Templar(loader=self._loader, variables=variables)
|
||||
|
||||
for term in terms:
|
||||
try:
|
||||
self.reset() # clear out things for this iteration
|
||||
|
||||
term = templar.template(term)
|
||||
term = self._templar.template(term)
|
||||
try:
|
||||
if not self.parse_simple_args(term):
|
||||
self.parse_kv_args(parse_kv(term))
|
||||
|
|
|
@ -33,8 +33,9 @@ class LookupModule(LookupBase):
|
|||
raise AnsibleError(
|
||||
"subelements lookup expects a list of two or three items, "
|
||||
+ msg)
|
||||
terms = listify_lookup_plugin_terms(terms, variables, loader=self._loader)
|
||||
terms[0] = listify_lookup_plugin_terms(terms[0], variables, loader=self._loader)
|
||||
|
||||
terms = listify_lookup_plugin_terms(terms, templar=self._templar, loader=self._loader)
|
||||
terms[0] = listify_lookup_plugin_terms(terms[0], templar=self._templar, loader=self._loader)
|
||||
|
||||
# check lookup terms - check number of terms
|
||||
if not isinstance(terms, list) or not 2 <= len(terms) <= 3:
|
||||
|
|
|
@ -21,7 +21,6 @@ import os
|
|||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.template import Templar
|
||||
|
||||
class LookupModule(LookupBase):
|
||||
|
||||
|
@ -34,8 +33,6 @@ class LookupModule(LookupBase):
|
|||
|
||||
ret = []
|
||||
|
||||
templar = Templar(loader=self._loader, variables=variables)
|
||||
|
||||
for term in terms:
|
||||
self._display.debug("File lookup term: %s" % term)
|
||||
|
||||
|
@ -44,7 +41,7 @@ class LookupModule(LookupBase):
|
|||
if lookupfile and os.path.exists(lookupfile):
|
||||
with open(lookupfile, 'r') as f:
|
||||
template_data = f.read()
|
||||
res = templar.template(template_data, preserve_trailing_newlines=True)
|
||||
res = self._templar.template(template_data, preserve_trailing_newlines=True)
|
||||
ret.append(res)
|
||||
else:
|
||||
raise AnsibleError("the template file %s could not be found for the lookup" % term)
|
||||
|
|
|
@ -31,16 +31,16 @@ class LookupModule(LookupBase):
|
|||
[1, 2], [3] -> [1, 3], [2, None]
|
||||
"""
|
||||
|
||||
def __lookup_variabless(self, terms, variables):
|
||||
def __lookup_variables(self, terms):
|
||||
results = []
|
||||
for x in terms:
|
||||
intermediate = listify_lookup_plugin_terms(x, variables, loader=self._loader)
|
||||
intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader)
|
||||
results.append(intermediate)
|
||||
return results
|
||||
|
||||
def run(self, terms, variables=None, **kwargs):
|
||||
|
||||
terms = self.__lookup_variabless(terms, variables)
|
||||
terms = self.__lookup_variables(terms)
|
||||
|
||||
my_list = terms[:]
|
||||
if len(my_list) == 0:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue