mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 11:10:21 -07:00
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.
This commit is contained in:
parent
9c64ceb0f8
commit
5fa3d9b148
1 changed files with 60 additions and 31 deletions
|
@ -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,6 +466,13 @@ 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)
|
||||||
|
|
||||||
|
(host, ok, data, err) = (None, None, None, None)
|
||||||
|
|
||||||
|
if not self.is_playbook:
|
||||||
|
|
||||||
|
# templating remotely, since we don't have the benefit of SETUP_CACHE
|
||||||
|
# TODO: maybe just fetch the setup file to a tempfile
|
||||||
|
|
||||||
# first copy the source template over
|
# first copy the source template over
|
||||||
temppath = tmp + os.path.split(source)[-1]
|
temppath = tmp + os.path.split(source)[-1]
|
||||||
conn.put_file(utils.path_dwim(self.basedir, source), temppath)
|
conn.put_file(utils.path_dwim(self.basedir, source), temppath)
|
||||||
|
@ -488,6 +493,30 @@ class Runner(object):
|
||||||
(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)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue