2018-04-20 19:17:02 +00:00
|
|
|
|
.. _playbooks_async:
|
|
|
|
|
|
2013-09-29 23:03:51 +00:00
|
|
|
|
Asynchronous Actions and Polling
|
|
|
|
|
================================
|
|
|
|
|
|
|
|
|
|
By default tasks in playbooks block, meaning the connections stay open
|
|
|
|
|
until the task is done on each node. This may not always be desirable, or you may
|
|
|
|
|
be running operations that take longer than the SSH timeout.
|
|
|
|
|
|
2018-02-13 15:23:55 +00:00
|
|
|
|
To avoid blocking or timeout issues, you can use asynchronous mode to run all of your tasks at once and then poll until they are done.
|
2013-09-29 23:03:51 +00:00
|
|
|
|
|
|
|
|
|
To launch a task asynchronously, specify its maximum runtime
|
|
|
|
|
and how frequently you would like to poll for status. The default
|
|
|
|
|
poll value is 10 seconds if you do not specify a value for `poll`::
|
|
|
|
|
|
|
|
|
|
---
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2013-09-29 23:03:51 +00:00
|
|
|
|
- hosts: all
|
|
|
|
|
remote_user: root
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2013-09-29 23:03:51 +00:00
|
|
|
|
tasks:
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2014-06-12 07:12:52 +00:00
|
|
|
|
- name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
|
2013-09-29 23:03:51 +00:00
|
|
|
|
command: /bin/sleep 15
|
|
|
|
|
async: 45
|
|
|
|
|
poll: 5
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
There is no default for the async time limit. If you leave off the
|
|
|
|
|
'async' keyword, the task runs synchronously, which is Ansible's
|
|
|
|
|
default.
|
|
|
|
|
|
|
|
|
|
Alternatively, if you do not need to wait on the task to complete, you may
|
2017-11-14 02:32:37 +00:00
|
|
|
|
run the task asynchronously by specifying a poll value of 0::
|
2013-09-29 23:03:51 +00:00
|
|
|
|
|
|
|
|
|
---
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2013-09-29 23:03:51 +00:00
|
|
|
|
- hosts: all
|
|
|
|
|
remote_user: root
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2013-09-29 23:03:51 +00:00
|
|
|
|
tasks:
|
2014-02-28 19:18:44 +00:00
|
|
|
|
|
2014-06-12 07:12:52 +00:00
|
|
|
|
- name: simulate long running op, allow to run for 45 sec, fire and forget
|
2013-09-29 23:03:51 +00:00
|
|
|
|
command: /bin/sleep 15
|
|
|
|
|
async: 45
|
|
|
|
|
poll: 0
|
|
|
|
|
|
|
|
|
|
.. note::
|
2017-11-14 02:32:37 +00:00
|
|
|
|
You shouldn't attempt run a task asynchronously by specifying a poll value of 0:: to with operations that require
|
|
|
|
|
exclusive locks (such as yum transactions) if you expect to run other
|
2013-09-29 23:03:51 +00:00
|
|
|
|
commands later in the playbook against those same resources.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
Using a higher value for ``--forks`` will result in kicking off asynchronous
|
|
|
|
|
tasks even faster. This also increases the efficiency of polling.
|
|
|
|
|
|
2018-04-16 00:32:11 +00:00
|
|
|
|
If you would like to perform a task asynchronously and check on it later you can perform a task similar to the
|
2014-05-21 01:28:14 +00:00
|
|
|
|
following::
|
|
|
|
|
|
2017-11-14 02:32:37 +00:00
|
|
|
|
---
|
2014-09-26 00:16:41 +00:00
|
|
|
|
# Requires ansible 1.8+
|
2017-11-14 02:32:37 +00:00
|
|
|
|
- name: 'YUM - async task'
|
|
|
|
|
yum:
|
|
|
|
|
name: docker-io
|
|
|
|
|
state: installed
|
2014-05-21 01:28:14 +00:00
|
|
|
|
async: 1000
|
|
|
|
|
poll: 0
|
|
|
|
|
register: yum_sleeper
|
|
|
|
|
|
2017-11-14 02:32:37 +00:00
|
|
|
|
- name: 'YUM - check on async task'
|
|
|
|
|
async_status:
|
|
|
|
|
jid: "{{ yum_sleeper.ansible_job_id }}"
|
2014-05-21 01:28:14 +00:00
|
|
|
|
register: job_result
|
|
|
|
|
until: job_result.finished
|
|
|
|
|
retries: 30
|
|
|
|
|
|
|
|
|
|
.. note::
|
2017-11-14 02:32:37 +00:00
|
|
|
|
If the value of ``async:`` is not high enough, this will cause the
|
2014-05-21 01:28:14 +00:00
|
|
|
|
"check on it later" task to fail because the temporary status file that
|
2017-11-14 02:32:37 +00:00
|
|
|
|
the ``async_status:`` is looking for will not have been written or no longer exist
|
2013-10-05 16:31:16 +00:00
|
|
|
|
|
2017-08-18 20:02:35 +00:00
|
|
|
|
If you would like to run multiple asynchronous tasks while limiting the amount
|
|
|
|
|
of tasks running concurrently, you can do it this way::
|
|
|
|
|
|
|
|
|
|
#####################
|
|
|
|
|
# main.yml
|
|
|
|
|
#####################
|
|
|
|
|
- name: Run items asynchronously in batch of two items
|
|
|
|
|
vars:
|
|
|
|
|
sleep_durations:
|
|
|
|
|
- 1
|
|
|
|
|
- 2
|
|
|
|
|
- 3
|
|
|
|
|
- 4
|
|
|
|
|
- 5
|
|
|
|
|
durations: "{{ item }}"
|
2017-09-17 18:02:46 +00:00
|
|
|
|
include_tasks: execute_batch.yml
|
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 03:32:34 +00:00
|
|
|
|
loop:
|
2017-08-18 20:02:35 +00:00
|
|
|
|
- "{{ sleep_durations | batch(2) | list }}"
|
|
|
|
|
|
|
|
|
|
#####################
|
|
|
|
|
# execute_batch.yml
|
|
|
|
|
#####################
|
|
|
|
|
- name: Async sleeping for batched_items
|
|
|
|
|
command: sleep {{ async_item }}
|
|
|
|
|
async: 45
|
|
|
|
|
poll: 0
|
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 03:32:34 +00:00
|
|
|
|
loop: "{{ durations }}"
|
2017-08-18 20:02:35 +00:00
|
|
|
|
loop_control:
|
|
|
|
|
loop_var: "async_item"
|
|
|
|
|
register: async_results
|
|
|
|
|
|
|
|
|
|
- name: Check sync status
|
|
|
|
|
async_status:
|
|
|
|
|
jid: "{{ async_result_item.ansible_job_id }}"
|
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 03:32:34 +00:00
|
|
|
|
loop: "{{ async_results.results }}"
|
2017-08-18 20:02:35 +00:00
|
|
|
|
loop_control:
|
|
|
|
|
loop_var: "async_result_item"
|
|
|
|
|
register: async_poll_results
|
|
|
|
|
until: async_poll_results.finished
|
|
|
|
|
retries: 30
|
|
|
|
|
|
2013-10-05 16:31:16 +00:00
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
|
|
:doc:`playbooks`
|
|
|
|
|
An introduction to playbooks
|
2018-07-21 13:48:47 +00:00
|
|
|
|
`User Mailing List <https://groups.google.com/group/ansible-devel>`_
|
2013-10-05 16:31:16 +00:00
|
|
|
|
Have a question? Stop by the google group!
|
|
|
|
|
`irc.freenode.net <http://irc.freenode.net>`_
|
|
|
|
|
#ansible IRC chat channel
|
|
|
|
|
|