From 6840b79e560b627b26fc5e6e646510ec478988a2 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 4 Jun 2021 13:01:26 -0400 Subject: [PATCH] bring back async_status module for direct callers also, deprecate it, they should use action instead Co-authored-by: Felix Fontein --- .../fragments/async_unremove_redeprecate.yml | 2 + lib/ansible/modules/async_status.py | 65 +++++++++++++++++++ test/sanity/ignore.txt | 2 + 3 files changed, 69 insertions(+) create mode 100644 changelogs/fragments/async_unremove_redeprecate.yml diff --git a/changelogs/fragments/async_unremove_redeprecate.yml b/changelogs/fragments/async_unremove_redeprecate.yml new file mode 100644 index 00000000000..d57187226a4 --- /dev/null +++ b/changelogs/fragments/async_unremove_redeprecate.yml @@ -0,0 +1,2 @@ +bugfixes: + - async_status, resurrected module to deprecate for those that were invoking it directly. diff --git a/lib/ansible/modules/async_status.py b/lib/ansible/modules/async_status.py index 10af8dec225..c8ef9fc97f9 100644 --- a/lib/ansible/modules/async_status.py +++ b/lib/ansible/modules/async_status.py @@ -87,3 +87,68 @@ erased: returned: when file is erased type: str ''' + +import json +import os + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.six import iteritems +from ansible.module_utils._text import to_native + + +def main(): + + module = AnsibleModule(argument_spec=dict( + jid=dict(type='str', required=True), + mode=dict(type='str', default='status', choices=['cleanup', 'status']), + # passed in from the async_status action plugin + _async_dir=dict(type='path', required=True), + )) + + module.deprecate("The async_status module should not be called directly anymore, use the action plugin instead", version='2.16') + + mode = module.params['mode'] + jid = module.params['jid'] + async_dir = module.params['_async_dir'] + + # setup logging directory + logdir = os.path.expanduser(async_dir) + log_path = os.path.join(logdir, jid) + + if not os.path.exists(log_path): + module.fail_json(msg="could not find job", ansible_job_id=jid, started=1, finished=1) + + if mode == 'cleanup': + os.unlink(log_path) + module.exit_json(ansible_job_id=jid, erased=log_path) + + # NOT in cleanup mode, assume regular status mode + # no remote kill mode currently exists, but probably should + # consider log_path + ".pid" file and also unlink that above + + data = None + try: + with open(log_path) as f: + data = json.loads(f.read()) + except Exception: + if not data: + # file not written yet? That means it is running + module.exit_json(results_file=log_path, ansible_job_id=jid, started=1, finished=0) + else: + module.fail_json(ansible_job_id=jid, results_file=log_path, + msg="Could not parse job output: %s" % data, started=1, finished=1) + + if 'started' not in data: + data['finished'] = 1 + data['ansible_job_id'] = jid + elif 'finished' not in data: + data['finished'] = 0 + + # Fix error: TypeError: exit_json() keywords must be strings + data = dict([(to_native(k), v) for k, v in iteritems(data)]) + + module.exit_json(**data) + + +if __name__ == '__main__': + main() diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 9c9886d5643..ce89b04a163 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -58,6 +58,8 @@ lib/ansible/modules/apt.py validate-modules:parameter-invalid lib/ansible/modules/apt_key.py validate-modules:parameter-type-not-in-doc lib/ansible/modules/apt_repository.py validate-modules:parameter-invalid lib/ansible/modules/assemble.py validate-modules:nonexistent-parameter-documented +lib/ansible/modules/async_status.py use-argspec-type-path +lib/ansible/modules/async_status.py validate-modules!skip lib/ansible/modules/async_wrapper.py ansible-doc!skip # not an actual module lib/ansible/modules/async_wrapper.py pylint:ansible-bad-function # ignore, required lib/ansible/modules/async_wrapper.py use-argspec-type-path