2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# 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
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['stableinterface'],
|
|
|
|
'supported_by': 'core'}
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: async_status
|
|
|
|
short_description: Obtain status of asynchronous task
|
|
|
|
description:
|
2017-06-26 15:26:53 +00:00
|
|
|
- This module gets the status of an asynchronous task.
|
|
|
|
- This module is also supported for Windows targets.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "0.5"
|
|
|
|
options:
|
|
|
|
jid:
|
|
|
|
description:
|
|
|
|
- Job or task identifier
|
|
|
|
required: true
|
|
|
|
mode:
|
|
|
|
description:
|
2018-01-16 05:15:04 +00:00
|
|
|
- if C(status), obtain the status; if C(cleanup), clean up the async job cache (by default in C(~/.ansible_async/)) for the specified job I(jid).
|
2014-09-26 01:01:01 +00:00
|
|
|
choices: [ "status", "cleanup" ]
|
|
|
|
default: "status"
|
|
|
|
notes:
|
2018-04-22 07:15:16 +00:00
|
|
|
- See also U(https://docs.ansible.com/playbooks_async.html)
|
2017-06-26 15:26:53 +00:00
|
|
|
- This module is also supported for Windows targets.
|
2017-01-27 23:20:31 +00:00
|
|
|
author:
|
2015-06-15 19:53:30 +00:00
|
|
|
- "Ansible Core Team"
|
|
|
|
- "Michael DeHaan"
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2016-08-31 15:18:54 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
2014-09-26 01:01:01 +00:00
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=dict(
|
|
|
|
jid=dict(required=True),
|
2017-08-10 08:36:28 +00:00
|
|
|
mode=dict(default='status', choices=['status', 'cleanup']),
|
2014-09-26 01:01:01 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
mode = module.params['mode']
|
2017-08-10 08:36:28 +00:00
|
|
|
jid = module.params['jid']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-01-16 05:15:04 +00:00
|
|
|
async_dir = os.environ.get('ANSIBLE_ASYNC_DIR', '~/.ansible_async')
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
# setup logging directory
|
2018-01-16 05:15:04 +00:00
|
|
|
logdir = os.path.expanduser(async_dir)
|
2014-09-26 01:01:01 +00:00
|
|
|
log_path = os.path.join(logdir, jid)
|
|
|
|
|
|
|
|
if not os.path.exists(log_path):
|
2015-11-13 00:19:52 +00:00
|
|
|
module.fail_json(msg="could not find job", ansible_job_id=jid, started=1, finished=1)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-07-07 18:41:35 +00:00
|
|
|
data = None
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
2016-09-03 01:35:14 +00:00
|
|
|
data = open(log_path).read()
|
2014-09-26 01:01:01 +00:00
|
|
|
data = json.loads(data)
|
2016-06-13 18:40:13 +00:00
|
|
|
except Exception:
|
2016-07-07 18:41:35 +00:00
|
|
|
if not data:
|
2014-09-26 01:01:01 +00:00
|
|
|
# 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,
|
2017-08-10 08:36:28 +00:00
|
|
|
msg="Could not parse job output: %s" % data, started=1, finished=1)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-10 08:36:28 +00:00
|
|
|
if 'started' not in data:
|
2014-09-26 01:01:01 +00:00
|
|
|
data['finished'] = 1
|
|
|
|
data['ansible_job_id'] = jid
|
2016-07-07 18:41:35 +00:00
|
|
|
elif 'finished' not in data:
|
|
|
|
data['finished'] = 0
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Fix error: TypeError: exit_json() keywords must be strings
|
2016-08-31 15:18:54 +00:00
|
|
|
data = dict([(str(k), v) for k, v in iteritems(data)])
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
module.exit_json(**data)
|
|
|
|
|
2016-12-05 18:00:03 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|