title: Ansible: How do I execute tasks in ansible playbook in parallel with some time gap
body: <p>Let me try and explain my need:</p>
  
  <p>As part of the regular deployment of our application, we have a SQL script(which would alter tables, add tables or update, etc) which needs to be run on 3 schemas in a region and 5 schemas in another for example. The application is in AWS and the database is Arora db(RDS)- MySQL. This schema can take anywhere between 30 minutes to 3 hours.</p>
  
  <p><strong>This SQL script needs to be run in parallel and with a delay of 2 minutes between each schema run.</strong></p>
  
  <p>This is what I have achieved till now:</p>
  
  <ol>
  <li>A file having DB details- dbdata.yml</li>
  </ol>
  
  <pre><code>---
  
  conn_details:
      - { host: localhost, user: root, password: "Password1!" }
      - { host: localhost, user: root, password: "Password1!" }
  </code></pre>
  
  <ol start="2">
  <li>The playbook:</li>
  </ol>
  
  <pre><code>- hosts: localhost
  
    vars:
      script_file: "{{ path }}"
    vars_files:
      - dbdata.yml
    tasks:
    - name: shell command to execute script in parallel
      shell: |
        sleep 30s
        "mysql -h {{ item.host }} -u {{ item.user }} -p{{ item.password }} &lt; {{ script_file }} &gt;&gt; /usr/local/testscript.log"
      with_items: "{{ conn_details }}"
      register: sql_query_output
      async: 600
      poll: 0
  
    - name: Wait for sql execution to finish
      async_status:
        jid: "{{ item.ansible_job_id }}"
      register: _jobs
      until: _jobs.finished
      delay: 20  # Check every 20 seconds. Adjust as you like.
      retries: 10
      with_items: "{{ sql_query_output.results }}"
  </code></pre>
  
  <p>1st part- executes the script in parallel and this also includes a time gap of 30 seconds before each execution.</p>
  
  <p>2nd part- picks the <strong>ansible job id</strong> from the registered output and checks if the job is completed or not.</p>
  
  <p>Please note: before including the 30 seconds sleep, this playbook was working fine.</p>
  
  <p>We have following erroneous output upon execution:</p>
  
  <pre><code>ansible-playbook parallel_local.yml --extra-vars "path=RDS_script.sql"
   [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
  
  
  PLAY [localhost] **************************************************************************************************************************************************************************************************
  
  TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
  ok: [localhost]
  
  TASK [sample command- ansible-playbook my_sqldeploy.yml --extra-vars "path=/home/NICEONDEMAND/bsahu/RDS_create_user1.sql"] ****************************************************************************************
  changed: [localhost] =&gt; (item={u'host': u'localhost', u'password': u'Password1!', u'user': u'root'})
  changed: [localhost] =&gt; (item={u'host': u'localhost', u'password': u'Password1!', u'user': u'root'})
  
  TASK [Wait for creation to finish] ********************************************************************************************************************************************************************************
  FAILED - RETRYING: Wait for creation to finish (10 retries left).
  FAILED - RETRYING: Wait for creation to finish (9 retries left).
  failed: [localhost] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'591787538842.77844', 'item': {u'host': u'localhost', u'password': u'Password1!', u'user': u'root'}, u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/591787538842.77844'}) =&gt; {"ansible_job_id": "591787538842.77844", "ansible_loop_var": "item", "attempts": 3, "changed": true, "cmd": "sleep 30s\n\"mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log\"\n", "delta": "0:00:30.073191", "end": "2019-11-28 17:01:57.632285", "finished": 1, "item": {"ansible_job_id": "591787538842.77844", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": {"host": "localhost", "password": "Password1!", "user": "root"}, "results_file": "/root/.ansible_async/591787538842.77844", "started": 1}, "msg": "non-zero return code", "rc": 127, "start": "2019-11-28 17:01:27.559094", "stderr": "/bin/sh: line 1: mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log: No such file or directory", "stderr_lines": ["/bin/sh: line 1: mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log: No such file or directory"], "stdout": "", "stdout_lines": []}
  failed: [localhost] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'999397686792.77873', 'item': {u'host': u'localhost', u'password': u'Password1!', u'user': u'root'}, u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/999397686792.77873'}) =&gt; {"ansible_job_id": "999397686792.77873", "ansible_loop_var": "item", "attempts": 1, "changed": true, "cmd": "sleep 30s\n\"mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log\"\n", "delta": "0:00:30.120136", "end": "2019-11-28 17:01:58.694713", "finished": 1, "item": {"ansible_job_id": "999397686792.77873", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": {"host": "localhost", "password": "Password1!", "user": "root"}, "results_file": "/root/.ansible_async/999397686792.77873", "started": 1}, "msg": "non-zero return code", "rc": 127, "start": "2019-11-28 17:01:28.574577", "stderr": "/bin/sh: line 1: mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log: No such file or directory", "stderr_lines": ["/bin/sh: line 1: mysql -h localhost -u root -pPassword1! &lt; RDS_script.sql &gt;&gt; /usr/local/testscript.log: No such file or directory"], "stdout": "", "stdout_lines": []}
  
  PLAY RECAP ********************************************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
  </code></pre>
  
  <p>Any suggestions how to overcome this. Thanks in advance for all the help.</p>