2012-09-06 22:57:04 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2013-03-11 21:13:59 +00:00
|
|
|
import pipes
|
2013-04-10 21:52:35 +00:00
|
|
|
from ansible.utils import template
|
2012-09-06 22:57:04 +00:00
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
|
|
|
from ansible.runner.return_data import ReturnData
|
2013-02-08 03:51:33 +00:00
|
|
|
import base64
|
2012-09-06 22:57:04 +00:00
|
|
|
|
|
|
|
class ActionModule(object):
|
|
|
|
|
2013-12-11 09:55:56 +00:00
|
|
|
TRANSFERS_FILES = True
|
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
def __init__(self, runner):
|
|
|
|
self.runner = runner
|
|
|
|
|
2013-02-17 20:01:49 +00:00
|
|
|
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
2012-09-06 22:57:04 +00:00
|
|
|
''' handler for template operations '''
|
|
|
|
|
2013-02-04 00:46:25 +00:00
|
|
|
# note: since this module just calls the copy module, the --check mode support
|
|
|
|
# can be implemented entirely over there
|
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
if not self.runner.is_playbook:
|
|
|
|
raise errors.AnsibleError("in current versions of ansible, templates are only usable in playbooks")
|
|
|
|
|
|
|
|
# load up options
|
2013-02-28 13:27:42 +00:00
|
|
|
options = {}
|
|
|
|
if complex_args:
|
|
|
|
options.update(complex_args)
|
|
|
|
options.update(utils.parse_kv(module_args))
|
2013-03-07 05:04:48 +00:00
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
source = options.get('src', None)
|
|
|
|
dest = options.get('dest', None)
|
2012-10-13 00:07:05 +00:00
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
if (source is None and 'first_available_file' not in inject) or dest is None:
|
|
|
|
result = dict(failed=True, msg="src and dest are required")
|
|
|
|
return ReturnData(conn=conn, comm_ok=False, result=result)
|
|
|
|
|
|
|
|
# if we have first_available_file in our vars
|
|
|
|
# look up the files and use the first one we find as src
|
2013-02-08 03:51:33 +00:00
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
if 'first_available_file' in inject:
|
|
|
|
found = False
|
|
|
|
for fn in self.runner.module_vars.get('first_available_file'):
|
2013-04-12 03:40:23 +00:00
|
|
|
fn_orig = fn
|
2013-04-10 21:52:35 +00:00
|
|
|
fnt = template.template(self.runner.basedir, fn, inject)
|
2012-11-06 19:34:05 +00:00
|
|
|
fnd = utils.path_dwim(self.runner.basedir, fnt)
|
2013-04-06 16:13:04 +00:00
|
|
|
if not os.path.exists(fnd) and '_original_file' in inject:
|
2013-06-03 20:47:50 +00:00
|
|
|
fnd = utils.path_dwim_relative(inject['_original_file'], 'templates', fnt, self.runner.basedir, check=False)
|
2012-11-06 19:34:05 +00:00
|
|
|
if os.path.exists(fnd):
|
2013-04-12 03:40:23 +00:00
|
|
|
source = fnd
|
2012-09-06 22:57:04 +00:00
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
result = dict(failed=True, msg="could not find src in first_available_file list")
|
|
|
|
return ReturnData(conn=conn, comm_ok=False, result=result)
|
2012-11-06 19:34:05 +00:00
|
|
|
else:
|
2013-04-10 21:52:35 +00:00
|
|
|
source = template.template(self.runner.basedir, source, inject)
|
2013-04-06 16:13:04 +00:00
|
|
|
|
|
|
|
if '_original_file' in inject:
|
|
|
|
source = utils.path_dwim_relative(inject['_original_file'], 'templates', source, self.runner.basedir)
|
|
|
|
else:
|
|
|
|
source = utils.path_dwim(self.runner.basedir, source)
|
|
|
|
|
2012-09-06 22:57:04 +00:00
|
|
|
|
2013-01-25 22:14:52 +00:00
|
|
|
if dest.endswith("/"):
|
|
|
|
base = os.path.basename(source)
|
|
|
|
dest = os.path.join(dest, base)
|
|
|
|
|
2013-02-04 00:46:25 +00:00
|
|
|
# template the source data locally & get ready to transfer
|
2012-09-06 22:57:04 +00:00
|
|
|
try:
|
2014-04-04 17:05:33 +00:00
|
|
|
resultant = template.template_from_file(self.runner.basedir, source, inject, vault_password=self.runner.vault_pass)
|
2012-09-06 22:57:04 +00:00
|
|
|
except Exception, e:
|
|
|
|
result = dict(failed=True, msg=str(e))
|
|
|
|
return ReturnData(conn=conn, comm_ok=False, result=result)
|
|
|
|
|
2013-02-04 00:46:25 +00:00
|
|
|
local_md5 = utils.md5s(resultant)
|
|
|
|
remote_md5 = self.runner._remote_md5(conn, tmp, dest)
|
|
|
|
|
|
|
|
if local_md5 != remote_md5:
|
|
|
|
|
2013-02-18 00:40:38 +00:00
|
|
|
# template is different from the remote value
|
|
|
|
|
|
|
|
# if showing diffs, we need to get the remote value
|
2013-02-26 15:45:56 +00:00
|
|
|
dest_contents = ''
|
2013-02-18 00:40:38 +00:00
|
|
|
|
|
|
|
if self.runner.diff:
|
|
|
|
# using persist_files to keep the temp directory around to avoid needing to grab another
|
|
|
|
dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True)
|
|
|
|
if 'content' in dest_result.result:
|
|
|
|
dest_contents = dest_result.result['content']
|
|
|
|
if dest_result.result['encoding'] == 'base64':
|
|
|
|
dest_contents = base64.b64decode(dest_contents)
|
|
|
|
else:
|
|
|
|
raise Exception("unknown encoding, failed: %s" % dest_result.result)
|
2013-02-10 04:24:03 +00:00
|
|
|
|
2013-02-18 00:40:38 +00:00
|
|
|
xfered = self.runner._transfer_str(conn, tmp, 'source', resultant)
|
|
|
|
|
|
|
|
# fix file permissions when the copy is done as a different user
|
|
|
|
if self.runner.sudo and self.runner.sudo_user != 'root':
|
|
|
|
self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp)
|
|
|
|
|
|
|
|
# run the copy module
|
2013-03-26 02:19:35 +00:00
|
|
|
module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(source)))
|
2013-02-18 00:40:38 +00:00
|
|
|
|
2013-08-20 21:09:44 +00:00
|
|
|
if self.runner.noop_on_check(inject):
|
2013-02-26 15:53:59 +00:00
|
|
|
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))
|
2013-02-18 00:40:38 +00:00
|
|
|
else:
|
2013-02-28 13:27:42 +00:00
|
|
|
res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject, complex_args=complex_args)
|
2014-03-24 17:55:31 +00:00
|
|
|
if res.result.get('changed', False):
|
2014-03-21 07:40:38 +00:00
|
|
|
res.diff = dict(before=dest_contents, after=resultant)
|
2013-02-18 00:40:38 +00:00
|
|
|
return res
|
2013-02-04 00:46:25 +00:00
|
|
|
else:
|
2013-02-28 13:27:42 +00:00
|
|
|
return self.runner._execute_module(conn, tmp, 'file', module_args, inject=inject, complex_args=complex_args)
|
2012-09-06 22:57:04 +00:00
|
|
|
|