async: use async_dir for the async results file directory (#45461)

* win async: use async_dir for the async results file directory

* tried to unify POSIX and PowerShell async implementations of async_dir

* fix sanity issue
This commit is contained in:
Jordan Borean
2018-09-20 19:37:54 +10:00
committed by GitHub
parent 60979a96a9
commit 5c73d4f4bd
11 changed files with 269 additions and 19 deletions

View File

@@ -751,8 +751,8 @@ class TaskExecutor:
# Because this is an async task, the action handler is async. However,
# we need the 'normal' action handler for the status check, so get it
# now via the action_loader
normal_handler = self._shared_loader_obj.action_loader.get(
'normal',
async_handler = self._shared_loader_obj.action_loader.get(
'async_status',
task=async_task,
connection=self._connection,
play_context=self._play_context,
@@ -766,7 +766,7 @@ class TaskExecutor:
time.sleep(self._task.poll)
try:
async_result = normal_handler.run(task_vars=task_vars)
async_result = async_handler.run(task_vars=task_vars)
# We do not bail out of the loop in cases where the failure
# is associated with a parsing error. The async_runner can
# have issues which result in a half-written/unparseable result
@@ -783,7 +783,7 @@ class TaskExecutor:
display.vvvv("Exception during async poll, retrying... (%s)" % to_text(e))
display.debug("Async poll exception was:\n%s" % to_text(traceback.format_exc()))
try:
normal_handler._connection.reset()
async_handler._connection.reset()
except AttributeError:
pass

View File

@@ -51,12 +51,13 @@ def main():
module = AnsibleModule(argument_spec=dict(
jid=dict(required=True),
mode=dict(default='status', choices=['status', 'cleanup']),
# passed in from the async_status action plugin
_async_dir=dict(required=True, type='path'),
))
mode = module.params['mode']
jid = module.params['jid']
async_dir = os.environ.get('ANSIBLE_ASYNC_DIR', '~/.ansible_async')
async_dir = module.params['_async_dir']
# setup logging directory
logdir = os.path.expanduser(async_dir)

View File

@@ -9,13 +9,15 @@ $results = @{changed=$false}
$parsed_args = Parse-Args $args
$jid = Get-AnsibleParam $parsed_args "jid" -failifempty $true -resultobj $results
$mode = Get-AnsibleParam $parsed_args "mode" -Default "status" -ValidateSet "status","cleanup"
$_remote_tmp = Get-AnsibleParam $parsed_args "_ansible_remote_tmp" -type "path" -default $env:TMP
$log_path = [System.IO.Path]::Combine($_remote_tmp, ".ansible_async", $jid)
# parsed in from the async_status action plugin
$async_dir = Get-AnsibleParam $parsed_args "_async_dir" -type "path" -failifempty $true
$log_path = [System.IO.Path]::Combine($async_dir, $jid)
If(-not $(Test-Path $log_path))
{
Fail-Json @{ansible_job_id=$jid; started=1; finished=1} "could not find job"
Fail-Json @{ansible_job_id=$jid; started=1; finished=1} "could not find job at '$async_dir'"
}
If($mode -eq "cleanup") {

View File

@@ -734,6 +734,30 @@ class ActionBase(with_metaclass(ABCMeta, object)):
self._update_module_args(module_name, module_args, task_vars)
# 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})
# FUTURE: refactor this along with module build process to better encapsulate "smart wrapper" functionality
(module_style, shebang, module_data, module_path) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
display.vvv("Using module file %s" % module_path)
@@ -776,6 +800,12 @@ class ActionBase(with_metaclass(ABCMeta, object)):
environment_string = self._compute_environment_string()
# 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]
remote_files = []
if tmpdir and remote_module_path:
remote_files = [tmpdir, remote_module_path]

View File

@@ -0,0 +1,51 @@
# Copyright: (c) 2018, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.utils.vars import merge_hash
class ActionModule(ActionBase):
_VALID_ARGS = frozenset(('jid', 'mode'))
def run(self, tmp=None, task_vars=None):
results = super(ActionModule, self).run(tmp, task_vars)
del tmp # tmp no longer has any effect
if "jid" not in self._task.args:
raise AnsibleError("jid is required")
jid = self._task.args["jid"]
mode = self._task.args.get("mode", "status")
env_async_dir = [e for e in self._task.environment if
"ANSIBLE_ASYNC_DIR" in e]
if len(env_async_dir) > 0:
# for backwards compatibility we need to get the dir from
# ANSIBLE_ASYNC_DIR that is defined in the environment. This is
# deprecated and will be removed in favour of shell options
async_dir = env_async_dir[0]['ANSIBLE_ASYNC_DIR']
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:
# inject the async directory based on the shell option into the
# module args
try:
async_dir = self._connection._shell.get_option('async_dir')
except KeyError:
# here for 3rd party shell plugin compatibility in case they do
# not define the async_dir option
async_dir = "~/.ansible_async"
module_args = dict(jid=jid, mode=mode, _async_dir=async_dir)
status = self._execute_module(task_vars=task_vars,
module_args=module_args)
results = merge_hash(results, status)
return results

View File

@@ -12,6 +12,17 @@ DOCUMENTATION = '''
description:
- The only option when using 'winrm' as a connection plugin
options:
async_dir:
description:
- Directory in which ansible will keep async job information.
- Before Ansible 2.8, this was set to C(remote_tmp + "\\.ansible_async").
default: '%USERPROFILE%\\.ansible_async'
ini:
- section: powershell
key: async_dir
vars:
- name: ansible_async_dir
version_added: '2.8'
remote_tmp:
description:
- Temporary directory to use on targets when copying files to the host.
@@ -1213,14 +1224,18 @@ $exec_wrapper = {
Function Run($payload) {
$remote_tmp = $payload["module_args"]["_ansible_remote_tmp"]
$remote_tmp = [System.Environment]::ExpandEnvironmentVariables($remote_tmp)
if ($payload.environment.ContainsKey("ANSIBLE_ASYNC_DIR")) {
$async_dir = $payload.environment.ANSIBLE_ASYNC_DIR
} else {
$async_dir = "%USERPROFILE%\.ansible_async"
}
$async_dir = [System.Environment]::ExpandEnvironmentVariables($async_dir)
# calculate the result path so we can include it in the worker payload
$jid = $payload.async_jid
$local_jid = $jid + "." + $pid
$results_path = [System.IO.Path]::Combine($remote_tmp, ".ansible_async", $local_jid)
$results_path = [System.IO.Path]::Combine($async_dir, $local_jid)
$payload.async_results_path = $results_path

View File

@@ -32,7 +32,7 @@ options:
- name: ansible_system_tmpdirs
async_dir:
description:
- Directory in which ansible will keep async job inforamtion
- Directory in which ansible will keep async job information
default: '~/.ansible_async'
env: [{name: ANSIBLE_ASYNC_DIR}]
ini: