2018-09-20 17:31:48 +00:00
|
|
|
|
# coding: utf-8
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# Copyright: (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
|
# Copyright: (c) 2018, Ansible Project
|
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2014-10-02 17:07:05 +00:00
|
|
|
|
|
2014-10-15 23:22:54 +00:00
|
|
|
|
# Make coding more python3-ish
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
2015-07-27 02:29:56 +00:00
|
|
|
|
import base64
|
2014-11-14 22:14:08 +00:00
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import random
|
2016-01-20 20:26:45 +00:00
|
|
|
|
import re
|
2015-07-27 02:29:56 +00:00
|
|
|
|
import stat
|
2014-11-14 22:14:08 +00:00
|
|
|
|
import tempfile
|
|
|
|
|
import time
|
2015-10-22 23:07:26 +00:00
|
|
|
|
from abc import ABCMeta, abstractmethod
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
from ansible import constants as C
|
2017-04-21 20:17:12 +00:00
|
|
|
|
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleActionSkip, AnsibleActionFail
|
2017-06-27 05:58:09 +00:00
|
|
|
|
from ansible.executor.module_common import modify_module
|
2016-10-02 15:03:42 +00:00
|
|
|
|
from ansible.module_utils.json_utils import _filter_non_json_lines
|
2017-03-23 20:35:05 +00:00
|
|
|
|
from ansible.module_utils.six import binary_type, string_types, text_type, iteritems, with_metaclass
|
|
|
|
|
from ansible.module_utils.six.moves import shlex_quote
|
|
|
|
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
2014-11-14 22:14:08 +00:00
|
|
|
|
from ansible.parsing.utils.jsonify import jsonify
|
2016-09-07 05:54:17 +00:00
|
|
|
|
from ansible.release import __version__
|
2017-04-26 22:09:36 +00:00
|
|
|
|
from ansible.utils.unsafe_proxy import wrap_var
|
2017-10-29 04:33:02 +00:00
|
|
|
|
from ansible.vars.clean import remove_internal_keys
|
2016-09-07 05:54:17 +00:00
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2015-07-23 14:24:50 +00:00
|
|
|
|
try:
|
|
|
|
|
from __main__ import display
|
|
|
|
|
except ImportError:
|
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
|
display = Display()
|
|
|
|
|
|
2015-11-11 16:29:37 +00:00
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
class ActionBase(with_metaclass(ABCMeta, object)):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
This class is the base class for all action plugins, and defines
|
|
|
|
|
code common to all actions. The base class handles the connection
|
|
|
|
|
by putting/getting files and executing commands based on the current
|
|
|
|
|
action in use.
|
|
|
|
|
'''
|
|
|
|
|
|
2018-08-30 13:40:36 +00:00
|
|
|
|
# A set of valid arguments
|
|
|
|
|
_VALID_ARGS = frozenset([])
|
|
|
|
|
|
2015-07-21 16:12:22 +00:00
|
|
|
|
def __init__(self, task, connection, play_context, loader, templar, shared_loader_obj):
|
2017-06-02 11:14:11 +00:00
|
|
|
|
self._task = task
|
|
|
|
|
self._connection = connection
|
|
|
|
|
self._play_context = play_context
|
|
|
|
|
self._loader = loader
|
|
|
|
|
self._templar = templar
|
2015-05-02 04:48:11 +00:00
|
|
|
|
self._shared_loader_obj = shared_loader_obj
|
2017-06-02 11:14:11 +00:00
|
|
|
|
self._cleanup_remote_tmp = False
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2015-05-11 16:22:41 +00:00
|
|
|
|
self._supports_check_mode = True
|
2017-06-02 11:14:11 +00:00
|
|
|
|
self._supports_async = False
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# Backwards compat: self._display isn't really needed, just import the global display and use that.
|
|
|
|
|
self._display = display
|
|
|
|
|
|
2018-08-02 17:30:57 +00:00
|
|
|
|
self._used_interpreter = None
|
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
@abstractmethod
|
|
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
|
""" Action Plugins should implement this method to perform their
|
|
|
|
|
tasks. Everything else in this base class is a helper method for the
|
|
|
|
|
action plugin to do that.
|
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
:kwarg tmp: Deprecated parameter. This is no longer used. An action plugin that calls
|
|
|
|
|
another one and wants to use the same remote tmp for both should set
|
2018-02-15 17:01:02 +00:00
|
|
|
|
self._connection._shell.tmpdir rather than this parameter.
|
2015-10-22 23:07:26 +00:00
|
|
|
|
:kwarg task_vars: The variables (host vars, group vars, config vars,
|
|
|
|
|
etc) associated with this task.
|
|
|
|
|
:returns: dictionary of results from the module
|
|
|
|
|
|
|
|
|
|
Implementors of action modules may find the following variables especially useful:
|
|
|
|
|
|
|
|
|
|
* Module parameters. These are stored in self._task.args
|
|
|
|
|
"""
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2017-04-19 15:35:25 +00:00
|
|
|
|
result = {}
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
if tmp is not None:
|
|
|
|
|
result['warning'] = ['ActionModule.run() no longer honors the tmp parameter. Action'
|
2018-02-15 17:01:02 +00:00
|
|
|
|
' plugins should set self._connection._shell.tmpdir to share'
|
|
|
|
|
' the tmpdir']
|
2018-02-07 23:11:36 +00:00
|
|
|
|
del tmp
|
|
|
|
|
|
2017-11-22 20:35:58 +00:00
|
|
|
|
if self._task.async_val and not self._supports_async:
|
2017-04-21 20:17:12 +00:00
|
|
|
|
raise AnsibleActionFail('async is not supported for this task.')
|
2016-12-14 17:52:18 +00:00
|
|
|
|
elif self._play_context.check_mode and not self._supports_check_mode:
|
2017-04-21 20:17:12 +00:00
|
|
|
|
raise AnsibleActionSkip('check mode is not supported for this task.')
|
2017-11-22 20:35:58 +00:00
|
|
|
|
elif self._task.async_val and self._play_context.check_mode:
|
2017-04-21 20:17:12 +00:00
|
|
|
|
raise AnsibleActionFail('check mode and async cannot be used on same task.')
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2018-08-30 13:40:36 +00:00
|
|
|
|
# Error if invalid argument is passed
|
|
|
|
|
if self._VALID_ARGS:
|
|
|
|
|
task_opts = frozenset(self._task.args.keys())
|
|
|
|
|
bad_opts = task_opts.difference(self._VALID_ARGS)
|
|
|
|
|
if bad_opts:
|
|
|
|
|
raise AnsibleActionFail('Invalid options for %s: %s' % (self._task.action, ','.join(list(bad_opts))))
|
|
|
|
|
|
2018-02-15 17:01:02 +00:00
|
|
|
|
if self._connection._shell.tmpdir is None and self._early_needs_tmp_path():
|
2018-01-16 05:15:04 +00:00
|
|
|
|
self._make_tmp_path()
|
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
return result
|
2015-10-22 23:07:26 +00:00
|
|
|
|
|
2016-03-25 15:13:44 +00:00
|
|
|
|
def _remote_file_exists(self, path):
|
|
|
|
|
cmd = self._connection._shell.exists(path)
|
|
|
|
|
result = self._low_level_execute_command(cmd=cmd, sudoable=True)
|
|
|
|
|
if result['rc'] == 0:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
def _configure_module(self, module_name, module_args, task_vars=None):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
|
|
|
|
Handles the loading and templating of the module code through the
|
2015-02-10 20:35:34 +00:00
|
|
|
|
modify_module() function.
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2015-10-22 23:07:26 +00:00
|
|
|
|
if task_vars is None:
|
|
|
|
|
task_vars = dict()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
# Search module path(s) for named module.
|
2015-09-10 19:55:59 +00:00
|
|
|
|
for mod_type in self._connection.module_implementation_preferences:
|
|
|
|
|
# Check to determine if PowerShell modules are supported, and apply
|
|
|
|
|
# some fixes (hacks) to module name + args.
|
|
|
|
|
if mod_type == '.ps1':
|
|
|
|
|
# win_stat, win_file, and win_copy are not just like their
|
|
|
|
|
# python counterparts but they are compatible enough for our
|
|
|
|
|
# internal usage
|
|
|
|
|
if module_name in ('stat', 'file', 'copy') and self._task.action != module_name:
|
|
|
|
|
module_name = 'win_%s' % module_name
|
|
|
|
|
|
|
|
|
|
# Remove extra quotes surrounding path parameters before sending to module.
|
|
|
|
|
if module_name in ('win_stat', 'win_file', 'win_copy', 'slurp') and module_args and hasattr(self._connection._shell, '_unquote'):
|
|
|
|
|
for key in ('src', 'dest', 'path'):
|
|
|
|
|
if key in module_args:
|
|
|
|
|
module_args[key] = self._connection._shell._unquote(module_args[key])
|
|
|
|
|
|
|
|
|
|
module_path = self._shared_loader_obj.module_loader.find_plugin(module_name, mod_type)
|
|
|
|
|
if module_path:
|
|
|
|
|
break
|
2015-12-13 13:54:57 +00:00
|
|
|
|
else: # This is a for-else: http://bit.ly/1ElPkyg
|
2015-07-24 16:39:54 +00:00
|
|
|
|
# Use Windows version of ping module to check module paths when
|
2015-10-22 20:03:37 +00:00
|
|
|
|
# using a connection that supports .ps1 suffixes. We check specifically
|
|
|
|
|
# for win_ping here, otherwise the code would look for ping.ps1
|
2015-09-10 19:55:59 +00:00
|
|
|
|
if '.ps1' in self._connection.module_implementation_preferences:
|
2015-07-24 16:39:54 +00:00
|
|
|
|
ping_module = 'win_ping'
|
|
|
|
|
else:
|
|
|
|
|
ping_module = 'ping'
|
2015-09-10 19:55:59 +00:00
|
|
|
|
module_path2 = self._shared_loader_obj.module_loader.find_plugin(ping_module, self._connection.module_implementation_preferences)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
if module_path2 is not None:
|
|
|
|
|
raise AnsibleError("The module %s was not found in configured module paths" % (module_name))
|
|
|
|
|
else:
|
2015-11-11 16:29:37 +00:00
|
|
|
|
raise AnsibleError("The module %s was not found in configured module paths. "
|
|
|
|
|
"Additionally, core modules are missing. If this is a checkout, "
|
2017-01-09 17:11:39 +00:00
|
|
|
|
"run 'git pull --rebase' to correct this problem." % (module_name))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
# insert shared code and arguments into the module
|
2017-06-27 05:58:09 +00:00
|
|
|
|
final_environment = dict()
|
|
|
|
|
self._compute_environment_string(final_environment)
|
|
|
|
|
|
module_common: handle None value for templar (#36651)
* module_common: set required parameter templar
Fix the following error (related to b455901):
$ ./hacking/test-module -m ./lib/ansible/modules/system/ping.py -I ansible_python_interpreter=/usr/bin/python
Traceback (most recent call last):
File "./hacking/test-module", line 268, in <module>
main()
File "./hacking/test-module", line 249, in main
(modfile, modname, module_style) = boilerplate_module(options.module_path, options.module_args, interpreters, options.check, options.filename)
File "./hacking/test-module", line 152, in boilerplate_module
task_vars=task_vars
File "ansible/lib/ansible/executor/module_common.py", line 910, in modify_module
environment=environment)
File "ansible/lib/ansible/executor/module_common.py", line 736, in _find_module_utils
shebang, interpreter = _get_shebang(u'/usr/bin/python', task_vars, templar)
File "ansible/lib/ansible/executor/module_common.py", line 452, in _get_shebang
interpreter = templar.template(task_vars[interpreter_config].strip())
AttributeError: 'NoneType' object has no attribute 'template'
* module_common.modify_module: templar is required
2018-03-29 17:54:48 +00:00
|
|
|
|
(module_data, module_style, module_shebang) = modify_module(module_name, module_path, module_args, self._templar,
|
|
|
|
|
task_vars=task_vars,
|
2017-12-08 14:59:24 +00:00
|
|
|
|
module_compression=self._play_context.module_compression,
|
|
|
|
|
async_timeout=self._task.async_val,
|
|
|
|
|
become=self._play_context.become,
|
|
|
|
|
become_method=self._play_context.become_method,
|
|
|
|
|
become_user=self._play_context.become_user,
|
2017-06-27 05:58:09 +00:00
|
|
|
|
become_password=self._play_context.become_pass,
|
2018-01-19 21:58:10 +00:00
|
|
|
|
become_flags=self._play_context.become_flags,
|
2017-06-27 05:58:09 +00:00
|
|
|
|
environment=final_environment)
|
2017-02-17 08:09:56 +00:00
|
|
|
|
|
2016-05-11 20:14:01 +00:00
|
|
|
|
return (module_style, module_shebang, module_data, module_path)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2017-09-12 07:11:13 +00:00
|
|
|
|
def _compute_environment_string(self, raw_environment_out=None):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
|
|
|
|
Builds the environment string to be used when executing the remote task.
|
|
|
|
|
'''
|
|
|
|
|
|
2015-07-21 17:52:51 +00:00
|
|
|
|
final_environment = dict()
|
|
|
|
|
if self._task.environment is not None:
|
|
|
|
|
environments = self._task.environment
|
|
|
|
|
if not isinstance(environments, list):
|
2017-06-02 11:14:11 +00:00
|
|
|
|
environments = [environments]
|
2015-07-21 17:52:51 +00:00
|
|
|
|
|
2017-12-08 15:13:22 +00:00
|
|
|
|
# The order of environments matters to make sure we merge
|
|
|
|
|
# in the parent's values first so those in the block then
|
|
|
|
|
# task 'win' in precedence
|
2015-07-21 17:52:51 +00:00
|
|
|
|
for environment in environments:
|
2017-06-27 05:58:09 +00:00
|
|
|
|
if environment is None or len(environment) == 0:
|
2015-08-25 14:15:32 +00:00
|
|
|
|
continue
|
2015-12-17 14:44:40 +00:00
|
|
|
|
temp_environment = self._templar.template(environment)
|
|
|
|
|
if not isinstance(temp_environment, dict):
|
|
|
|
|
raise AnsibleError("environment must be a dictionary, received %s (%s)" % (temp_environment, type(temp_environment)))
|
2015-10-26 21:04:28 +00:00
|
|
|
|
# very deliberately using update here instead of combine_vars, as
|
2015-07-21 17:52:51 +00:00
|
|
|
|
# these environment settings should not need to merge sub-dicts
|
2015-12-17 14:44:40 +00:00
|
|
|
|
final_environment.update(temp_environment)
|
2015-07-21 17:52:51 +00:00
|
|
|
|
|
2017-06-27 05:58:09 +00:00
|
|
|
|
if len(final_environment) > 0:
|
|
|
|
|
final_environment = self._templar.template(final_environment)
|
2017-03-24 00:48:15 +00:00
|
|
|
|
|
|
|
|
|
if isinstance(raw_environment_out, dict):
|
|
|
|
|
raw_environment_out.clear()
|
|
|
|
|
raw_environment_out.update(final_environment)
|
|
|
|
|
|
2015-07-21 17:52:51 +00:00
|
|
|
|
return self._connection._shell.env_prefix(**final_environment)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
def _early_needs_tmp_path(self):
|
|
|
|
|
'''
|
2018-02-15 17:01:02 +00:00
|
|
|
|
Determines if a tmp path should be created before the action is executed.
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
return getattr(self, 'TRANSFERS_FILES', False)
|
2015-04-13 16:35:20 +00:00
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
def _is_pipelining_enabled(self, module_style, wrap_async=False):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2016-12-14 17:52:18 +00:00
|
|
|
|
Determines if we are required and can do pipelining
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2017-02-17 08:09:56 +00:00
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
# any of these require a true
|
|
|
|
|
for condition in [
|
2017-03-23 18:40:02 +00:00
|
|
|
|
self._connection.has_pipelining,
|
2017-06-27 05:58:09 +00:00
|
|
|
|
self._play_context.pipelining or self._connection.always_pipeline_modules, # pipelining enabled for play or connection requires it (eg winrm)
|
2017-03-23 18:40:02 +00:00
|
|
|
|
module_style == "new", # old style modules do not support pipelining
|
|
|
|
|
not C.DEFAULT_KEEP_REMOTE_FILES, # user wants remote files
|
2018-01-04 02:51:53 +00:00
|
|
|
|
not wrap_async or self._connection.always_pipeline_modules, # async does not normally support pipelining unless it does (eg winrm)
|
2017-03-23 18:40:02 +00:00
|
|
|
|
self._play_context.become_method != 'su', # su does not work with pipelining,
|
|
|
|
|
# FIXME: we might need to make become_method exclusion a configurable list
|
2017-03-22 02:19:40 +00:00
|
|
|
|
]:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
if not condition:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
2018-07-06 17:49:19 +00:00
|
|
|
|
def _get_admin_users(self):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2018-07-06 17:49:19 +00:00
|
|
|
|
Returns a list of admin users that are configured for the current shell
|
|
|
|
|
plugin
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2018-07-06 17:49:19 +00:00
|
|
|
|
try:
|
|
|
|
|
admin_users = self._connection._shell.get_option('admin_users')
|
|
|
|
|
except AnsibleError:
|
|
|
|
|
# fallback for old custom plugins w/o get_option
|
|
|
|
|
admin_users = ['root']
|
|
|
|
|
return admin_users
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2018-07-06 17:49:19 +00:00
|
|
|
|
def _is_become_unprivileged(self):
|
|
|
|
|
'''
|
|
|
|
|
The user is not the same as the connection user and is not part of the
|
|
|
|
|
shell configured admin users
|
|
|
|
|
'''
|
|
|
|
|
# if we don't use become then we know we aren't switching to a
|
|
|
|
|
# different unprivileged user
|
|
|
|
|
if not self._play_context.become:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# if we use become and the user is not an admin (or same user) then
|
|
|
|
|
# we need to return become_unprivileged as True
|
|
|
|
|
admin_users = self._get_admin_users()
|
2018-01-16 05:15:04 +00:00
|
|
|
|
try:
|
2018-07-06 17:49:19 +00:00
|
|
|
|
remote_user = self._connection.get_option('remote_user')
|
2018-04-27 04:16:19 +00:00
|
|
|
|
except AnsibleError:
|
2018-07-06 17:49:19 +00:00
|
|
|
|
remote_user = self._play_context.remote_user
|
|
|
|
|
return bool(self._play_context.become_user not in admin_users + [remote_user])
|
|
|
|
|
|
|
|
|
|
def _make_tmp_path(self, remote_user=None):
|
|
|
|
|
'''
|
|
|
|
|
Create and return a temporary path on a remote box.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
become_unprivileged = self._is_become_unprivileged()
|
2018-01-16 05:15:04 +00:00
|
|
|
|
try:
|
2018-02-15 17:01:02 +00:00
|
|
|
|
remote_tmp = self._connection._shell.get_option('remote_tmp')
|
2018-04-27 04:16:19 +00:00
|
|
|
|
except AnsibleError:
|
2018-05-14 23:31:21 +00:00
|
|
|
|
remote_tmp = '~/.ansible/tmp'
|
2017-01-25 18:09:36 +00:00
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# deal with tmpdir creation
|
|
|
|
|
basefile = 'ansible-tmp-%s-%s' % (time.time(), random.randint(0, 2**48))
|
2018-05-17 22:21:30 +00:00
|
|
|
|
# Network connection plugins (network_cli, netconf, etc.) execute on the controller, rather than the remote host.
|
|
|
|
|
# As such, we want to avoid using remote_user for paths as remote_user may not line up with the local user
|
|
|
|
|
# This is a hack and should be solved by more intelligent handling of remote_tmp in 2.7
|
|
|
|
|
if getattr(self._connection, '_remote_is_local', False):
|
|
|
|
|
tmpdir = C.DEFAULT_LOCAL_TMP
|
|
|
|
|
else:
|
|
|
|
|
tmpdir = self._remote_expand_user(remote_tmp, sudoable=False)
|
2018-07-06 17:49:19 +00:00
|
|
|
|
cmd = self._connection._shell.mkdtemp(basefile=basefile, system=become_unprivileged, tmpdir=tmpdir)
|
2015-09-24 20:29:36 +00:00
|
|
|
|
result = self._low_level_execute_command(cmd, sudoable=False)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
# error handling on this seems a little aggressive?
|
|
|
|
|
if result['rc'] != 0:
|
|
|
|
|
if result['rc'] == 5:
|
|
|
|
|
output = 'Authentication failure.'
|
2015-04-15 23:32:44 +00:00
|
|
|
|
elif result['rc'] == 255 and self._connection.transport in ('ssh',):
|
2015-06-06 04:16:35 +00:00
|
|
|
|
|
2015-07-21 16:12:22 +00:00
|
|
|
|
if self._play_context.verbosity > 3:
|
2015-10-06 21:10:25 +00:00
|
|
|
|
output = u'SSH encountered an unknown error. The output was:\n%s%s' % (result['stdout'], result['stderr'])
|
2015-06-06 04:16:35 +00:00
|
|
|
|
else:
|
2017-06-02 11:14:11 +00:00
|
|
|
|
output = (u'SSH encountered an unknown error during the connection. '
|
|
|
|
|
'We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue')
|
2015-06-06 04:16:35 +00:00
|
|
|
|
|
2015-10-06 21:10:25 +00:00
|
|
|
|
elif u'No space left on device' in result['stderr']:
|
2014-11-14 22:14:08 +00:00
|
|
|
|
output = result['stderr']
|
|
|
|
|
else:
|
2017-06-02 11:14:11 +00:00
|
|
|
|
output = ('Authentication or permission failure. '
|
|
|
|
|
'In some cases, you may have been able to authenticate and did not have permissions on the target directory. '
|
2018-02-15 17:01:02 +00:00
|
|
|
|
'Consider changing the remote tmp path in ansible.cfg to a path rooted in "/tmp". '
|
2017-06-02 11:14:11 +00:00
|
|
|
|
'Failed command was: %s, exited with result %d' % (cmd, result['rc']))
|
2015-10-06 21:10:25 +00:00
|
|
|
|
if 'stdout' in result and result['stdout'] != u'':
|
2017-06-28 19:55:39 +00:00
|
|
|
|
output = output + u", stdout output: %s" % result['stdout']
|
|
|
|
|
if self._play_context.verbosity > 3 and 'stderr' in result and result['stderr'] != u'':
|
|
|
|
|
output += u", stderr output: %s" % result['stderr']
|
2015-09-09 19:26:40 +00:00
|
|
|
|
raise AnsibleConnectionFailure(output)
|
2016-12-14 17:52:18 +00:00
|
|
|
|
else:
|
|
|
|
|
self._cleanup_remote_tmp = True
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2015-11-30 17:16:33 +00:00
|
|
|
|
try:
|
2016-04-08 15:18:35 +00:00
|
|
|
|
stdout_parts = result['stdout'].strip().split('%s=' % basefile, 1)
|
|
|
|
|
rc = self._connection._shell.join_path(stdout_parts[-1], u'').splitlines()[-1]
|
2015-11-30 17:16:33 +00:00
|
|
|
|
except IndexError:
|
|
|
|
|
# stdout was empty or just space, set to / to trigger error in next if
|
|
|
|
|
rc = '/'
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
# Catch failure conditions, files should never be
|
|
|
|
|
# written to locations in /.
|
|
|
|
|
if rc == '/':
|
2015-04-14 23:13:27 +00:00
|
|
|
|
raise AnsibleError('failed to resolve remote temporary directory from %s: `%s` returned empty string' % (basefile, cmd))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2018-02-15 17:01:02 +00:00
|
|
|
|
self._connection._shell.tmpdir = rc
|
2018-01-16 05:15:04 +00:00
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
|
return rc
|
|
|
|
|
|
2016-09-07 00:38:12 +00:00
|
|
|
|
def _should_remove_tmp_path(self, tmp_path):
|
|
|
|
|
'''Determine if temporary path should be deleted or kept by user request/config'''
|
|
|
|
|
return tmp_path and self._cleanup_remote_tmp and not C.DEFAULT_KEEP_REMOTE_FILES and "-tmp-" in tmp_path
|
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
|
def _remove_tmp_path(self, tmp_path):
|
|
|
|
|
'''Remove a temporary path we created. '''
|
|
|
|
|
|
2018-02-15 17:01:02 +00:00
|
|
|
|
if tmp_path is None and self._connection._shell.tmpdir:
|
|
|
|
|
tmp_path = self._connection._shell.tmpdir
|
2018-02-07 21:11:32 +00:00
|
|
|
|
|
2016-09-07 00:38:12 +00:00
|
|
|
|
if self._should_remove_tmp_path(tmp_path):
|
2015-06-29 19:41:51 +00:00
|
|
|
|
cmd = self._connection._shell.remove(tmp_path, recurse=True)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
# If we have gotten here we have a working ssh configuration.
|
|
|
|
|
# If ssh breaks we could leave tmp directories out on the remote system.
|
2016-12-14 17:52:18 +00:00
|
|
|
|
tmp_rm_res = self._low_level_execute_command(cmd, sudoable=False)
|
|
|
|
|
|
2018-08-10 17:27:12 +00:00
|
|
|
|
if tmp_rm_res.get('rc', 0) != 0:
|
2017-06-02 11:14:11 +00:00
|
|
|
|
display.warning('Error deleting remote temporary files (rc: %s, stderr: %s})'
|
|
|
|
|
% (tmp_rm_res.get('rc'), tmp_rm_res.get('stderr', 'No error string available.')))
|
2018-02-07 23:11:36 +00:00
|
|
|
|
else:
|
2018-02-15 17:01:02 +00:00
|
|
|
|
self._connection._shell.tmpdir = None
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2016-03-21 21:17:53 +00:00
|
|
|
|
def _transfer_file(self, local_path, remote_path):
|
|
|
|
|
self._connection.put_file(local_path, remote_path)
|
|
|
|
|
return remote_path
|
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
|
def _transfer_data(self, remote_path, data):
|
|
|
|
|
'''
|
|
|
|
|
Copies the module data out to the temporary module path.
|
|
|
|
|
'''
|
|
|
|
|
|
2015-05-11 16:22:41 +00:00
|
|
|
|
if isinstance(data, dict):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
data = jsonify(data)
|
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
afd, afile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP)
|
2016-06-04 23:19:57 +00:00
|
|
|
|
afo = os.fdopen(afd, 'wb')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
try:
|
2016-09-07 05:54:17 +00:00
|
|
|
|
data = to_bytes(data, errors='surrogate_or_strict')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
afo.write(data)
|
2015-04-13 16:35:20 +00:00
|
|
|
|
except Exception as e:
|
2016-09-07 05:54:17 +00:00
|
|
|
|
raise AnsibleError("failure writing module data to temporary file for transfer: %s" % to_native(e))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
afo.flush()
|
|
|
|
|
afo.close()
|
|
|
|
|
|
|
|
|
|
try:
|
2016-03-21 21:17:53 +00:00
|
|
|
|
self._transfer_file(afile, remote_path)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
finally:
|
|
|
|
|
os.unlink(afile)
|
|
|
|
|
|
|
|
|
|
return remote_path
|
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
def _fixup_perms2(self, remote_paths, remote_user=None, execute=True):
|
2016-03-21 21:17:53 +00:00
|
|
|
|
"""
|
2016-04-22 15:40:34 +00:00
|
|
|
|
We need the files we upload to be readable (and sometimes executable)
|
|
|
|
|
by the user being sudo'd to but we want to limit other people's access
|
|
|
|
|
(because the files could contain passwords or other private
|
|
|
|
|
information. We achieve this in one of these ways:
|
|
|
|
|
|
|
|
|
|
* If no sudo is performed or the remote_user is sudo'ing to
|
2016-06-15 15:16:24 +00:00
|
|
|
|
themselves, we don't have to change permissions.
|
2016-04-22 15:40:34 +00:00
|
|
|
|
* If the remote_user sudo's to a privileged user (for instance, root),
|
|
|
|
|
we don't have to change permissions
|
2016-06-15 15:16:24 +00:00
|
|
|
|
* If the remote_user sudo's to an unprivileged user then we attempt to
|
|
|
|
|
grant the unprivileged user access via file system acls.
|
|
|
|
|
* If granting file system acls fails we try to change the owner of the
|
|
|
|
|
file with chown which only works in case the remote_user is
|
|
|
|
|
privileged or the remote systems allows chown calls by unprivileged
|
|
|
|
|
users (e.g. HP-UX)
|
|
|
|
|
* If the chown fails we can set the file to be world readable so that
|
|
|
|
|
the second unprivileged user can read the file.
|
2016-04-22 15:40:34 +00:00
|
|
|
|
Since this could allow other users to get access to private
|
2018-01-22 23:35:33 +00:00
|
|
|
|
information we only do this if ansible is configured with
|
2016-04-22 15:40:34 +00:00
|
|
|
|
"allow_world_readable_tmpfiles" in the ansible.cfg
|
2016-03-21 21:17:53 +00:00
|
|
|
|
"""
|
2016-12-14 17:52:18 +00:00
|
|
|
|
if remote_user is None:
|
|
|
|
|
remote_user = self._play_context.remote_user
|
|
|
|
|
|
2016-04-07 14:27:01 +00:00
|
|
|
|
if self._connection._shell.SHELL_FAMILY == 'powershell':
|
|
|
|
|
# This won't work on Powershell as-is, so we'll just completely skip until
|
|
|
|
|
# we have a need for it, at which point we'll have to do something different.
|
2016-08-06 01:40:28 +00:00
|
|
|
|
return remote_paths
|
2016-03-21 21:17:53 +00:00
|
|
|
|
|
2018-07-06 17:49:19 +00:00
|
|
|
|
if self._is_become_unprivileged():
|
2016-03-21 21:17:53 +00:00
|
|
|
|
# Unprivileged user that's different than the ssh user. Let's get
|
|
|
|
|
# to work!
|
2016-04-22 15:40:34 +00:00
|
|
|
|
|
2016-06-15 15:16:24 +00:00
|
|
|
|
# Try to use file system acls to make the files readable for sudo'd
|
|
|
|
|
# user
|
|
|
|
|
if execute:
|
2016-11-22 20:16:04 +00:00
|
|
|
|
chmod_mode = 'rx'
|
|
|
|
|
setfacl_mode = 'r-x'
|
2016-06-15 15:16:24 +00:00
|
|
|
|
else:
|
2016-11-22 20:16:04 +00:00
|
|
|
|
chmod_mode = 'rX'
|
2017-06-02 11:14:11 +00:00
|
|
|
|
# NOTE: this form fails silently on freebsd. We currently
|
2016-11-21 20:09:23 +00:00
|
|
|
|
# never call _fixup_perms2() with execute=False but if we
|
|
|
|
|
# start to we'll have to fix this.
|
2016-11-22 20:16:04 +00:00
|
|
|
|
setfacl_mode = 'r-X'
|
2016-06-15 15:16:24 +00:00
|
|
|
|
|
2016-11-22 20:16:04 +00:00
|
|
|
|
res = self._remote_set_user_facl(remote_paths, self._play_context.become_user, setfacl_mode)
|
2016-06-15 15:16:24 +00:00
|
|
|
|
if res['rc'] != 0:
|
|
|
|
|
# File system acls failed; let's try to use chown next
|
|
|
|
|
# Set executable bit first as on some systems an
|
|
|
|
|
# unprivileged user can use chown
|
2016-03-21 21:17:53 +00:00
|
|
|
|
if execute:
|
2016-08-06 01:40:28 +00:00
|
|
|
|
res = self._remote_chmod(remote_paths, 'u+x')
|
2016-03-29 05:07:14 +00:00
|
|
|
|
if res['rc'] != 0:
|
2016-11-22 19:19:47 +00:00
|
|
|
|
raise AnsibleError('Failed to set file mode on remote temporary files (rc: {0}, err: {1})'.format(res['rc'], to_native(res['stderr'])))
|
2016-04-22 15:40:34 +00:00
|
|
|
|
|
2016-08-06 01:40:28 +00:00
|
|
|
|
res = self._remote_chown(remote_paths, self._play_context.become_user)
|
2018-07-06 17:49:19 +00:00
|
|
|
|
if res['rc'] != 0 and remote_user in self._get_admin_users():
|
|
|
|
|
# chown failed even if remote_user is administrator/root
|
2018-01-16 05:15:04 +00:00
|
|
|
|
raise AnsibleError('Failed to change ownership of the temporary files Ansible needs to create despite connecting as a privileged user. '
|
2017-06-02 11:14:11 +00:00
|
|
|
|
'Unprivileged become user would be unable to read the file.')
|
2016-06-15 15:16:24 +00:00
|
|
|
|
elif res['rc'] != 0:
|
2018-01-22 23:35:33 +00:00
|
|
|
|
if C.ALLOW_WORLD_READABLE_TMPFILES:
|
2016-06-15 15:16:24 +00:00
|
|
|
|
# chown and fs acls failed -- do things this insecure
|
|
|
|
|
# way only if the user opted in in the config file
|
2017-06-02 11:14:11 +00:00
|
|
|
|
display.warning('Using world-readable permissions for temporary files Ansible needs to create when becoming an unprivileged user. '
|
|
|
|
|
'This may be insecure. For information on securing this, see '
|
|
|
|
|
'https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user')
|
2016-11-22 20:16:04 +00:00
|
|
|
|
res = self._remote_chmod(remote_paths, 'a+%s' % chmod_mode)
|
2016-03-29 05:07:14 +00:00
|
|
|
|
if res['rc'] != 0:
|
2016-11-22 19:19:47 +00:00
|
|
|
|
raise AnsibleError('Failed to set file mode on remote files (rc: {0}, err: {1})'.format(res['rc'], to_native(res['stderr'])))
|
2016-03-21 21:17:53 +00:00
|
|
|
|
else:
|
2017-06-02 11:14:11 +00:00
|
|
|
|
raise AnsibleError('Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user '
|
|
|
|
|
'(rc: %s, err: %s}). For information on working around this, see '
|
|
|
|
|
'https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user'
|
|
|
|
|
% (res['rc'], to_native(res['stderr'])))
|
2016-03-21 21:17:53 +00:00
|
|
|
|
elif execute:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
# Can't depend on the file being transferred with execute permissions.
|
|
|
|
|
# Only need user perms because no become was used here
|
2016-08-06 01:40:28 +00:00
|
|
|
|
res = self._remote_chmod(remote_paths, 'u+x')
|
2016-03-29 05:07:14 +00:00
|
|
|
|
if res['rc'] != 0:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
raise AnsibleError('Failed to set execute bit on remote files (rc: {0}, err: {1})'.format(res['rc'], to_native(res['stderr'])))
|
2016-03-21 21:17:53 +00:00
|
|
|
|
|
2016-08-06 01:40:28 +00:00
|
|
|
|
return remote_paths
|
2016-03-21 21:17:53 +00:00
|
|
|
|
|
2016-08-06 01:40:28 +00:00
|
|
|
|
def _remote_chmod(self, paths, mode, sudoable=False):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
|
|
|
|
Issue a remote chmod command
|
|
|
|
|
'''
|
2016-08-06 01:40:28 +00:00
|
|
|
|
cmd = self._connection._shell.chmod(paths, mode)
|
2016-03-21 21:17:53 +00:00
|
|
|
|
res = self._low_level_execute_command(cmd, sudoable=sudoable)
|
|
|
|
|
return res
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2016-08-06 01:40:28 +00:00
|
|
|
|
def _remote_chown(self, paths, user, sudoable=False):
|
2016-03-21 21:17:53 +00:00
|
|
|
|
'''
|
|
|
|
|
Issue a remote chown command
|
|
|
|
|
'''
|
2016-08-06 01:40:28 +00:00
|
|
|
|
cmd = self._connection._shell.chown(paths, user)
|
2016-03-21 21:17:53 +00:00
|
|
|
|
res = self._low_level_execute_command(cmd, sudoable=sudoable)
|
|
|
|
|
return res
|
|
|
|
|
|
2016-08-06 01:40:28 +00:00
|
|
|
|
def _remote_set_user_facl(self, paths, user, mode, sudoable=False):
|
2016-03-21 21:17:53 +00:00
|
|
|
|
'''
|
|
|
|
|
Issue a remote call to setfacl
|
|
|
|
|
'''
|
2016-08-06 01:40:28 +00:00
|
|
|
|
cmd = self._connection._shell.set_user_facl(paths, user, mode)
|
2015-09-24 20:29:36 +00:00
|
|
|
|
res = self._low_level_execute_command(cmd, sudoable=sudoable)
|
2015-02-09 22:54:44 +00:00
|
|
|
|
return res
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2017-02-25 02:10:09 +00:00
|
|
|
|
def _execute_remote_stat(self, path, all_vars, follow, tmp=None, checksum=True):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2016-02-15 22:11:49 +00:00
|
|
|
|
Get information from remote file.
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2018-02-07 23:11:36 +00:00
|
|
|
|
if tmp is not None:
|
|
|
|
|
display.warning('_execute_remote_stat no longer honors the tmp parameter. Action'
|
2018-02-15 17:01:02 +00:00
|
|
|
|
' plugins should set self._connection._shell.tmpdir to share'
|
|
|
|
|
' the tmpdir')
|
2018-02-07 23:11:36 +00:00
|
|
|
|
del tmp # No longer used
|
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
module_args = dict(
|
2017-01-29 07:28:53 +00:00
|
|
|
|
path=path,
|
|
|
|
|
follow=follow,
|
2017-02-25 02:10:09 +00:00
|
|
|
|
get_checksum=checksum,
|
2017-01-29 07:28:53 +00:00
|
|
|
|
checksum_algo='sha1',
|
2016-02-15 22:11:49 +00:00
|
|
|
|
)
|
2018-02-07 23:11:36 +00:00
|
|
|
|
mystat = self._execute_module(module_name='stat', module_args=module_args, task_vars=all_vars,
|
2017-03-23 01:50:28 +00:00
|
|
|
|
wrap_async=False)
|
2016-02-15 22:11:49 +00:00
|
|
|
|
|
2016-12-18 18:55:21 +00:00
|
|
|
|
if mystat.get('failed'):
|
|
|
|
|
msg = mystat.get('module_stderr')
|
|
|
|
|
if not msg:
|
2017-01-30 23:01:47 +00:00
|
|
|
|
msg = mystat.get('module_stdout')
|
2016-12-18 18:55:21 +00:00
|
|
|
|
if not msg:
|
2017-01-30 23:01:47 +00:00
|
|
|
|
msg = mystat.get('msg')
|
2016-12-18 18:55:21 +00:00
|
|
|
|
raise AnsibleError('Failed to get information on remote file (%s): %s' % (path, msg))
|
2016-02-15 22:11:49 +00:00
|
|
|
|
|
|
|
|
|
if not mystat['stat']['exists']:
|
|
|
|
|
# empty might be matched, 1 should never match, also backwards compatible
|
|
|
|
|
mystat['stat']['checksum'] = '1'
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2016-02-23 20:06:37 +00:00
|
|
|
|
# happens sometimes when it is a dir and not on bsd
|
2016-09-07 05:54:17 +00:00
|
|
|
|
if 'checksum' not in mystat['stat']:
|
2016-02-23 20:06:37 +00:00
|
|
|
|
mystat['stat']['checksum'] = ''
|
2016-12-13 17:14:47 +00:00
|
|
|
|
elif not isinstance(mystat['stat']['checksum'], string_types):
|
|
|
|
|
raise AnsibleError("Invalid checksum returned by stat: expected a string type but got %s" % type(mystat['stat']['checksum']))
|
2016-02-23 20:06:37 +00:00
|
|
|
|
|
2016-02-15 22:11:49 +00:00
|
|
|
|
return mystat['stat']
|
2015-08-15 16:00:13 +00:00
|
|
|
|
|
2016-05-26 21:47:11 +00:00
|
|
|
|
def _remote_checksum(self, path, all_vars, follow=False):
|
2016-02-15 22:11:49 +00:00
|
|
|
|
'''
|
|
|
|
|
Produces a remote checksum given a path,
|
|
|
|
|
Returns a number 0-4 for specific errors instead of checksum, also ensures it is different
|
|
|
|
|
0 = unknown error
|
|
|
|
|
1 = file does not exist, this might not be an error
|
|
|
|
|
2 = permissions issue
|
|
|
|
|
3 = its a directory, not a file
|
|
|
|
|
4 = stat module failed, likely due to not finding python
|
2017-04-17 17:55:56 +00:00
|
|
|
|
5 = appropriate json module not found
|
2016-02-15 22:11:49 +00:00
|
|
|
|
'''
|
2017-03-21 22:11:11 +00:00
|
|
|
|
x = "0" # unknown error has occurred
|
2014-11-14 22:14:08 +00:00
|
|
|
|
try:
|
2016-05-26 21:47:11 +00:00
|
|
|
|
remote_stat = self._execute_remote_stat(path, all_vars, follow=follow)
|
2016-02-15 22:11:49 +00:00
|
|
|
|
if remote_stat['exists'] and remote_stat['isdir']:
|
2016-09-07 05:54:17 +00:00
|
|
|
|
x = "3" # its a directory not a file
|
2014-11-14 22:14:08 +00:00
|
|
|
|
else:
|
2016-09-07 05:54:17 +00:00
|
|
|
|
x = remote_stat['checksum'] # if 1, file is missing
|
2016-02-15 22:11:49 +00:00
|
|
|
|
except AnsibleError as e:
|
2016-09-07 05:54:17 +00:00
|
|
|
|
errormsg = to_text(e)
|
|
|
|
|
if errormsg.endswith(u'Permission denied'):
|
|
|
|
|
x = "2" # cannot read file
|
|
|
|
|
elif errormsg.endswith(u'MODULE FAILURE'):
|
|
|
|
|
x = "4" # python not found or module uncaught exception
|
2018-08-10 16:13:29 +00:00
|
|
|
|
elif 'json' in errormsg:
|
|
|
|
|
x = "5" # json module needed
|
2016-02-15 22:11:49 +00:00
|
|
|
|
finally:
|
2017-09-13 01:49:24 +00:00
|
|
|
|
return x # pylint: disable=lost-exception
|
2016-02-15 22:11:49 +00:00
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
def _remote_expand_user(self, path, sudoable=True, pathsep=None):
|
|
|
|
|
''' takes a remote path and performs tilde/$HOME expansion on the remote host '''
|
|
|
|
|
|
|
|
|
|
# We only expand ~/path and ~username/path
|
|
|
|
|
if not path.startswith('~'):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
return path
|
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# Per Jborean, we don't have to worry about Windows as we don't have a notion of user's home
|
|
|
|
|
# dir there.
|
2014-11-14 22:14:08 +00:00
|
|
|
|
split_path = path.split(os.path.sep, 1)
|
|
|
|
|
expand_path = split_path[0]
|
2018-01-16 05:15:04 +00:00
|
|
|
|
|
2018-05-02 17:15:37 +00:00
|
|
|
|
if expand_path == '~':
|
2018-05-17 22:21:30 +00:00
|
|
|
|
# Network connection plugins (network_cli, netconf, etc.) execute on the controller, rather than the remote host.
|
|
|
|
|
# As such, we want to avoid using remote_user for paths as remote_user may not line up with the local user
|
|
|
|
|
# This is a hack and should be solved by more intelligent handling of remote_tmp in 2.7
|
|
|
|
|
if getattr(self._connection, '_remote_is_local', False):
|
|
|
|
|
pass
|
|
|
|
|
elif sudoable and self._play_context.become and self._play_context.become_user:
|
2018-05-02 17:15:37 +00:00
|
|
|
|
expand_path = '~%s' % self._play_context.become_user
|
|
|
|
|
else:
|
|
|
|
|
# use remote user instead, if none set default to current user
|
2018-05-21 17:04:25 +00:00
|
|
|
|
expand_path = '~%s' % (self._play_context.remote_user or self._connection.default_user or '')
|
2017-01-26 20:18:10 +00:00
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# use shell to construct appropriate command and execute
|
2015-06-29 19:41:51 +00:00
|
|
|
|
cmd = self._connection._shell.expand_user(expand_path)
|
2015-09-24 20:29:36 +00:00
|
|
|
|
data = self._low_level_execute_command(cmd, sudoable=False)
|
2018-01-16 05:15:04 +00:00
|
|
|
|
|
2017-02-22 21:14:39 +00:00
|
|
|
|
try:
|
|
|
|
|
initial_fragment = data['stdout'].strip().splitlines()[-1]
|
|
|
|
|
except IndexError:
|
|
|
|
|
initial_fragment = None
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
if not initial_fragment:
|
2018-01-16 05:15:04 +00:00
|
|
|
|
# Something went wrong trying to expand the path remotely. Try using pwd, if not, return
|
2014-11-14 22:14:08 +00:00
|
|
|
|
# the original string
|
2018-01-16 05:15:04 +00:00
|
|
|
|
cmd = self._connection._shell.pwd()
|
|
|
|
|
pwd = self._low_level_execute_command(cmd, sudoable=False).get('stdout', '').strip()
|
|
|
|
|
if pwd:
|
|
|
|
|
expanded = pwd
|
|
|
|
|
else:
|
|
|
|
|
expanded = path
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
elif len(split_path) > 1:
|
|
|
|
|
expanded = self._connection._shell.join_path(initial_fragment, *split_path[1:])
|
2014-11-14 22:14:08 +00:00
|
|
|
|
else:
|
2018-01-16 05:15:04 +00:00
|
|
|
|
expanded = initial_fragment
|
|
|
|
|
|
|
|
|
|
return expanded
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2016-01-20 20:26:45 +00:00
|
|
|
|
def _strip_success_message(self, data):
|
|
|
|
|
'''
|
|
|
|
|
Removes the BECOME-SUCCESS message from the data.
|
|
|
|
|
'''
|
|
|
|
|
if data.strip().startswith('BECOME-SUCCESS-'):
|
|
|
|
|
data = re.sub(r'^((\r)?\n)?BECOME-SUCCESS.*(\r)?\n', '', data)
|
|
|
|
|
return data
|
|
|
|
|
|
2016-12-19 21:54:31 +00:00
|
|
|
|
def _update_module_args(self, module_name, module_args, task_vars):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2015-01-28 14:55:18 +00:00
|
|
|
|
# set check mode in the module arguments, if required
|
2016-01-07 06:37:19 +00:00
|
|
|
|
if self._play_context.check_mode:
|
2015-01-28 14:55:18 +00:00
|
|
|
|
if not self._supports_check_mode:
|
|
|
|
|
raise AnsibleError("check mode is not supported for this operation")
|
|
|
|
|
module_args['_ansible_check_mode'] = True
|
2016-01-07 06:37:19 +00:00
|
|
|
|
else:
|
|
|
|
|
module_args['_ansible_check_mode'] = False
|
2015-01-28 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
# set no log in the module arguments, if required
|
2016-01-07 06:37:19 +00:00
|
|
|
|
module_args['_ansible_no_log'] = self._play_context.no_log or C.DEFAULT_NO_TARGET_SYSLOG
|
2015-01-28 14:55:18 +00:00
|
|
|
|
|
2015-09-26 03:57:03 +00:00
|
|
|
|
# set debug in the module arguments, if required
|
2016-01-07 06:37:19 +00:00
|
|
|
|
module_args['_ansible_debug'] = C.DEFAULT_DEBUG
|
|
|
|
|
|
|
|
|
|
# let module know we are in diff mode
|
|
|
|
|
module_args['_ansible_diff'] = self._play_context.diff
|
|
|
|
|
|
|
|
|
|
# let module know our verbosity
|
2016-05-12 00:54:01 +00:00
|
|
|
|
module_args['_ansible_verbosity'] = display.verbosity
|
2015-09-26 03:57:03 +00:00
|
|
|
|
|
2016-05-13 03:30:05 +00:00
|
|
|
|
# give the module information about the ansible version
|
|
|
|
|
module_args['_ansible_version'] = __version__
|
|
|
|
|
|
2016-06-10 15:48:54 +00:00
|
|
|
|
# give the module information about its name
|
2016-12-19 21:54:31 +00:00
|
|
|
|
module_args['_ansible_module_name'] = module_name
|
2016-06-10 15:48:54 +00:00
|
|
|
|
|
2016-05-13 03:30:05 +00:00
|
|
|
|
# set the syslog facility to be used in the module
|
|
|
|
|
module_args['_ansible_syslog_facility'] = task_vars.get('ansible_syslog_facility', C.DEFAULT_SYSLOG_FACILITY)
|
|
|
|
|
|
|
|
|
|
# let module know about filesystems that selinux treats specially
|
|
|
|
|
module_args['_ansible_selinux_special_fs'] = C.DEFAULT_SELINUX_SPECIAL_FS
|
|
|
|
|
|
2017-02-09 19:05:54 +00:00
|
|
|
|
# give the module the socket for persistent connections
|
2017-11-09 20:04:40 +00:00
|
|
|
|
module_args['_ansible_socket'] = getattr(self._connection, 'socket_path')
|
|
|
|
|
if not module_args['_ansible_socket']:
|
|
|
|
|
module_args['_ansible_socket'] = task_vars.get('ansible_socket')
|
2017-02-09 19:05:54 +00:00
|
|
|
|
|
2017-10-05 15:02:41 +00:00
|
|
|
|
# make sure all commands use the designated shell executable
|
|
|
|
|
module_args['_ansible_shell_executable'] = self._play_context.executable
|
|
|
|
|
|
2018-05-14 23:31:21 +00:00
|
|
|
|
# make sure modules are aware if they need to keep the remote files
|
|
|
|
|
module_args['_ansible_keep_remote_files'] = C.DEFAULT_KEEP_REMOTE_FILES
|
|
|
|
|
|
|
|
|
|
# make sure all commands use the designated temporary directory if created
|
2018-07-06 17:49:19 +00:00
|
|
|
|
if self._is_become_unprivileged(): # force fallback on remote_tmp as user cannot normally write to dir
|
|
|
|
|
module_args['_ansible_tmpdir'] = None
|
|
|
|
|
else:
|
|
|
|
|
module_args['_ansible_tmpdir'] = self._connection._shell.tmpdir
|
2018-01-23 01:39:25 +00:00
|
|
|
|
|
2018-05-14 23:31:21 +00:00
|
|
|
|
# make sure the remote_tmp value is sent through in case modules needs to create their own
|
|
|
|
|
try:
|
|
|
|
|
module_args['_ansible_remote_tmp'] = self._connection._shell.get_option('remote_tmp')
|
|
|
|
|
except KeyError:
|
|
|
|
|
# here for 3rd party shell plugin compatibility in case they do not define the remote_tmp option
|
|
|
|
|
module_args['_ansible_remote_tmp'] = '~/.ansible/tmp'
|
|
|
|
|
|
2017-11-16 18:49:57 +00:00
|
|
|
|
def _update_connection_options(self, options, variables=None):
|
|
|
|
|
''' ensures connections have the appropriate information '''
|
|
|
|
|
update = {}
|
|
|
|
|
|
|
|
|
|
if getattr(self.connection, 'glob_option_vars', False):
|
|
|
|
|
# if the connection allows for it, pass any variables matching it.
|
|
|
|
|
if variables is not None:
|
|
|
|
|
for varname in variables:
|
|
|
|
|
if varname.match('ansible_%s_' % self.connection._load_name):
|
|
|
|
|
update[varname] = variables[varname]
|
|
|
|
|
|
|
|
|
|
# always override existing with options
|
|
|
|
|
update.update(options)
|
|
|
|
|
self.connection.set_options(update)
|
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
def _execute_module(self, module_name=None, module_args=None, tmp=None, task_vars=None, persist_files=False, delete_remote_tmp=None, wrap_async=False):
|
2016-12-15 20:47:29 +00:00
|
|
|
|
'''
|
|
|
|
|
Transfer and run a module along with its arguments.
|
|
|
|
|
'''
|
2018-02-07 23:11:36 +00:00
|
|
|
|
if tmp is not None:
|
|
|
|
|
display.warning('_execute_module no longer honors the tmp parameter. Action plugins'
|
2018-02-15 17:01:02 +00:00
|
|
|
|
' should set self._connection._shell.tmpdir to share the tmpdir')
|
2018-02-07 23:11:36 +00:00
|
|
|
|
del tmp # No longer used
|
|
|
|
|
if delete_remote_tmp is not None:
|
|
|
|
|
display.warning('_execute_module no longer honors the delete_remote_tmp parameter.'
|
2018-02-15 17:01:02 +00:00
|
|
|
|
' Action plugins should check self._connection._shell.tmpdir to'
|
|
|
|
|
' see if a tmpdir existed before they were called to determine'
|
2018-02-07 23:11:36 +00:00
|
|
|
|
' if they are responsible for removing it.')
|
|
|
|
|
del delete_remote_tmp # No longer used
|
|
|
|
|
|
2018-05-14 23:31:21 +00:00
|
|
|
|
tmpdir = self._connection._shell.tmpdir
|
|
|
|
|
|
|
|
|
|
# We set the module_style to new here so the remote_tmp is created
|
|
|
|
|
# before the module args are built if remote_tmp is needed (async).
|
|
|
|
|
# If the module_style turns out to not be new and we didn't create the
|
|
|
|
|
# remote tmp here, it will still be created. This must be done before
|
|
|
|
|
# calling self._update_module_args() so the module wrapper has the
|
|
|
|
|
# correct remote_tmp value set
|
|
|
|
|
if not self._is_pipelining_enabled("new", wrap_async) and tmpdir is None:
|
|
|
|
|
self._make_tmp_path()
|
|
|
|
|
tmpdir = self._connection._shell.tmpdir
|
|
|
|
|
|
2016-12-15 20:47:29 +00:00
|
|
|
|
if task_vars is None:
|
|
|
|
|
task_vars = dict()
|
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
# if a module name was not specified for this execution, use the action from the task
|
2016-12-15 20:47:29 +00:00
|
|
|
|
if module_name is None:
|
|
|
|
|
module_name = self._task.action
|
|
|
|
|
if module_args is None:
|
|
|
|
|
module_args = self._task.args
|
|
|
|
|
|
2016-12-19 21:54:31 +00:00
|
|
|
|
self._update_module_args(module_name, module_args, task_vars)
|
2016-12-15 20:47:29 +00:00
|
|
|
|
|
2018-09-20 09:37:54 +00:00
|
|
|
|
# FIXME: convert async_wrapper.py to not rely on environment variables
|
|
|
|
|
# make sure we get the right async_dir variable, backwards compatibility
|
|
|
|
|
# means we need to lookup the env value ANSIBLE_ASYNC_DIR first
|
|
|
|
|
remove_async_dir = None
|
|
|
|
|
if wrap_async or self._task.async_val:
|
|
|
|
|
env_async_dir = [e for e in self._task.environment if
|
|
|
|
|
"ANSIBLE_ASYNC_DIR" in e]
|
|
|
|
|
if len(env_async_dir) > 0:
|
|
|
|
|
msg = "Setting the async dir from the environment keyword " \
|
|
|
|
|
"ANSIBLE_ASYNC_DIR is deprecated. Set the async_dir " \
|
|
|
|
|
"shell option instead"
|
|
|
|
|
self._display.deprecated(msg, "2.12")
|
|
|
|
|
else:
|
|
|
|
|
# ANSIBLE_ASYNC_DIR is not set on the task, we get the value
|
|
|
|
|
# from the shell option and temporarily add to the environment
|
|
|
|
|
# list for async_wrapper to pick up
|
|
|
|
|
try:
|
|
|
|
|
async_dir = self._connection._shell.get_option('async_dir')
|
|
|
|
|
except KeyError:
|
|
|
|
|
# in case 3rd party plugin has not set this, use the default
|
|
|
|
|
async_dir = "~/.ansible_async"
|
|
|
|
|
remove_async_dir = len(self._task.environment)
|
|
|
|
|
self._task.environment.append({"ANSIBLE_ASYNC_DIR": async_dir})
|
|
|
|
|
|
2017-02-17 08:09:56 +00:00
|
|
|
|
# FUTURE: refactor this along with module build process to better encapsulate "smart wrapper" functionality
|
2016-05-11 20:14:01 +00:00
|
|
|
|
(module_style, shebang, module_data, module_path) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
|
2016-05-20 13:25:20 +00:00
|
|
|
|
display.vvv("Using module file %s" % module_path)
|
2016-05-11 20:14:01 +00:00
|
|
|
|
if not shebang and module_style != 'binary':
|
2016-01-12 16:24:43 +00:00
|
|
|
|
raise AnsibleError("module (%s) is missing interpreter line" % module_name)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2018-08-02 17:30:57 +00:00
|
|
|
|
self._used_interpreter = shebang
|
2018-02-07 23:11:36 +00:00
|
|
|
|
remote_module_path = None
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
if not self._is_pipelining_enabled(module_style, wrap_async):
|
2018-02-15 17:01:02 +00:00
|
|
|
|
# we might need remote tmp dir
|
|
|
|
|
if tmpdir is None:
|
2018-02-07 23:11:36 +00:00
|
|
|
|
self._make_tmp_path()
|
2018-02-15 17:01:02 +00:00
|
|
|
|
tmpdir = self._connection._shell.tmpdir
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2016-01-11 17:07:05 +00:00
|
|
|
|
remote_module_filename = self._connection._shell.get_remote_filename(module_path)
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 18:23:59 +00:00
|
|
|
|
remote_module_path = self._connection._shell.join_path(tmpdir, 'AnsiballZ_%s' % remote_module_filename)
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
args_file_path = None
|
2016-12-14 17:52:18 +00:00
|
|
|
|
if module_style in ('old', 'non_native_want_json', 'binary'):
|
2018-02-15 17:01:02 +00:00
|
|
|
|
# we'll also need a tmp file to hold our module arguments
|
|
|
|
|
args_file_path = self._connection._shell.join_path(tmpdir, 'args')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2015-10-02 16:58:15 +00:00
|
|
|
|
if remote_module_path or module_style != 'new':
|
2016-05-20 13:25:20 +00:00
|
|
|
|
display.debug("transferring module to remote %s" % remote_module_path)
|
2016-05-11 20:14:01 +00:00
|
|
|
|
if module_style == 'binary':
|
2016-01-11 17:07:05 +00:00
|
|
|
|
self._transfer_file(module_path, remote_module_path)
|
2016-01-08 16:34:46 +00:00
|
|
|
|
else:
|
2016-01-11 17:07:05 +00:00
|
|
|
|
self._transfer_data(remote_module_path, module_data)
|
2015-10-02 16:58:15 +00:00
|
|
|
|
if module_style == 'old':
|
|
|
|
|
# we need to dump the module args to a k=v string in a file on
|
|
|
|
|
# the remote system, which can be read and parsed by the module
|
|
|
|
|
args_data = ""
|
2017-06-02 11:14:11 +00:00
|
|
|
|
for k, v in iteritems(module_args):
|
2016-11-17 21:18:29 +00:00
|
|
|
|
args_data += '%s=%s ' % (k, shlex_quote(text_type(v)))
|
2015-10-02 16:58:15 +00:00
|
|
|
|
self._transfer_data(args_file_path, args_data)
|
2016-05-11 20:14:01 +00:00
|
|
|
|
elif module_style in ('non_native_want_json', 'binary'):
|
2016-01-08 17:37:28 +00:00
|
|
|
|
self._transfer_data(args_file_path, json.dumps(module_args))
|
2015-11-11 16:29:37 +00:00
|
|
|
|
display.debug("done transferring module to remote")
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
environment_string = self._compute_environment_string()
|
|
|
|
|
|
2018-09-20 09:37:54 +00:00
|
|
|
|
# remove the ANSIBLE_ASYNC_DIR env entry if we added a temporary one for
|
|
|
|
|
# the async_wrapper task - this is so the async_status plugin doesn't
|
|
|
|
|
# fire a deprecation warning when it runs after this task
|
|
|
|
|
if remove_async_dir is not None:
|
|
|
|
|
del self._task.environment[remove_async_dir]
|
|
|
|
|
|
2018-02-07 23:11:36 +00:00
|
|
|
|
remote_files = []
|
2018-02-15 17:01:02 +00:00
|
|
|
|
if tmpdir and remote_module_path:
|
|
|
|
|
remote_files = [tmpdir, remote_module_path]
|
2016-08-06 01:40:28 +00:00
|
|
|
|
|
|
|
|
|
if args_file_path:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
remote_files.append(args_file_path)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
|
sudoable = True
|
2014-11-14 22:14:08 +00:00
|
|
|
|
in_data = None
|
2016-12-14 17:52:18 +00:00
|
|
|
|
cmd = ""
|
|
|
|
|
|
2018-01-04 02:51:53 +00:00
|
|
|
|
if wrap_async and not self._connection.always_pipeline_modules:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
# configure, upload, and chmod the async_wrapper module
|
2017-03-23 01:50:28 +00:00
|
|
|
|
(async_module_style, shebang, async_module_data, async_module_path) = self._configure_module(module_name='async_wrapper', module_args=dict(),
|
|
|
|
|
task_vars=task_vars)
|
2016-12-14 17:52:18 +00:00
|
|
|
|
async_module_remote_filename = self._connection._shell.get_remote_filename(async_module_path)
|
2018-02-15 17:01:02 +00:00
|
|
|
|
remote_async_module_path = self._connection._shell.join_path(tmpdir, async_module_remote_filename)
|
2016-12-14 17:52:18 +00:00
|
|
|
|
self._transfer_data(remote_async_module_path, async_module_data)
|
|
|
|
|
remote_files.append(remote_async_module_path)
|
|
|
|
|
|
2017-11-22 20:35:58 +00:00
|
|
|
|
async_limit = self._task.async_val
|
2017-06-02 11:14:11 +00:00
|
|
|
|
async_jid = str(random.randint(0, 999999999999))
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
|
|
# call the interpreter for async_wrapper directly
|
|
|
|
|
# this permits use of a script for an interpreter on non-Linux platforms
|
|
|
|
|
# TODO: re-implement async_wrapper as a regular module to avoid this special case
|
|
|
|
|
interpreter = shebang.replace('#!', '').strip()
|
|
|
|
|
async_cmd = [interpreter, remote_async_module_path, async_jid, async_limit, remote_module_path]
|
|
|
|
|
|
|
|
|
|
if environment_string:
|
|
|
|
|
async_cmd.insert(0, environment_string)
|
|
|
|
|
|
|
|
|
|
if args_file_path:
|
|
|
|
|
async_cmd.append(args_file_path)
|
|
|
|
|
else:
|
|
|
|
|
# maintain a fixed number of positional parameters for async_wrapper
|
|
|
|
|
async_cmd.append('_')
|
|
|
|
|
|
2018-02-15 17:01:02 +00:00
|
|
|
|
if not self._should_remove_tmp_path(tmpdir):
|
2016-12-14 17:52:18 +00:00
|
|
|
|
async_cmd.append("-preserve_tmp")
|
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
cmd = " ".join(to_text(x) for x in async_cmd)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
|
|
else:
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
|
|
if self._is_pipelining_enabled(module_style):
|
|
|
|
|
in_data = module_data
|
|
|
|
|
else:
|
2014-11-14 22:14:08 +00:00
|
|
|
|
cmd = remote_module_path
|
|
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
|
cmd = self._connection._shell.build_module_command(environment_string, shebang, cmd, arg_path=args_file_path).strip()
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
2018-02-15 17:01:02 +00:00
|
|
|
|
# Fix permissions of the tmpdir path and tmpdir files. This should be called after all
|
2018-02-07 23:11:36 +00:00
|
|
|
|
# files have been transferred.
|
2016-12-14 17:52:18 +00:00
|
|
|
|
if remote_files:
|
|
|
|
|
# remove none/empty
|
2017-06-02 11:14:11 +00:00
|
|
|
|
remote_files = [x for x in remote_files if x]
|
2016-12-14 17:52:18 +00:00
|
|
|
|
self._fixup_perms2(remote_files, self._play_context.remote_user)
|
|
|
|
|
|
|
|
|
|
# actually execute
|
2015-09-24 20:29:36 +00:00
|
|
|
|
res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2017-03-16 00:12:16 +00:00
|
|
|
|
# parse the main result
|
2016-03-30 16:45:21 +00:00
|
|
|
|
data = self._parse_returned_data(res)
|
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
# NOTE: INTERNAL KEYS ONLY ACCESSIBLE HERE
|
2016-12-14 17:52:18 +00:00
|
|
|
|
# get internal info before cleaning
|
2018-01-16 05:15:04 +00:00
|
|
|
|
if data.pop("_ansible_suppress_tmpdir_delete", False):
|
|
|
|
|
self._cleanup_remote_tmp = False
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
|
|
# remove internal keys
|
2017-08-15 20:38:59 +00:00
|
|
|
|
remove_internal_keys(data)
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
|
|
if wrap_async:
|
2018-02-15 17:01:02 +00:00
|
|
|
|
# async_wrapper will clean up its tmpdir on its own so we want the controller side to
|
2018-02-08 06:38:45 +00:00
|
|
|
|
# forget about it now
|
2018-02-15 17:01:02 +00:00
|
|
|
|
self._connection._shell.tmpdir = None
|
2018-02-08 06:38:45 +00:00
|
|
|
|
|
|
|
|
|
# FIXME: for backwards compat, figure out if still makes sense
|
2016-12-14 17:52:18 +00:00
|
|
|
|
data['changed'] = True
|
|
|
|
|
|
|
|
|
|
# pre-split stdout/stderr into lines if needed
|
2016-03-30 16:45:21 +00:00
|
|
|
|
if 'stdout' in data and 'stdout_lines' not in data:
|
2017-03-21 22:11:11 +00:00
|
|
|
|
# if the value is 'False', a default won't catch it.
|
|
|
|
|
txt = data.get('stdout', None) or u''
|
|
|
|
|
data['stdout_lines'] = txt.splitlines()
|
2016-12-14 17:52:18 +00:00
|
|
|
|
if 'stderr' in data and 'stderr_lines' not in data:
|
2017-03-21 22:11:11 +00:00
|
|
|
|
# if the value is 'False', a default won't catch it.
|
|
|
|
|
txt = data.get('stderr', None) or u''
|
|
|
|
|
data['stderr_lines'] = txt.splitlines()
|
2017-01-31 23:13:26 +00:00
|
|
|
|
|
2016-03-30 16:45:21 +00:00
|
|
|
|
display.debug("done with _execute_module (%s, %s)" % (module_name, module_args))
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def _parse_returned_data(self, res):
|
2015-04-21 14:48:13 +00:00
|
|
|
|
try:
|
2016-10-02 15:03:42 +00:00
|
|
|
|
filtered_output, warnings = _filter_non_json_lines(res.get('stdout', u''))
|
|
|
|
|
for w in warnings:
|
|
|
|
|
display.warning(w)
|
2017-01-12 19:54:40 +00:00
|
|
|
|
|
2016-10-02 15:03:42 +00:00
|
|
|
|
data = json.loads(filtered_output)
|
2017-01-12 19:54:40 +00:00
|
|
|
|
|
2016-10-24 18:05:56 +00:00
|
|
|
|
if 'ansible_facts' in data and isinstance(data['ansible_facts'], dict):
|
2016-12-13 17:14:47 +00:00
|
|
|
|
data['ansible_facts'] = wrap_var(data['ansible_facts'])
|
2017-03-20 14:08:13 +00:00
|
|
|
|
data['_ansible_parsed'] = True
|
2015-04-21 14:48:13 +00:00
|
|
|
|
except ValueError:
|
|
|
|
|
# not valid json, lets try to capture error
|
2016-08-16 15:59:30 +00:00
|
|
|
|
data = dict(failed=True, _ansible_parsed=False)
|
2016-03-16 03:31:40 +00:00
|
|
|
|
data['module_stdout'] = res.get('stdout', u'')
|
|
|
|
|
if 'stderr' in res:
|
|
|
|
|
data['module_stderr'] = res['stderr']
|
|
|
|
|
if res['stderr'].startswith(u'Traceback'):
|
|
|
|
|
data['exception'] = res['stderr']
|
2018-08-02 17:30:57 +00:00
|
|
|
|
|
|
|
|
|
# try to figure out if we are missing interpreter
|
|
|
|
|
if self._used_interpreter is not None and '%s: No such file or directory' % self._used_interpreter.lstrip('!#') in data['module_stderr']:
|
|
|
|
|
data['msg'] = "The module failed to execute correctly, you probably need to set the interpreter."
|
|
|
|
|
else:
|
|
|
|
|
data['msg'] = "MODULE FAILURE"
|
|
|
|
|
|
|
|
|
|
data['msg'] += '\nSee stdout/stderr for the exact error'
|
|
|
|
|
|
2016-12-07 16:00:11 +00:00
|
|
|
|
if 'rc' in res:
|
|
|
|
|
data['rc'] = res['rc']
|
2014-11-14 22:14:08 +00:00
|
|
|
|
return data
|
|
|
|
|
|
2017-07-05 14:39:00 +00:00
|
|
|
|
def _low_level_execute_command(self, cmd, sudoable=True, in_data=None, executable=None, encoding_errors='surrogate_then_replace', chdir=None):
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
|
|
|
|
This is the function which executes the low level shell command, which
|
|
|
|
|
may be commands to create/remove directories for temporary files, or to
|
|
|
|
|
run the module code or python directly when pipelining.
|
2015-10-06 21:10:25 +00:00
|
|
|
|
|
|
|
|
|
:kwarg encoding_errors: If the value returned by the command isn't
|
|
|
|
|
utf-8 then we have to figure out how to transform it to unicode.
|
|
|
|
|
If the value is just going to be displayed to the user (or
|
|
|
|
|
discarded) then the default of 'replace' is fine. If the data is
|
|
|
|
|
used as a key or is going to be written back out to a file
|
|
|
|
|
verbatim, then this won't work. May have to use some sort of
|
|
|
|
|
replacement strategy (python3 could use surrogateescape)
|
2017-07-05 14:39:00 +00:00
|
|
|
|
:kwarg chdir: cd into this directory before executing the command.
|
2014-11-14 22:14:08 +00:00
|
|
|
|
'''
|
2015-12-22 19:12:41 +00:00
|
|
|
|
|
2015-12-11 01:41:48 +00:00
|
|
|
|
display.debug("_low_level_execute_command(): starting")
|
2017-02-17 08:09:56 +00:00
|
|
|
|
# if not cmd:
|
|
|
|
|
# # this can happen with powershell modules when there is no analog to a Windows command (like chmod)
|
|
|
|
|
# display.debug("_low_level_execute_command(): no command, exiting")
|
|
|
|
|
# return dict(stdout='', stderr='', rc=254)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2017-07-05 14:39:00 +00:00
|
|
|
|
if chdir:
|
|
|
|
|
display.debug("_low_level_execute_command(): changing cwd to %s for this command" % chdir)
|
|
|
|
|
cmd = self._connection._shell.append_command('cd %s' % chdir, cmd)
|
|
|
|
|
|
2015-09-17 12:59:22 +00:00
|
|
|
|
allow_same_user = C.BECOME_ALLOW_SAME_USER
|
|
|
|
|
same_user = self._play_context.become_user == self._play_context.remote_user
|
|
|
|
|
if sudoable and self._play_context.become and (allow_same_user or not same_user):
|
2015-12-11 01:41:48 +00:00
|
|
|
|
display.debug("_low_level_execute_command(): using become for this command")
|
2017-11-09 20:04:40 +00:00
|
|
|
|
if self._connection.transport != 'network_cli' and self._play_context.become_method != 'enable':
|
|
|
|
|
cmd = self._play_context.make_become_cmd(cmd, executable=executable)
|
2015-06-24 16:12:54 +00:00
|
|
|
|
|
2016-03-12 01:38:38 +00:00
|
|
|
|
if self._connection.allow_executable:
|
|
|
|
|
if executable is None:
|
|
|
|
|
executable = self._play_context.executable
|
2016-05-19 17:33:17 +00:00
|
|
|
|
# mitigation for SSH race which can drop stdout (https://github.com/ansible/ansible/issues/13876)
|
|
|
|
|
# only applied for the default executable to avoid interfering with the raw action
|
|
|
|
|
cmd = self._connection._shell.append_command(cmd, 'sleep 0')
|
2016-03-28 15:02:27 +00:00
|
|
|
|
if executable:
|
2016-11-17 21:18:29 +00:00
|
|
|
|
cmd = executable + ' -c ' + shlex_quote(cmd)
|
2016-02-10 00:05:41 +00:00
|
|
|
|
|
2015-12-11 01:41:48 +00:00
|
|
|
|
display.debug("_low_level_execute_command(): executing: %s" % (cmd,))
|
2016-07-25 12:11:45 +00:00
|
|
|
|
|
2016-10-03 12:42:32 +00:00
|
|
|
|
# Change directory to basedir of task for command execution when connection is local
|
|
|
|
|
if self._connection.transport == 'local':
|
|
|
|
|
cwd = os.getcwd()
|
2016-10-04 18:23:57 +00:00
|
|
|
|
os.chdir(self._loader.get_basedir())
|
2016-07-25 12:11:45 +00:00
|
|
|
|
try:
|
|
|
|
|
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
|
|
|
|
finally:
|
2016-10-03 12:42:32 +00:00
|
|
|
|
if self._connection.transport == 'local':
|
|
|
|
|
os.chdir(cwd)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
2015-10-01 14:28:56 +00:00
|
|
|
|
# stdout and stderr may be either a file-like or a bytes object.
|
|
|
|
|
# Convert either one to a text type
|
|
|
|
|
if isinstance(stdout, binary_type):
|
2016-09-07 05:54:17 +00:00
|
|
|
|
out = to_text(stdout, errors=encoding_errors)
|
2015-10-01 14:28:56 +00:00
|
|
|
|
elif not isinstance(stdout, text_type):
|
2016-09-07 05:54:17 +00:00
|
|
|
|
out = to_text(b''.join(stdout.readlines()), errors=encoding_errors)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
else:
|
|
|
|
|
out = stdout
|
|
|
|
|
|
2015-10-01 14:28:56 +00:00
|
|
|
|
if isinstance(stderr, binary_type):
|
2016-09-07 05:54:17 +00:00
|
|
|
|
err = to_text(stderr, errors=encoding_errors)
|
2015-10-01 14:28:56 +00:00
|
|
|
|
elif not isinstance(stderr, text_type):
|
2016-09-07 05:54:17 +00:00
|
|
|
|
err = to_text(b''.join(stderr.readlines()), errors=encoding_errors)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
else:
|
|
|
|
|
err = stderr
|
|
|
|
|
|
2015-06-06 04:16:35 +00:00
|
|
|
|
if rc is None:
|
|
|
|
|
rc = 0
|
|
|
|
|
|
2016-02-09 16:18:09 +00:00
|
|
|
|
# be sure to remove the BECOME-SUCCESS message now
|
|
|
|
|
out = self._strip_success_message(out)
|
2015-12-11 01:41:48 +00:00
|
|
|
|
|
2016-09-16 23:23:42 +00:00
|
|
|
|
display.debug(u"_low_level_execute_command() done: rc=%d, stdout=%s, stderr=%s" % (rc, out, err))
|
2018-05-14 13:56:49 +00:00
|
|
|
|
return dict(rc=rc, stdout=out, stdout_lines=out.splitlines(), stderr=err, stderr_lines=err.splitlines())
|
2015-07-15 23:47:59 +00:00
|
|
|
|
|
2015-09-24 20:29:36 +00:00
|
|
|
|
def _get_diff_data(self, destination, source, task_vars, source_file=True):
|
2015-07-27 02:29:56 +00:00
|
|
|
|
|
2018-09-20 17:31:48 +00:00
|
|
|
|
# Note: Since we do not diff the source and destination before we transform from bytes into
|
|
|
|
|
# text the diff between source and destination may not be accurate. To fix this, we'd need
|
|
|
|
|
# to move the diffing from the callback plugins into here.
|
|
|
|
|
#
|
|
|
|
|
# Example of data which would cause trouble is src_content == b'\xff' and dest_content ==
|
|
|
|
|
# b'\xfe'. Neither of those are valid utf-8 so both get turned into the replacement
|
|
|
|
|
# character: diff['before'] = u'<27>' ; diff['after'] = u'<27>' When the callback plugin later
|
|
|
|
|
# diffs before and after it shows an empty diff.
|
|
|
|
|
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff = {}
|
2015-11-11 16:29:37 +00:00
|
|
|
|
display.debug("Going to peek to see if file has changed permissions")
|
2018-04-24 02:38:42 +00:00
|
|
|
|
peek_result = self._execute_module(module_name='file', module_args=dict(path=destination, _diff_peek=True), task_vars=task_vars, persist_files=True)
|
2015-07-27 02:29:56 +00:00
|
|
|
|
|
2016-12-07 16:00:11 +00:00
|
|
|
|
if not peek_result.get('failed', False) or peek_result.get('rc', 0) == 0:
|
2015-07-27 02:29:56 +00:00
|
|
|
|
|
2017-01-04 23:04:34 +00:00
|
|
|
|
if peek_result.get('state') == 'absent':
|
2018-09-20 17:31:48 +00:00
|
|
|
|
diff['before'] = u''
|
2017-01-04 23:04:34 +00:00
|
|
|
|
elif peek_result.get('appears_binary'):
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff['dst_binary'] = 1
|
2017-01-04 23:04:34 +00:00
|
|
|
|
elif peek_result.get('size') and C.MAX_FILE_SIZE_FOR_DIFF > 0 and peek_result['size'] > C.MAX_FILE_SIZE_FOR_DIFF:
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff['dst_larger'] = C.MAX_FILE_SIZE_FOR_DIFF
|
|
|
|
|
else:
|
2018-09-20 17:31:48 +00:00
|
|
|
|
display.debug(u"Slurping the file %s" % source)
|
2015-07-27 02:29:56 +00:00
|
|
|
|
dest_result = self._execute_module(module_name='slurp', module_args=dict(path=destination), task_vars=task_vars, persist_files=True)
|
|
|
|
|
if 'content' in dest_result:
|
|
|
|
|
dest_contents = dest_result['content']
|
2018-09-20 17:31:48 +00:00
|
|
|
|
if dest_result['encoding'] == u'base64':
|
2015-07-27 02:29:56 +00:00
|
|
|
|
dest_contents = base64.b64decode(dest_contents)
|
|
|
|
|
else:
|
2018-09-20 17:31:48 +00:00
|
|
|
|
raise AnsibleError("unknown encoding in content option, failed: %s" % to_native(dest_result))
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff['before_header'] = destination
|
2018-09-20 17:31:48 +00:00
|
|
|
|
diff['before'] = to_text(dest_contents)
|
2015-07-27 02:29:56 +00:00
|
|
|
|
|
|
|
|
|
if source_file:
|
2016-02-10 17:05:45 +00:00
|
|
|
|
st = os.stat(source)
|
2016-02-10 16:23:49 +00:00
|
|
|
|
if C.MAX_FILE_SIZE_FOR_DIFF > 0 and st[stat.ST_SIZE] > C.MAX_FILE_SIZE_FOR_DIFF:
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff['src_larger'] = C.MAX_FILE_SIZE_FOR_DIFF
|
|
|
|
|
else:
|
2016-02-10 17:05:45 +00:00
|
|
|
|
display.debug("Reading local copy of the file %s" % source)
|
|
|
|
|
try:
|
2017-04-07 17:20:18 +00:00
|
|
|
|
with open(source, 'rb') as src:
|
|
|
|
|
src_contents = src.read()
|
2016-02-10 17:05:45 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise AnsibleError("Unexpected error while reading source (%s) for diff: %s " % (source, str(e)))
|
2016-02-10 16:23:49 +00:00
|
|
|
|
|
2017-04-07 17:20:18 +00:00
|
|
|
|
if b"\x00" in src_contents:
|
2016-02-10 17:05:45 +00:00
|
|
|
|
diff['src_binary'] = 1
|
|
|
|
|
else:
|
|
|
|
|
diff['after_header'] = source
|
2018-09-20 17:31:48 +00:00
|
|
|
|
diff['after'] = to_text(src_contents)
|
2015-07-27 02:29:56 +00:00
|
|
|
|
else:
|
2018-09-20 17:31:48 +00:00
|
|
|
|
display.debug(u"source of file passed in")
|
|
|
|
|
diff['after_header'] = u'dynamically generated'
|
2015-07-27 02:29:56 +00:00
|
|
|
|
diff['after'] = source
|
|
|
|
|
|
2016-02-09 06:05:46 +00:00
|
|
|
|
if self._play_context.no_log:
|
|
|
|
|
if 'before' in diff:
|
2018-09-20 17:31:48 +00:00
|
|
|
|
diff["before"] = u""
|
2016-02-09 06:05:46 +00:00
|
|
|
|
if 'after' in diff:
|
2018-09-20 17:31:48 +00:00
|
|
|
|
diff["after"] = u" [[ Diff output has been hidden because 'no_log: true' was specified for this result ]]\n"
|
2016-02-04 20:44:45 +00:00
|
|
|
|
|
2015-07-27 02:29:56 +00:00
|
|
|
|
return diff
|
2016-06-28 21:23:30 +00:00
|
|
|
|
|
|
|
|
|
def _find_needle(self, dirname, needle):
|
|
|
|
|
'''
|
|
|
|
|
find a needle in haystack of paths, optionally using 'dirname' as a subdir.
|
|
|
|
|
This will build the ordered list of paths to search and pass them to dwim
|
|
|
|
|
to get back the first existing file found.
|
|
|
|
|
'''
|
|
|
|
|
|
2016-10-17 14:32:28 +00:00
|
|
|
|
# dwim already deals with playbook basedirs
|
2016-07-13 14:06:34 +00:00
|
|
|
|
path_stack = self._task.get_search_path()
|
2016-06-28 21:23:30 +00:00
|
|
|
|
|
2017-07-03 19:27:53 +00:00
|
|
|
|
# if missing it will return a file not found exception
|
|
|
|
|
return self._loader.path_dwim_relative_stack(path_stack, dirname, needle)
|