Unable to grab success or failure i.e rc of long running Ansible task in a loop

368 Views Asked by At

Below is my code that takes about 20 minutes in all. The task takes about 1 minute and the loop runs 20 times so 20X1=20minutes. I want the playbook to fail if any task in shell module failed i.e rc != 0

        - name: Check DB connection with DBPING
          shell: "java utils.dbping ORACLE_THIN {{ item | trim }}"
          with_items: "{{ db_conn_dets }}"
          delegate_to: localhost

Inorder, to speedup the tasks I decided to run them in parallel and then grab the success or failure of each tasks like below.

        - name: Check DB connection with DBPING
          shell: "java utils.dbping ORACLE_THIN {{ item | trim }}"
          with_items: "{{ db_conn_dets }}"
          delegate_to: localhost
          async: 600
          poll: 0
          register: dbcheck

        - name: Result check
          async_status:
            jid: "{{ item.ansible_job_id }}"
          register: _jobs
          until: _jobs.finished
          delay: 10
          retries: 20
          with_items: "{{ dbcheck.results }}"

Now !! The task Check DB connection with DBPING completes in less than a minute for the entire loop. However, the problem is that ALL the task Result check fails even if the task Check DB connection with DBPING WOULD EVENTUALLY SUCCEED i.e rc=0

Below is one sample failing task Result check when it should have been successful

 failed: [remotehost] (item={'_ansible_parsed': True, '_ansible_item_result': True, '_ansible_no_log': False, u'ansible_job_id': u'234197058177.18294', '_ansible_delegated_vars': {'ansible_delegated_host': u'localhost', 'ansible_host': u'localhost'}, u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/home/ansbladm/.ansible_async/234197058177.18294', 'item': u'dbuser mypass dbhost:1521:mysid', '_ansible_ignore_errors': None}) => {
     "ansible_job_id": "234197058177.18294", 
     "attempts": 1, 
     "changed": false, 
     "finished": 1, 
     "invocation": {
         "module_args": {
             "jid": "234197058177.18294", 
             "mode": "status"
         }
     }, 
     "item": {
         "ansible_job_id": "234197058177.18294", 
         "changed": true, 
         "failed": false, 
         "finished": 0, 
         "item": "dbuser mypass dbhost:1521:mysid", 
         "results_file": "/home/ansbladm/.ansible_async/234197058177.18294", 
         "started": 1
     }, 
     "msg": "could not find job", 
     "started": 1
 }

Can you please let me know what is the issue with my task Result check and how can i make sure it keeps trying till the task Check DB connection with DBPING completes and thereby reports success or failure upon the .rc of shell module?

It will also be great if i could get the the debug module to print all failed shell tasks of Check DB connection with DBPING.

Kindly suggest.

1

There are 1 best solutions below

0
Ashar On

Although this is a partial answer to the set of queries i had. I will accept a complete answer if it comes in the future.

Adding delegate_to: localhost to task Result check inoder to resolve the behavior.

I'm however still waiting for the code to show all failed tasks in the debug.