Kamil Sacharczuk
Kamil Sacharczuk

Reputation: 23

Ansible async task collecting results: could not find job

I'm trying to fire-and-forget some tasks and collect results after that. Here's my playbook:

---
- hosts: node2
  gather_facts: yes 
  tasks:
    - name: 'Some long script no 1 on node2'
      shell: "time sleep $[ ( $RANDOM % 20 )  + 20 ]s" 
      async: 40
      poll: 0
      register: script1 

    - name: 'Another long script no 2 on node2'
      shell: "time sleep $[ ( $RANDOM % 20 )  + 20 ]s" 
      async: 40
      poll: 0
      register: script2

- hosts: node2
  tasks:
    - name: "Collect results"
      async_status:
        jid: loop_item.ansible_job_id
      loop:
        - script1
        - script2
      loop_control:
        loop_var: loop_item
      register: async_poll_results
      until: async_poll_results.finished
      retries: 30 

When I run it, I receive following error:

PLAY [node2]     ****************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [hostname]

TASK [Some long script no 1 on node2] ***************************************************************************************************
changed: [hostname] => {"ansible_job_id": "814448842231.125544", "changed": true, "finished": 0, "results_file": "/home/external.kamil.sacharczuk/.ansible_async/814448842231.125544", "started": 1}

TASK [Another long script no 2 on node2] ************************************************************************************************
changed: [hostname] => {"ansible_job_id": "586999441005.125616", "changed": true, "finished": 0, "results_file": "/home/external.kamil.sacharczuk/.ansible_async/586999441005.125616", "started": 1}

PLAY [node2] ****************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
ok: [hostname]

TASK [Collect results] ******************************************************************************************************************
failed: [hostname] (item=script1) => {"ansible_job_id": "loop_item.ansible_job_id", "attempts": 1, "changed": false, "finished": 1, "loop_item": "script1", "msg": "could not find job", "started": 1}
failed: [hostname] (item=script2) => {"ansible_job_id": "loop_item.ansible_job_id", "attempts": 1, "changed": false, "finished": 1, "loop_item": "script2", "msg": "could not find job", "started": 1}
to retry, use: --limit @xxxxxxxxx

PLAY RECAP ******************************************************************************************************************************
hostname         : ok=4    changed=2    unreachable=0    failed=1   

Don't really know why I receive this "could not find job". I tried to run this "collect" tasks localy first, than I figured out, that this job results are stored on node2, so I run it there. Tried with or without gathering facts. Tried also use

hostvars['hostname'][loop_item][ansible_job_id]

but this gave me same error as here.

Any help would be much appreciated!

PS. I am running ansible 2.6.1

Upvotes: 1

Views: 6996

Answers (2)

George Shuklin
George Shuklin

Reputation: 7897

If someone got here searching for this error message, there is an another reason for the 'could not find job' error. If a job was run as become, so should be an async_status job. If you try to async_status for become-job without adding become to the async_status module call, it will fail with this message.

Upvotes: 21

Random_Automation
Random_Automation

Reputation: 452

Please try as below with proper quotation::

   - hosts: localhost
     gather_facts: false
     tasks:
      - name: 'Some long script no 1 on node2'
        shell: "time sleep $[ ( $RANDOM % 20 )  + 20 ]s"
        async: 40
        poll: 0
        register: script1

      - name: 'Another long script no 2 on node2'
        shell: "time sleep $[ ( $RANDOM % 20 )  + 20 ]s"
        async: 40
        poll: 0
        register: script2

   - hosts: localhost
     tasks:
      - name: "Collect results"
        async_status:
           jid: "{{ loop_item.ansible_job_id }}"
        loop:
           - "{{ script1 }}"
           - "{{ script2 }}"
        loop_control:
           loop_var: loop_item
        register: async_poll_results
        until: async_poll_results.finished
        retries: 30

Upvotes: 1

Related Questions