mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Add code to flag legacy templating like $foo.{bar} as deprecated in favor of Jinja2 {{ foo.bar }} so we
can remove the legacy system at a later date.
This commit is contained in:
@@ -34,6 +34,7 @@ import pipes
|
||||
import ansible.constants as C
|
||||
import ansible.inventory
|
||||
from ansible import utils
|
||||
from ansible.utils import template
|
||||
from ansible import errors
|
||||
from ansible import module_common
|
||||
import poller
|
||||
@@ -62,7 +63,14 @@ def _executor_hook(job_queue, result_queue):
|
||||
while not job_queue.empty():
|
||||
try:
|
||||
host = job_queue.get(block=False)
|
||||
result_queue.put(multiprocessing_runner._executor(host))
|
||||
return_data = multiprocessing_runner._executor(host)
|
||||
result_queue.put(return_data)
|
||||
|
||||
# print "FLAGS=%s" % return_data.flags
|
||||
if 'LEGACY_TEMPLATE_WARNING' in return_data.flags:
|
||||
# pass data back up across the multiprocessing fork boundary
|
||||
template.Flags.LEGACY_TEMPLATE_WARNING = True
|
||||
|
||||
except Queue.Empty:
|
||||
pass
|
||||
except:
|
||||
@@ -233,7 +241,7 @@ class Runner(object):
|
||||
|
||||
if not self.environment:
|
||||
return ""
|
||||
enviro = utils.template(self.basedir, self.environment, inject)
|
||||
enviro = template.template(self.basedir, self.environment, inject)
|
||||
if type(enviro) != dict:
|
||||
raise errors.AnsibleError("environment must be a dictionary, received %s" % enviro)
|
||||
result = ""
|
||||
@@ -271,7 +279,7 @@ class Runner(object):
|
||||
# do --check mode, so to be safe we will not run it.
|
||||
return ReturnData(conn=conn, result=dict(skippped=True, msg="cannot run check mode against old-style modules"))
|
||||
|
||||
args = utils.template(self.basedir, args, inject)
|
||||
args = template.template(self.basedir, args, inject)
|
||||
argsfile = self._transfer_str(conn, tmp, 'arguments', args)
|
||||
if async_jid is None:
|
||||
cmd = "%s %s" % (remote_module_path, argsfile)
|
||||
@@ -300,10 +308,20 @@ class Runner(object):
|
||||
def _executor(self, host):
|
||||
''' handler for multiprocessing library '''
|
||||
|
||||
def get_flags():
|
||||
# flags are a way of passing arbitrary event information
|
||||
# back up the chain, since multiprocessing forks and doesn't
|
||||
# allow state exchange
|
||||
flags = []
|
||||
if template.Flags.LEGACY_TEMPLATE_WARNING:
|
||||
flags.append('LEGACY_TEMPLATE_WARNING')
|
||||
return flags
|
||||
|
||||
try:
|
||||
exec_rc = self._executor_internal(host)
|
||||
if type(exec_rc) != ReturnData:
|
||||
raise Exception("unexpected return type: %s" % type(exec_rc))
|
||||
exec_rc.flags = get_flags()
|
||||
# redundant, right?
|
||||
if not exec_rc.comm_ok:
|
||||
self.callbacks.on_unreachable(host, exec_rc.result)
|
||||
@@ -311,11 +329,11 @@ class Runner(object):
|
||||
except errors.AnsibleError, ae:
|
||||
msg = str(ae)
|
||||
self.callbacks.on_unreachable(host, msg)
|
||||
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg))
|
||||
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg), flags=get_flags())
|
||||
except Exception:
|
||||
msg = traceback.format_exc()
|
||||
self.callbacks.on_unreachable(host, msg)
|
||||
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg))
|
||||
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg), flags=get_flags())
|
||||
|
||||
# *****************************************************
|
||||
|
||||
@@ -353,7 +371,7 @@ class Runner(object):
|
||||
if items_plugin is not None and items_plugin in utils.plugins.lookup_loader:
|
||||
|
||||
items_terms = self.module_vars.get('items_lookup_terms', '')
|
||||
items_terms = utils.template(self.basedir, items_terms, inject)
|
||||
items_terms = template.template(self.basedir, items_terms, inject)
|
||||
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=self.basedir).run(items_terms, inject=inject)
|
||||
if type(items) != list:
|
||||
raise errors.AnsibleError("lookup plugins have to return a list: %r" % items)
|
||||
@@ -423,9 +441,9 @@ class Runner(object):
|
||||
new_args = new_args + "%s='%s' " % (k,v)
|
||||
module_args = new_args
|
||||
|
||||
module_name = utils.template(self.basedir, module_name, inject)
|
||||
module_args = utils.template(self.basedir, module_args, inject)
|
||||
complex_args = utils.template(self.basedir, complex_args, inject)
|
||||
module_name = template.template(self.basedir, module_name, inject)
|
||||
module_args = template.template(self.basedir, module_args, inject)
|
||||
complex_args = template.template(self.basedir, complex_args, inject)
|
||||
|
||||
if module_name in utils.plugins.action_loader:
|
||||
if self.background != 0:
|
||||
@@ -436,7 +454,7 @@ class Runner(object):
|
||||
else:
|
||||
handler = utils.plugins.action_loader.get('async', self)
|
||||
|
||||
conditional = utils.template(self.basedir, self.conditional, inject, expand_lists=False)
|
||||
conditional = template.template(self.basedir, self.conditional, inject, expand_lists=False)
|
||||
|
||||
if not utils.check_conditional(conditional):
|
||||
result = utils.jsonify(dict(skipped=True))
|
||||
@@ -457,7 +475,7 @@ class Runner(object):
|
||||
# and we need to transfer those, and only those, variables
|
||||
delegate_to = inject.get('delegate_to', None)
|
||||
if delegate_to is not None:
|
||||
delegate_to = utils.template(self.basedir, delegate_to, inject)
|
||||
delegate_to = template.template(self.basedir, delegate_to, inject)
|
||||
inject = inject.copy()
|
||||
interpreters = []
|
||||
for i in inject:
|
||||
@@ -481,8 +499,8 @@ class Runner(object):
|
||||
actual_host = delegate_to
|
||||
actual_port = port
|
||||
|
||||
actual_user = utils.template(self.basedir, actual_user, inject)
|
||||
actual_pass = utils.template(self.basedir, actual_pass, inject)
|
||||
actual_user = template.template(self.basedir, actual_user, inject)
|
||||
actual_pass = template.template(self.basedir, actual_pass, inject)
|
||||
|
||||
try:
|
||||
if actual_port is not None:
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
import os
|
||||
|
||||
from ansible import utils
|
||||
from ansible import template
|
||||
from ansible import errors
|
||||
from ansible.runner.return_data import ReturnData
|
||||
import base64
|
||||
@@ -54,7 +55,7 @@ class ActionModule(object):
|
||||
if 'first_available_file' in inject:
|
||||
found = False
|
||||
for fn in inject.get('first_available_file'):
|
||||
fn = utils.template(self.runner.basedir, fn, inject)
|
||||
fn = template.template(self.runner.basedir, fn, inject)
|
||||
fn = utils.path_dwim(self.runner.basedir, fn)
|
||||
if not os.path.exists(fn) and '_original_file' in inject:
|
||||
fn = utils.path_dwim_relative(inject['_original_file'], 'files', fn, self.runner.basedir, check=False)
|
||||
@@ -77,7 +78,7 @@ class ActionModule(object):
|
||||
f.close()
|
||||
source = tmp_content
|
||||
else:
|
||||
source = utils.template(self.runner.basedir, source, inject)
|
||||
source = template.template(self.runner.basedir, source, inject)
|
||||
if '_original_file' in inject:
|
||||
source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
|
||||
else:
|
||||
|
||||
@@ -19,6 +19,7 @@ import os
|
||||
import shlex
|
||||
|
||||
import ansible.constants as C
|
||||
from ansible.utils import template
|
||||
from ansible import utils
|
||||
from ansible import errors
|
||||
from ansible.runner.return_data import ReturnData
|
||||
@@ -39,7 +40,7 @@ class ActionModule(object):
|
||||
source = tokens[0]
|
||||
# FIXME: error handling
|
||||
args = " ".join(tokens[1:])
|
||||
source = utils.template(self.runner.basedir, source, inject)
|
||||
source = template.template(self.runner.basedir, source, inject)
|
||||
source = utils.path_dwim(self.runner.basedir, source)
|
||||
|
||||
# transfer the file to a remote tmp location
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
import os
|
||||
import pipes
|
||||
from ansible.utils import template
|
||||
from ansible import utils
|
||||
from ansible import errors
|
||||
from ansible.runner.return_data import ReturnData
|
||||
@@ -56,7 +57,7 @@ class ActionModule(object):
|
||||
|
||||
found = False
|
||||
for fn in self.runner.module_vars.get('first_available_file'):
|
||||
fnt = utils.template(self.runner.basedir, fn, inject)
|
||||
fnt = template.template(self.runner.basedir, fn, inject)
|
||||
fnd = utils.path_dwim(self.runner.basedir, fnt)
|
||||
if not os.path.exists(fnd) and '_original_file' in inject:
|
||||
fnd = utils.path_dwim_relative(inject['_original_file'], 'templates', fnd, self.runner.basedir, check=False)
|
||||
@@ -68,7 +69,7 @@ class ActionModule(object):
|
||||
result = dict(failed=True, msg="could not find src in first_available_file list")
|
||||
return ReturnData(conn=conn, comm_ok=False, result=result)
|
||||
else:
|
||||
source = utils.template(self.runner.basedir, source, inject)
|
||||
source = template.template(self.runner.basedir, source, inject)
|
||||
|
||||
if '_original_file' in inject:
|
||||
source = utils.path_dwim_relative(inject['_original_file'], 'templates', source, self.runner.basedir)
|
||||
@@ -82,7 +83,7 @@ class ActionModule(object):
|
||||
|
||||
# template the source data locally & get ready to transfer
|
||||
try:
|
||||
resultant = utils.template_from_file(self.runner.basedir, source, inject)
|
||||
resultant = template.template_from_file(self.runner.basedir, source, inject)
|
||||
except Exception, e:
|
||||
result = dict(failed=True, msg=str(e))
|
||||
return ReturnData(conn=conn, comm_ok=False, result=result)
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible import utils
|
||||
from ansible.utils import template
|
||||
|
||||
class LookupModule(object):
|
||||
|
||||
@@ -27,5 +27,5 @@ class LookupModule(object):
|
||||
terms = [ terms ]
|
||||
ret = []
|
||||
for term in terms:
|
||||
ret.append(utils.template_from_file(self.basedir, term, inject))
|
||||
ret.append(template.template_from_file(self.basedir, term, inject))
|
||||
return ret
|
||||
|
||||
@@ -20,10 +20,10 @@ from ansible import utils
|
||||
class ReturnData(object):
|
||||
''' internal return class for runner execute methods, not part of public API signature '''
|
||||
|
||||
__slots__ = [ 'result', 'comm_ok', 'host', 'diff' ]
|
||||
__slots__ = [ 'result', 'comm_ok', 'host', 'diff', 'flags' ]
|
||||
|
||||
def __init__(self, conn=None, host=None, result=None,
|
||||
comm_ok=True, diff=dict()):
|
||||
comm_ok=True, diff=dict(), flags=None):
|
||||
|
||||
# which host is this ReturnData about?
|
||||
if conn is not None:
|
||||
@@ -32,7 +32,6 @@ class ReturnData(object):
|
||||
if delegate is not None:
|
||||
self.host = delegate
|
||||
|
||||
|
||||
else:
|
||||
self.host = host
|
||||
|
||||
@@ -52,6 +51,10 @@ class ReturnData(object):
|
||||
if type(self.result) != dict:
|
||||
raise Exception("dictionary result expected")
|
||||
|
||||
if flags is None:
|
||||
flags = []
|
||||
self.flags = []
|
||||
|
||||
def communicated_ok(self):
|
||||
return self.comm_ok
|
||||
|
||||
|
||||
Reference in New Issue
Block a user