Is there a way to register multiple async tasks to the same variable? For example if I have two tasks that each invoke an async shell command:
- name: Run async task 1
shell: echo "task 1"
async: 30
poll: 0
register: db_wait
- name: Run async task 2
shell: echo "task 2"
async: 30
poll: 0
register: db_wait
- debug: msg="task vars {{db_wait}}"
When I print the db_wait variable it only contains the reference of one task.
"msg": "task vars {u'started': 1, u'results_file': u'/home/vagrant/.ansible_async/202582702042.7326', u'ansible_job_id': u'202582702042.7326', u'changed': False}"
Is there a way to register the same variable for the async tasks or some sort of list I can add to and can iterate at a later point?
I came up with the following solution in the end, hopefully it may help other people. The core of the problem is I needed to run some long running tasks on different hosts in different plays, but only want to gather the results at the very end of the playbook.
For the plays to run the jobs I have:
---
- name: Long Running tasks 1
hosts: remote_host_1
tasks:
- name: Tasks 1
shell: "{{item}}"
with_items:
- sleep 10
- echo "Another Task"
async: 3600
poll: 0
register: wait_task1
- name: Long Running tasks 2
hosts: remote_host_2
tasks:
- name: Tasks 2
shell: "{{item}}"
with_items:
- sleep 20
async: 3600
poll: 0
register: wait_task2
Then the final plays were to check the results:
- name: Verify Async Tasks 1
hosts: remote_host_1
tasks:
- include: "/ansible/plays/check_results.yml wait={{wait_task1}}"
- name: Verify Async Tasks 2
hosts: remote_host_2
tasks:
- include: "/ansible/plays/check_results.yml wait={{wait_task2}}"
And the check results looks familiar to people who have used this mechanism:
---
- name: Check Results
async_status: jid={{ item.ansible_job_id }}
register: task_result
until: task_result.finished
retries: 120
delay: 30
failed_when: task_result.rc != 0
with_items: wait.results
This kicks off all tasks asynchronously and then we can gather the results at the end for each play.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With