mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Moving ConnectionInformation -> PlayContext
Also making PlayContext a child class of the Playbook Base class, which gives it access to all of the FieldAttribute code to ensure field values are correctly typed after post_validation Fixes #11381
This commit is contained in:
parent
919aaa5c42
commit
e64989beb4
36 changed files with 477 additions and 459 deletions
|
@ -31,7 +31,6 @@ from ansible import constants as C
|
|||
from ansible.errors import AnsibleError
|
||||
from ansible.executor.module_common import modify_module
|
||||
from ansible.parsing.utils.jsonify import jsonify
|
||||
|
||||
from ansible.utils.debug import debug
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
|
@ -44,10 +43,10 @@ class ActionBase:
|
|||
action in use.
|
||||
'''
|
||||
|
||||
def __init__(self, task, connection, connection_info, loader, templar, shared_loader_obj):
|
||||
def __init__(self, task, connection, play_context, loader, templar, shared_loader_obj):
|
||||
self._task = task
|
||||
self._connection = connection
|
||||
self._connection_info = connection_info
|
||||
self._play_context = play_context
|
||||
self._loader = loader
|
||||
self._templar = templar
|
||||
self._shared_loader_obj = shared_loader_obj
|
||||
|
@ -82,16 +81,11 @@ class ActionBase:
|
|||
Builds the environment string to be used when executing the remote task.
|
||||
'''
|
||||
|
||||
enviro = {}
|
||||
if self._task.environment:
|
||||
if type(self._task.environment) != dict:
|
||||
raise errors.AnsibleError("environment must be a dictionary, received %s" % self._task.environment)
|
||||
|
||||
# FIXME: not sure where this comes from, probably task but maybe also the play?
|
||||
#if self.environment:
|
||||
# enviro = template.template(self.basedir, self.environment, inject, convert_bare=True)
|
||||
# enviro = utils.safe_eval(enviro)
|
||||
# if type(enviro) != dict:
|
||||
# raise errors.AnsibleError("environment must be a dictionary, received %s" % enviro)
|
||||
|
||||
return self._connection._shell.env_prefix(**enviro)
|
||||
return self._connection._shell.env_prefix(**self._task.environment)
|
||||
|
||||
def _early_needs_tmp_path(self):
|
||||
'''
|
||||
|
@ -109,7 +103,7 @@ class ActionBase:
|
|||
if tmp and "tmp" in tmp:
|
||||
# tmp has already been created
|
||||
return False
|
||||
if not self._connection.__class__.has_pipelining or not C.ANSIBLE_SSH_PIPELINING or C.DEFAULT_KEEP_REMOTE_FILES or self._connection_info.become:
|
||||
if not self._connection.__class__.has_pipelining or not C.ANSIBLE_SSH_PIPELINING or C.DEFAULT_KEEP_REMOTE_FILES or self._play_context.become:
|
||||
# tmp is necessary to store module source code
|
||||
return True
|
||||
if not self._connection.__class__.has_pipelining:
|
||||
|
@ -131,11 +125,11 @@ class ActionBase:
|
|||
basefile = 'ansible-tmp-%s-%s' % (time.time(), random.randint(0, 2**48))
|
||||
use_system_tmp = False
|
||||
|
||||
if self._connection_info.become and self._connection_info.become_user != 'root':
|
||||
if self._play_context.become and self._play_context.become_user != 'root':
|
||||
use_system_tmp = True
|
||||
|
||||
tmp_mode = None
|
||||
if self._connection_info.remote_user != 'root' or self._connection_info.become and self._connection_info.become_user != 'root':
|
||||
if self._play_context.remote_user != 'root' or self._play_context.become and self._play_context.become_user != 'root':
|
||||
tmp_mode = 'a+rx'
|
||||
|
||||
cmd = self._connection._shell.mkdtemp(basefile, use_system_tmp, tmp_mode)
|
||||
|
@ -149,7 +143,7 @@ class ActionBase:
|
|||
output = 'Authentication failure.'
|
||||
elif result['rc'] == 255 and self._connection.transport in ('ssh',):
|
||||
|
||||
if self._connection_info.verbosity > 3:
|
||||
if self._play_context.verbosity > 3:
|
||||
output = 'SSH encountered an unknown error. The output was:\n%s' % (result['stdout']+result['stderr'])
|
||||
else:
|
||||
output = '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'
|
||||
|
@ -264,8 +258,8 @@ class ActionBase:
|
|||
split_path = path.split(os.path.sep, 1)
|
||||
expand_path = split_path[0]
|
||||
if expand_path == '~':
|
||||
if self._connection_info.become and self._connection_info.become_user:
|
||||
expand_path = '~%s' % self._connection_info.become_user
|
||||
if self._play_context.become and self._play_context.become_user:
|
||||
expand_path = '~%s' % self._play_context.become_user
|
||||
|
||||
cmd = self._connection._shell.expand_user(expand_path)
|
||||
debug("calling _low_level_execute_command to expand the remote user path")
|
||||
|
@ -314,13 +308,13 @@ class ActionBase:
|
|||
module_args = self._task.args
|
||||
|
||||
# set check mode in the module arguments, if required
|
||||
if self._connection_info.check_mode and not self._task.always_run:
|
||||
if self._play_context.check_mode and not self._task.always_run:
|
||||
if not self._supports_check_mode:
|
||||
raise AnsibleError("check mode is not supported for this operation")
|
||||
module_args['_ansible_check_mode'] = True
|
||||
|
||||
# set no log in the module arguments, if required
|
||||
if self._connection_info.no_log:
|
||||
if self._play_context.no_log:
|
||||
module_args['_ansible_no_log'] = True
|
||||
|
||||
debug("in _execute_module (%s, %s)" % (module_name, module_args))
|
||||
|
@ -344,7 +338,7 @@ class ActionBase:
|
|||
|
||||
environment_string = self._compute_environment_string()
|
||||
|
||||
if tmp and "tmp" in tmp and self._connection_info.become and self._connection_info.become_user != 'root':
|
||||
if tmp and "tmp" in tmp and self._play_context.become and self._play_context.become_user != 'root':
|
||||
# deal with possible umask issues once sudo'ed to other user
|
||||
self._remote_chmod(tmp, 'a+r', remote_module_path)
|
||||
|
||||
|
@ -362,7 +356,7 @@ class ActionBase:
|
|||
|
||||
rm_tmp = None
|
||||
if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if not self._connection_info.become or self._connection_info.become_user == 'root':
|
||||
if not self._play_context.become or self._play_context.become_user == 'root':
|
||||
# not sudoing or sudoing to root, so can cleanup files in the same step
|
||||
rm_tmp = tmp
|
||||
|
||||
|
@ -380,7 +374,7 @@ class ActionBase:
|
|||
debug("_low_level_execute_command returned ok")
|
||||
|
||||
if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if self._connection_info.become and self._connection_info.become_user != 'root':
|
||||
if self._play_context.become and self._play_context.become_user != 'root':
|
||||
# not sudoing to root, so maybe can't delete files as that other user
|
||||
# have to clean up temp files as original user in a second step
|
||||
cmd2 = self._connection._shell.remove(tmp, recurse=True)
|
||||
|
@ -430,7 +424,7 @@ class ActionBase:
|
|||
return dict(stdout='', stderr='')
|
||||
|
||||
if sudoable:
|
||||
cmd = self._connection_info.make_become_cmd(cmd, executable=executable)
|
||||
cmd = self._play_context.make_become_cmd(cmd, executable=executable)
|
||||
|
||||
debug("executing the command %s through the connection" % cmd)
|
||||
rc, stdin, stdout, stderr = self._connection.exec_command(cmd, tmp, in_data=in_data, sudoable=sudoable)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue