c05b61777c
* Update windows_setup.rst (#55535): Wrong protocol and port in command. (cherry picked from commit6ea3eca8ff
) * Clarify the two targets of vault encryption, with notes about advantages and drawbacks of each Co-Authored-By: tacatac <taca@kadisius.eu> (cherry picked from commit79198cad7a
) * Improve consistency of loop documentation (#55674) (cherry picked from commita5cb47d697
) * Add Microsoft Document URL for WinRM Memory Hotfix (#55680) Co-Authored-By: hiyokotaisa <thel.vadam2485@gmail.com> (cherry picked from commit7b86208fcd
) * Clarify the documentation for `async` and `poll`; describe the behavior when `poll` = 0 and when it does not. Co-Authored-By: tacatac <taca@kadisius.eu> (cherry picked from commitdbc64ae64c
) * Add security group info and example to AWS guide (#55783): expand documentation on how to use lookup plugin aws_service_ip_ranges with ec2_group module (cherry picked from commitbb5059f2c7
) * correct description of modules vs plugins (#55784) (cherry picked from commit9d5b5d7ddd
) * Fix var naming (#55795): Make vars match tasks in Google Compute guide. (cherry picked from commit943f7334c5
) * Clarifies how Ansible processes multiple `failed_when` conditions (#55941): multiple failed_when conditions join with AND not OR to counter third-party pages online incorrectly stating that it uses `OR`. ([example](https://groups.google.com/d/msg/ansible-project/cIaQTmY3ZLE/c5w8rlmdHWIJ)). (cherry picked from commit5439eb8bd8
) * Docs: edits & expands module_utils & search path info in dev guide (#55931) (cherry picked from commit8542459b95
) * Add faq note about ssh ServerAliveInterval (#55568) (cherry picked from commit76dba7aa4f
) * docsite: correct path, list requirements for testing module docs, etc. (#52008) * dev_guide: correct path, list requirements, etc.; module HTML docs are in '_build/html/module' subdir (cherry picked from commitb14f477bee
) * Developer documentation update involving module invocation (#55747) * Update docs for the 2.7 change to AnsiballZ which invokes modules with one less Python interpreter * Add a section on how module results are returned and on trust between modules, action plugins, and the executor. * Update docs/docsite/rst/dev_guide/developing_program_flow_modules.rst Co-Authored-By: abadger <a.badger@gmail.com> (cherry picked from commitedafa71f42
) * add doc example of multiline failed_when with OR (#56007) * add variety to multiple OR failed_when doc example (cherry picked from commit7d5ada7161
) * Note that by default the regex test is identical to match, but can do much more (#50205) * Note that the regex test behaves like 'match', with default settings (cherry picked from commit86e98c5213
) * more info on how vaults work (#56183) also add warning about what it covers. (cherry picked from commit8ff27c4e0c
) * Fix var naming in GCE guide (cherry picked from commitdae5564e2b
) * dev_guide: Various small updates (#53273) * Document the clarifications that I usually remark when doing reviews * Update docs/docsite/rst/dev_guide/developing_modules_documenting.rst Co-Authored-By: dagwieers <dag@wieers.com> (cherry picked from commiteac7f1fb58
) * Lack of "--update" flag in older Ubuntu distros (#56283): when installing on older Ubuntu distributions be aware of the lack of ``-u`` or ``--update`` flag. (cherry picked from commitdd0b0ae47b
) * should have gone into 52373 (#56306) (cherry picked from commit3c8d8b1509
)
157 lines
5 KiB
ReStructuredText
157 lines
5 KiB
ReStructuredText
.. _playbooks_async:
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
The behaviour of asynchronous mode depends on the value of `poll`.
|
|
|
|
|
|
Avoid connection timeouts: poll > 0
|
|
-----------------------------------
|
|
|
|
When ``poll`` is a positive value, the playbook will *still* block on the task until it either completes, fails or times out.
|
|
|
|
In this case, however, `async` explicitly sets the timeout you wish to apply to this task rather than being limited by the connection method timeout.
|
|
|
|
To launch a task asynchronously, specify its maximum runtime
|
|
and how frequently you would like to poll for status. The default
|
|
poll value is 15 seconds if you do not specify a value for `poll`::
|
|
|
|
---
|
|
|
|
- hosts: all
|
|
remote_user: root
|
|
|
|
tasks:
|
|
|
|
- name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
|
|
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.
|
|
|
|
.. note::
|
|
As of Ansible 2.3, async does not support check mode and will fail the
|
|
task when run in check mode. See :doc:`playbooks_checkmode` on how to
|
|
skip a task in check mode.
|
|
|
|
|
|
Concurrent tasks: poll = 0
|
|
--------------------------
|
|
|
|
When ``poll`` is 0, Ansible will start the task and immediately move on to the next one without waiting for a result.
|
|
|
|
From the point of view of sequencing this is asynchronous programming: tasks may now run concurrently.
|
|
|
|
The playbook run will end without checking back on async tasks.
|
|
|
|
The async tasks will run until they either complete, fail or timeout according to their `async` value.
|
|
|
|
If you need a synchronization point with a task, register it to obtain its job ID and use the :ref:`async_status <async_status_module>` module to observe it.
|
|
|
|
You may run a task asynchronously by specifying a poll value of 0::
|
|
|
|
---
|
|
|
|
- hosts: all
|
|
remote_user: root
|
|
|
|
tasks:
|
|
|
|
- name: simulate long running op, allow to run for 45 sec, fire and forget
|
|
command: /bin/sleep 15
|
|
async: 45
|
|
poll: 0
|
|
|
|
.. note::
|
|
You shouldn't attempt run a task asynchronously by specifying a poll value of 0 with operations that require
|
|
exclusive locks (such as yum transactions) if you expect to run other
|
|
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.
|
|
|
|
If you would like to perform a task asynchronously and check on it later you can perform a task similar to the
|
|
following::
|
|
|
|
---
|
|
# Requires ansible 1.8+
|
|
- name: 'YUM - async task'
|
|
yum:
|
|
name: docker-io
|
|
state: present
|
|
async: 1000
|
|
poll: 0
|
|
register: yum_sleeper
|
|
|
|
- name: 'YUM - check on async task'
|
|
async_status:
|
|
jid: "{{ yum_sleeper.ansible_job_id }}"
|
|
register: job_result
|
|
until: job_result.finished
|
|
retries: 30
|
|
|
|
.. note::
|
|
If the value of ``async:`` is not high enough, this will cause the
|
|
"check on it later" task to fail because the temporary status file that
|
|
the ``async_status:`` is looking for will not have been written or no longer exist
|
|
|
|
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 }}"
|
|
include_tasks: execute_batch.yml
|
|
loop: "{{ sleep_durations | batch(2) | list }}"
|
|
|
|
#####################
|
|
# execute_batch.yml
|
|
#####################
|
|
- name: Async sleeping for batched_items
|
|
command: sleep {{ async_item }}
|
|
async: 45
|
|
poll: 0
|
|
loop: "{{ durations }}"
|
|
loop_control:
|
|
loop_var: "async_item"
|
|
register: async_results
|
|
|
|
- name: Check sync status
|
|
async_status:
|
|
jid: "{{ async_result_item.ansible_job_id }}"
|
|
loop: "{{ async_results.results }}"
|
|
loop_control:
|
|
loop_var: "async_result_item"
|
|
register: async_poll_results
|
|
until: async_poll_results.finished
|
|
retries: 30
|
|
|
|
.. seealso::
|
|
|
|
:doc:`playbooks`
|
|
An introduction to playbooks
|
|
`User Mailing List <https://groups.google.com/group/ansible-devel>`_
|
|
Have a question? Stop by the google group!
|
|
`irc.freenode.net <http://irc.freenode.net>`_
|
|
#ansible IRC chat channel
|