|
|
|
@ -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()
|
|
|
|
|