2012-03-03 17:41:22 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-03 17:41:22 +00:00
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2012-10-02 07:56:55 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: async_status
|
|
|
|
short_description: Obtain status of asynchronous task
|
|
|
|
description:
|
2013-11-08 19:59:24 +00:00
|
|
|
- "This module gets the status of an asynchronous task."
|
2013-11-28 02:23:03 +00:00
|
|
|
version_added: "0.5"
|
2012-10-02 07:56:55 +00:00
|
|
|
options:
|
|
|
|
jid:
|
|
|
|
description:
|
|
|
|
- Job or task identifier
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
mode:
|
|
|
|
description:
|
|
|
|
- if C(status), obtain the status; if C(cleanup), clean up the async job cache
|
|
|
|
located in C(~/.ansible_async/) for the specified job I(jid).
|
|
|
|
required: false
|
|
|
|
choices: [ "status", "cleanup" ]
|
|
|
|
default: "status"
|
|
|
|
notes:
|
2013-11-08 19:59:24 +00:00
|
|
|
- See also U(http://www.ansibleworks.com/docs/playbooks_async.html#asynchronous-actions-and-polling)
|
2012-10-02 07:56:55 +00:00
|
|
|
requirements: []
|
|
|
|
author: Michael DeHaan
|
|
|
|
'''
|
|
|
|
|
2012-03-03 17:41:22 +00:00
|
|
|
import datetime
|
|
|
|
import traceback
|
2012-07-24 23:03:18 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=dict(
|
|
|
|
jid=dict(required=True),
|
|
|
|
mode=dict(default='status', choices=['status','cleanup']),
|
|
|
|
))
|
|
|
|
|
|
|
|
mode = module.params['mode']
|
|
|
|
jid = module.params['jid']
|
|
|
|
|
|
|
|
# setup logging directory
|
|
|
|
logdir = os.path.expanduser("~/.ansible_async")
|
|
|
|
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)
|
|
|
|
|
|
|
|
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 = file(log_path).read()
|
|
|
|
try:
|
|
|
|
data = json.loads(data)
|
|
|
|
except Exception, e:
|
|
|
|
if data == '':
|
|
|
|
# file not written yet? That means it is running
|
|
|
|
module.exit_json(results_file=log_path, ansible_job_id=jid, started=1)
|
|
|
|
else:
|
2012-10-21 02:51:36 +00:00
|
|
|
module.fail_json(ansible_job_id=jid, results_file=log_path,
|
2012-07-24 23:03:18 +00:00
|
|
|
msg="Could not parse job output: %s" % data)
|
|
|
|
|
2012-08-11 16:35:58 +00:00
|
|
|
if not 'started' in data:
|
2012-07-24 23:03:18 +00:00
|
|
|
data['finished'] = 1
|
|
|
|
data['ansible_job_id'] = jid
|
2012-12-17 20:07:36 +00:00
|
|
|
|
|
|
|
# Fix error: TypeError: exit_json() keywords must be strings
|
|
|
|
data = dict([(str(k), v) for k, v in data.iteritems()])
|
|
|
|
|
2012-07-24 23:03:18 +00:00
|
|
|
module.exit_json(**data)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2012-07-24 23:03:18 +00:00
|
|
|
main()
|