Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to clarify run_once and forall! #37754

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docsite/keyword_desc.yml
Expand Up @@ -57,7 +57,7 @@ register: Name of variable that will contain task status and module return data.
rescue: List of tasks in a :term:`block` that run if there is a task error in the main :term:`block` list.
retries: "Number of retries before giving up in a :term:`until` loop. This setting is only used in combination with :term:`until`."
roles: List of roles to be imported into the play
run_once: Boolean that will bypass the host loop, forcing the task to execute on the first host available and will also apply any facts to all active hosts.
run_once: Boolean that will bypass the host loop, forcing the task to attempt to execute on the first host available and afterwards apply any results and facts to all active hosts in the same batch.
serial: |
Explicitly define how Ansible batches the execution of the current play on the play's target

Expand Down
24 changes: 15 additions & 9 deletions docs/docsite/rst/user_guide/playbooks_delegation.rst
Expand Up @@ -203,8 +203,8 @@ This way you can lookup `hostvars['dbhost1']['default_ipv4']['address']` even th
Run Once
````````

In some cases there may be a need to only run a task one time and only on one host. This can be achieved
by configuring "run_once" on a task::
In some cases there may be a need to only run a task one time for a batch of hosts.
This can be achieved by configuring "run_once" on a task::

---
# ...
Expand All @@ -218,25 +218,31 @@ by configuring "run_once" on a task::

# ...

This can be optionally paired with "delegate_to" to specify an individual host to execute on::
This directive forces the task to attempt execution on the first host in the current batch and then applies all results and facts to all the hosts in the same batch.

This approach is similar to applying a conditional to a task such as::

- command: /opt/application/upgrade_db.py
run_once: true
delegate_to: web01.example.org
when: inventory_hostname == webservers[0]

When "run_once" is not used with "delegate_to" it will execute on the first host, as defined by inventory,
in the group(s) of hosts targeted by the play - e.g. webservers[0] if the play targeted "hosts: webservers".
But the results are applied to all the hosts.

This approach is similar to applying a conditional to a task such as::
Like most tasks, this can be optionally paired with "delegate_to" to specify an individual host to execute on::

- command: /opt/application/upgrade_db.py
when: inventory_hostname == webservers[0]
run_once: true
delegate_to: web01.example.org

As always with delegation, the action will be executed on the delegated host, but the information is still that of the original host in the task.

.. note::
When used together with "serial", tasks marked as "run_once" will be run on one host in *each* serial batch.
If it's crucial that the task is run only once regardless of "serial" mode, use
:code:`when: inventory_hostname == ansible_play_hosts[0]` construct.

.. note::
Any conditional (i.e `when:`) will use the variables of the 'first host' to decide if the task runs or not, no other hosts will be tested.

.. _local_playbooks:

Local Playbooks
Expand Down