From 76814089ae213796030debb9a548686122d409ce Mon Sep 17 00:00:00 2001
From: Adam Miller <admiller@redhat.com>
Date: Tue, 20 May 2014 20:28:14 -0500
Subject: [PATCH 1/2] add async fire-forget/check functionality

---
 docsite/rst/playbooks_async.rst | 22 ++++++++++++++++++++++
 library/internal/async_status   |  2 +-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/docsite/rst/playbooks_async.rst b/docsite/rst/playbooks_async.rst
index 5a82189b65c..62b5c944a81 100644
--- a/docsite/rst/playbooks_async.rst
+++ b/docsite/rst/playbooks_async.rst
@@ -56,6 +56,28 @@ Alternatively, if you do not need to wait on the task to complete, you may
    Using a higher value for ``--forks`` will result in kicking off asynchronous
    tasks even faster.  This also increases the efficiency of polling.
 
+If you would like to perform a variation of the "fire and forget" where you 
+"fire and forget, check on it later" you can perform a task similar to the 
+following::
+
+      --- 
+      # Requires ansible 1.7+
+      - name: 'YUM - fire and forget task'
+        yum: name=docker-io state=installed
+        async: 1000
+        poll: 0
+        register: yum_sleeper
+
+      - name: 'YUM - check on fire and forget task'
+        async_status: jid={{ yum_sleeper.ansible_job_id }}
+        register: job_result
+        until: job_result.finished
+        retries: 30
+
+.. note::
+   If the value of ``async:`` is not high enough, this will cause the 
+   "check on it later" task to fail because the temporary status file that
+   the ``async_status:`` is looking for will not have been written 
 
 .. seealso::
 
diff --git a/library/internal/async_status b/library/internal/async_status
index 1605f877a46..f991b50064b 100644
--- a/library/internal/async_status
+++ b/library/internal/async_status
@@ -80,7 +80,7 @@ def main():
     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)
+            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)

From 21dcfd7192c2546f5394039edefd27c51e9eb018 Mon Sep 17 00:00:00 2001
From: James Cammarata <jimi@sngx.net>
Date: Thu, 25 Sep 2014 15:42:45 -0500
Subject: [PATCH 2/2] Adding integration tests for async fire-and-forget
 checking

Also updated the CHANGELOG for the feature
---
 CHANGELOG.md                                  |  1 +
 .../roles/test_async/tasks/main.yml           | 25 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 03564893b0c..0b84708e220 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ Major changes:
 * command_warnings feature will warn about when usage of the shell/command module can be simplified to use core modules - this can be enabled in ansible.cfg
 * new omit value can be used to leave off a parameter when not set, like so module_name: a=1 b={{ c | default(omit) }}, would not pass value for b (not even an empty value) if c was not set.
 * developers: 'baby JSON' in module responses, originally intended for writing modules in bash, is removed as a feature to simplify logic, script module remains available for running bash scripts.
+* async jobs started in "fire & forget" mode can now be checked on at a later time.
 
 New Modules:
 
diff --git a/test/integration/roles/test_async/tasks/main.yml b/test/integration/roles/test_async/tasks/main.yml
index 556284770ac..0b9991ec049 100644
--- a/test/integration/roles/test_async/tasks/main.yml
+++ b/test/integration/roles/test_async/tasks/main.yml
@@ -62,3 +62,28 @@
   async: 15
   poll: 0
   when: False
+
+# test async "fire and forget, but check later"
+
+- name: 'start a task with "fire-and-forget"'
+  command: sleep 15
+  async: 30
+  poll: 0
+  register: fnf_task
+
+- name: assert task was successfully started
+  assert:
+    that:
+    - fnf_task.started
+    - "'ansible_job_id' in fnf_task"
+
+- name: 'check on task started as a "fire-and-forget"'
+  async_status: jid={{ fnf_task.ansible_job_id }}
+  register: fnf_result
+  until: fnf_result.finished
+  retries: 30
+
+- name: assert task was successfully checked
+  assert:
+    that:
+    - fnf_result.finished