2015-01-02 13:51:15 +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/>.
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
import os
|
2017-07-05 14:39:00 +00:00
|
|
|
import re
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2016-06-28 21:23:30 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
from ansible.plugins.action import ActionBase
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
2015-01-02 13:51:15 +00:00
|
|
|
class ActionModule(ActionBase):
|
|
|
|
TRANSFERS_FILES = True
|
|
|
|
|
2017-07-05 14:39:00 +00:00
|
|
|
# On Windows platform, absolute paths begin with a (back)slash
|
|
|
|
# after chopping off a potential drive letter.
|
|
|
|
windows_absolute_path_detection = re.compile(r'^(?:[a-zA-Z]\:)?(\\|\/)')
|
|
|
|
|
2015-01-02 13:51:15 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
''' handler for file transfer operations '''
|
2015-10-22 23:07:26 +00:00
|
|
|
if task_vars is None:
|
|
|
|
task_vars = dict()
|
|
|
|
|
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
if not tmp:
|
2016-12-14 17:52:18 +00:00
|
|
|
tmp = self._make_tmp_path()
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
creates = self._task.args.get('creates')
|
|
|
|
if creates:
|
|
|
|
# do not run the command if the line contains creates=filename
|
|
|
|
# and the filename already exists. This allows idempotence
|
|
|
|
# of command executions.
|
2016-03-25 15:13:44 +00:00
|
|
|
if self._remote_file_exists(creates):
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-01-02 13:51:15 +00:00
|
|
|
return dict(skipped=True, msg=("skipped, since %s exists" % creates))
|
|
|
|
|
|
|
|
removes = self._task.args.get('removes')
|
|
|
|
if removes:
|
|
|
|
# do not run the command if the line contains removes=filename
|
|
|
|
# and the filename does not exist. This allows idempotence
|
|
|
|
# of command executions.
|
2016-03-31 19:18:48 +00:00
|
|
|
if not self._remote_file_exists(removes):
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-01-02 13:51:15 +00:00
|
|
|
return dict(skipped=True, msg=("skipped, since %s does not exist" % removes))
|
|
|
|
|
2017-07-05 14:39:00 +00:00
|
|
|
# The chdir must be absolute, because a relative path would rely on
|
|
|
|
# remote node behaviour & user config.
|
|
|
|
chdir = self._task.args.get('chdir')
|
|
|
|
if chdir:
|
|
|
|
# Powershell is the only Windows-path aware shell
|
|
|
|
if self._connection._shell.SHELL_FAMILY == 'powershell' and \
|
|
|
|
not self.windows_absolute_path_detection.matches(chdir):
|
|
|
|
return dict(failed=True, msg='chdir %s must be an absolute path for a Windows remote node' % chdir)
|
|
|
|
# Every other shell is unix-path-aware.
|
|
|
|
if self._connection._shell.SHELL_FAMILY != 'powershell' and not chdir.startswith('/'):
|
|
|
|
return dict(failed=True, msg='chdir %s must be an absolute path for a Unix-aware remote node' % chdir)
|
|
|
|
|
2015-01-02 13:51:15 +00:00
|
|
|
# the script name is the first item in the raw params, so we split it
|
|
|
|
# out now so we know the file name we need to transfer to the remote,
|
|
|
|
# and everything else is an argument to the script which we need later
|
|
|
|
# to append to the remote command
|
2017-06-02 11:14:11 +00:00
|
|
|
parts = self._task.args.get('_raw_params', '').strip().split()
|
2015-01-02 13:51:15 +00:00
|
|
|
source = parts[0]
|
2017-06-02 11:14:11 +00:00
|
|
|
args = ' '.join(parts[1:])
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2016-06-28 21:23:30 +00:00
|
|
|
try:
|
2017-03-24 19:39:25 +00:00
|
|
|
source = self._loader.get_real_file(self._find_needle('files', source), decrypt=self._task.args.get('decrypt', True))
|
2016-06-28 21:23:30 +00:00
|
|
|
except AnsibleError as e:
|
2016-09-07 05:54:17 +00:00
|
|
|
return dict(failed=True, msg=to_native(e))
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
# transfer the file to a remote tmp location
|
2015-07-01 15:15:40 +00:00
|
|
|
tmp_src = self._connection._shell.join_path(tmp, os.path.basename(source))
|
2016-03-21 21:17:53 +00:00
|
|
|
self._transfer_file(source, tmp_src)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
# set file permissions, more permissive when the copy is done as a different user
|
2016-12-14 17:52:18 +00:00
|
|
|
self._fixup_perms2((tmp, tmp_src), execute=True)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
# add preparation steps to one ssh roundtrip executing the script
|
2017-03-24 00:48:15 +00:00
|
|
|
env_dict = dict()
|
|
|
|
env_string = self._compute_environment_string(env_dict)
|
2015-01-02 13:51:15 +00:00
|
|
|
script_cmd = ' '.join([env_string, tmp_src, args])
|
2017-02-17 08:09:56 +00:00
|
|
|
script_cmd = self._connection._shell.wrap_for_exec(script_cmd)
|
2015-08-10 17:18:23 +00:00
|
|
|
|
2017-03-24 00:48:15 +00:00
|
|
|
exec_data = None
|
|
|
|
# HACK: come up with a sane way to pass around env outside the command
|
|
|
|
if self._connection.transport == "winrm":
|
|
|
|
exec_data = self._connection._create_raw_wrapper_payload(script_cmd, env_dict)
|
|
|
|
|
2017-07-05 14:39:00 +00:00
|
|
|
result.update(self._low_level_execute_command(cmd=script_cmd, in_data=exec_data, sudoable=True, chdir=chdir))
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
# clean up after
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
result['changed'] = True
|
|
|
|
|
2017-05-20 22:25:57 +00:00
|
|
|
if 'rc' in result and result['rc'] != 0:
|
|
|
|
result['failed'] = True
|
|
|
|
result['msg'] = 'non-zero return code'
|
|
|
|
|
2015-01-02 13:51:15 +00:00
|
|
|
return result
|