Allow async_status jid to be the full results_file path

pull/84154/head
Matt Martz 1 month ago
parent 56bab1d097
commit 2f6fa2d26c
No known key found for this signature in database
GPG Key ID: 40832D88E9FC91D8

@ -866,6 +866,7 @@ class TaskExecutor:
task_vars = self._job_vars task_vars = self._job_vars
async_jid = result.get('ansible_job_id') async_jid = result.get('ansible_job_id')
results_file = result.get('results_file')
if async_jid is None: if async_jid is None:
return dict(failed=True, msg="No job id was returned by the async task") return dict(failed=True, msg="No job id was returned by the async task")
@ -875,7 +876,7 @@ class TaskExecutor:
async_task = Task.load(dict( async_task = Task.load(dict(
action='async_status', action='async_status',
args={'jid': async_jid}, args={'jid': results_file or async_jid},
check_mode=self._task.check_mode, check_mode=self._task.check_mode,
environment=self._task.environment, environment=self._task.environment,
)) ))
@ -947,7 +948,7 @@ class TaskExecutor:
cleanup_task = Task.load( cleanup_task = Task.load(
{ {
'async_status': { 'async_status': {
'jid': async_jid, 'jid': results_file or async_jid,
'mode': 'cleanup', 'mode': 'cleanup',
}, },
'check_mode': self._task.check_mode, 'check_mode': self._task.check_mode,

@ -18,6 +18,8 @@ options:
jid: jid:
description: description:
- Job or task identifier - Job or task identifier
- May be the exact value of C(ansible_job_id) or the full path to the results file provided by C(results_file)
- Support for this value taking a path was added in 2.19.
type: str type: str
required: true required: true
mode: mode:
@ -73,7 +75,7 @@ EXAMPLES = r"""
- name: Clean up async file - name: Clean up async file
ansible.builtin.async_status: ansible.builtin.async_status:
jid: '{{ dnf_sleeper.ansible_job_id }}' jid: '{{ dnf_sleeper.results_file }}'
mode: cleanup mode: cleanup
""" """
@ -88,6 +90,12 @@ finished:
returned: always returned: always
type: int type: int
sample: 1 sample: 1
results_file:
description: Path to the async results file, which is a concatenation of the
async dir and the C(ansible_job_id)
returned: always
type: str
sample: '/home/user/.async_status/360874038559.4169'
started: started:
description: Whether the asynchronous job has started (V(1)) or not (V(0)) description: Whether the asynchronous job has started (V(1)) or not (V(0))
returned: always returned: always
@ -132,7 +140,10 @@ def main():
async_dir = module.params['_async_dir'] async_dir = module.params['_async_dir']
# setup logging directory # setup logging directory
log_path = os.path.join(async_dir, jid) if os.path.isabs(jid):
log_path = jid
else:
log_path = os.path.join(async_dir, jid)
if not os.path.exists(log_path): if not os.path.exists(log_path):
module.fail_json(msg="could not find job", ansible_job_id=jid, started=1, finished=1) module.fail_json(msg="could not find job", ansible_job_id=jid, started=1, finished=1)

@ -3,6 +3,8 @@
from __future__ import annotations from __future__ import annotations
import os
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
from ansible.utils.vars import merge_hash from ansible.utils.vars import merge_hash
@ -36,8 +38,13 @@ class ActionModule(ActionBase):
mode = new_module_args["mode"] mode = new_module_args["mode"]
results['ansible_job_id'] = jid results['ansible_job_id'] = jid
async_dir = self._get_async_dir() if os.path.isabs(jid):
log_path = self._connection._shell.join_path(async_dir, jid) log_path = jid
new_module_args['_async_dir'] = ''
else:
async_dir = self._get_async_dir()
log_path = self._connection._shell.join_path(async_dir, jid)
new_module_args['_async_dir'] = async_dir
if mode == 'cleanup': if mode == 'cleanup':
results['erased'] = log_path results['erased'] = log_path
@ -45,7 +52,6 @@ class ActionModule(ActionBase):
results['results_file'] = log_path results['results_file'] = log_path
results['started'] = 1 results['started'] = 1
new_module_args['_async_dir'] = async_dir
results = merge_hash(results, self._execute_module(module_name='ansible.legacy.async_status', task_vars=task_vars, module_args=new_module_args)) results = merge_hash(results, self._execute_module(module_name='ansible.legacy.async_status', task_vars=task_vars, module_args=new_module_args))
return results return results

@ -135,7 +135,12 @@ class ActionModule(ActionBase):
while jobs: while jobs:
for module in jobs: for module in jobs:
poll_args = {'jid': jobs[module]['ansible_job_id'], '_async_dir': os.path.dirname(jobs[module]['results_file'])} jid = jobs[module]['ansible_job_id']
if os.path.isabs(jid):
async_dir = ''
else:
async_dir = os.path.dirname(jobs[module]['results_file'])
poll_args = {'jid': jid, '_async_dir': async_dir}
res = self._execute_module(module_name='ansible.legacy.async_status', module_args=poll_args, task_vars=task_vars, wrap_async=False) res = self._execute_module(module_name='ansible.legacy.async_status', module_args=poll_args, task_vars=task_vars, wrap_async=False)
if res.get('finished', 0) == 1: if res.get('finished', 0) == 1:
if res.get('failed', False): if res.get('failed', False):

@ -122,7 +122,7 @@
- name: assert task failed correctly - name: assert task failed correctly
assert: assert:
that: that:
- async_result.ansible_job_id is match('j\d+\.\d+') - async_result.ansible_job_id|basename is match('j\d+\.\d+')
- async_result.finished == 1 - async_result.finished == 1
- async_result is finished - async_result is finished
- async_result is not changed - async_result is not changed
@ -140,7 +140,7 @@
- name: validate response - name: validate response
assert: assert:
that: that:
- async_result.ansible_job_id is match('j\d+\.\d+') - async_result.ansible_job_id|basename is match('j\d+\.\d+')
- async_result.finished == 1 - async_result.finished == 1
- async_result is finished - async_result is finished
- async_result.changed == false - async_result.changed == false
@ -159,7 +159,7 @@
- name: validate response - name: validate response
assert: assert:
that: that:
- async_result.ansible_job_id is match('j\d+\.\d+') - async_result.ansible_job_id|basename is match('j\d+\.\d+')
- async_result.finished == 1 - async_result.finished == 1
- async_result is finished - async_result is finished
- async_result.changed == true - async_result.changed == true
@ -176,7 +176,7 @@
- name: validate response - name: validate response
assert: assert:
that: that:
- async_result.ansible_job_id is match('j\d+\.\d+') - async_result.ansible_job_id|basename is match('j\d+\.\d+')
- async_result.finished == 1 - async_result.finished == 1
- async_result is finished - async_result is finished
- async_result.changed == true - async_result.changed == true

@ -28,7 +28,7 @@
- name: validate that by the end of the retry interval, we succeeded - name: validate that by the end of the retry interval, we succeeded
assert: assert:
that: that:
- async_result.ansible_job_id is match('j\d+\.\d+') - async_result.ansible_job_id|basename is match('j\d+\.\d+')
- async_result.finished == 1 - async_result.finished == 1
- async_result is finished - async_result is finished
- async_result is changed - async_result is changed

@ -136,8 +136,8 @@ set -e
# Check for async output # Check for async output
# NOTE: regex to match 1 or more digits works for both BSD and GNU grep # NOTE: regex to match 1 or more digits works for both BSD and GNU grep
ansible-playbook -i inventory test_async.yml 2>&1 | tee async_test.out ansible-playbook -i inventory test_async.yml 2>&1 | tee async_test.out
grep "ASYNC OK .* jid=j[0-9]\{1,\}" async_test.out grep "ASYNC OK .* jid=.*j[0-9]\{1,\}" async_test.out
grep "ASYNC FAILED .* jid=j[0-9]\{1,\}" async_test.out grep "ASYNC FAILED .* jid=.*j[0-9]\{1,\}" async_test.out
rm -f async_test.out rm -f async_test.out
# Hide skipped # Hide skipped

Loading…
Cancel
Save