Teach playbooks to template locally to eliminate the need for Jinja2 on remote nodes.

You still need jinja2 if using /usr/bin/ansible vs /usr/bin/ansible-playbook though
this could change later by fetching the ansible file with a 'slurp' module.
pull/176/head
Michael DeHaan 13 years ago
parent 9c64ceb0f8
commit 5fa3d9b148

@ -201,23 +201,22 @@ class Runner(object):
# ***************************************************** # *****************************************************
def _transfer_str(self, conn, tmp, name, args_str): def _transfer_str(self, conn, tmp, name, data):
''' transfer arguments as a single file to be fed to the module. ''' ''' transfer string to remote file '''
if type(args_str) == dict: if type(data) == dict:
args_str = utils.smjson(args_str) data = utils.smjson(data)
args_fd, args_file = tempfile.mkstemp() afd, afile = tempfile.mkstemp()
args_fo = os.fdopen(args_fd, 'w') afo = os.fdopen(afd, 'w')
args_fo.write(args_str) afo.write(data)
args_fo.flush() afo.flush()
args_fo.close() afo.close()
args_remote = os.path.join(tmp, name) remote = os.path.join(tmp, name)
conn.put_file(args_file, args_remote) conn.put_file(afile, remote)
os.unlink(args_file) os.unlink(afile)
return remote
return args_remote
# ***************************************************** # *****************************************************
@ -406,7 +405,6 @@ class Runner(object):
# files are saved in dest dir, with a subdir for each host, then the filename # files are saved in dest dir, with a subdir for each host, then the filename
dest = "%s/%s/%s" % (utils.path_dwim(self.basedir, dest), host, source) dest = "%s/%s/%s" % (utils.path_dwim(self.basedir, dest), host, source)
dest = dest.replace("//","/") dest = dest.replace("//","/")
print "DEST=%s" % dest
# compare old and new md5 for support of change hooks # compare old and new md5 for support of change hooks
local_md5 = None local_md5 = None
@ -468,25 +466,56 @@ class Runner(object):
inject = self.setup_cache.get(conn.host,{}) inject = self.setup_cache.get(conn.host,{})
source = utils.template(source, inject) source = utils.template(source, inject)
# first copy the source template over (host, ok, data, err) = (None, None, None, None)
temppath = tmp + os.path.split(source)[-1]
conn.put_file(utils.path_dwim(self.basedir, source), temppath)
# install the template module if not self.is_playbook:
template_module = self._transfer_module(conn, tmp, 'template')
# transfer module vars # templating remotely, since we don't have the benefit of SETUP_CACHE
if self.module_vars: # TODO: maybe just fetch the setup file to a tempfile
vars = utils.bigjson(self.module_vars)
vars_path = self._transfer_str(conn, tmp, 'module_vars', vars) # first copy the source template over
vars_arg=" vars=%s"%(vars_path) temppath = tmp + os.path.split(source)[-1]
else: conn.put_file(utils.path_dwim(self.basedir, source), temppath)
vars_arg=""
# install the template module
template_module = self._transfer_module(conn, tmp, 'template')
# transfer module vars
if self.module_vars:
vars = utils.bigjson(self.module_vars)
vars_path = self._transfer_str(conn, tmp, 'module_vars', vars)
vars_arg=" vars=%s"%(vars_path)
else:
vars_arg=""
# run the template module # run the template module
args = "src=%s dest=%s metadata=%s%s" % (temppath, dest, metadata, vars_arg) args = "src=%s dest=%s metadata=%s%s" % (temppath, dest, metadata, vars_arg)
(result1, err, executed) = self._execute_module(conn, tmp, template_module, args) (result1, err, executed) = self._execute_module(conn, tmp, template_module, args)
(host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed) (host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed)
else:
# templating LOCALLY to avoid Jinja2 dependency on nodes inside playbook runs
# non-playbook path can be moved to use this IF it is willing to fetch
# the metadata file first
# install the template module
copy_module = self._transfer_module(conn, tmp, 'copy')
# playbooks can template locally to avoid the jinja2 dependency
source_data = file(utils.path_dwim(self.basedir, source)).read()
resultant = ''
try:
resultant = utils.template(source_data, inject)
except Exception, e:
return (host, False, dict(failed=True, msg=str(e)), '')
xfered = self._transfer_str(conn, tmp, 'source', resultant)
# run the COPY module
args = "src=%s dest=%s" % (xfered, dest)
(result1, err, executed) = self._execute_module(conn, tmp, copy_module, args)
(host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed)
if ok: if ok:
return self._chain_file_module(conn, tmp, data, err, options, executed) return self._chain_file_module(conn, tmp, data, err, options, executed)

Loading…
Cancel
Save