Skip to content

Commit 7d38de3

Browse files
patchback[bot]lod
andauthored
Add details of interactions between notifications and loops (#597) (#2064)
* Add details of interactions between notifications and loops This clarifies behaviour raised in ansible/ansible#81950 and ansible/ansible#77550 as expected and documented. * Apply suggestions from code review Co-authored-by: Maxwell G <[email protected]> * Incorporate suggestions from @felixfontein Make the loop trigger even more explicit. --------- Co-authored-by: Sandra McCann <[email protected]> Co-authored-by: Maxwell G <[email protected]> (cherry picked from commit 74b2c95) Co-authored-by: David Tulloh <[email protected]>
1 parent 91ebd25 commit 7d38de3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: docs/docsite/rst/playbook_guide/playbooks_handlers.rst

+34
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,40 @@ Tasks can instruct one or more handlers to execute using the ``notify`` keyword.
7979
In the above example, the handlers are executed on task change in the following order: ``Restart memcached``, ``Restart apache``. Handlers are executed in the order they are defined in the ``handlers`` section, not in the order listed in the ``notify`` statement. Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it. For example, if multiple tasks update a configuration file and notify a handler to restart Apache, Ansible only bounces Apache once to avoid unnecessary restarts.
8080

8181

82+
Notifying and loops
83+
-------------------
84+
85+
Tasks can use loops to notify handlers. This is particularly useful when combined with variables to trigger multiple dynamic notifications.
86+
87+
Note that the handlers are triggered if the task as a whole is changed. When a loop is used the changed state is set if any of the loop items are changed. That is, any change triggers all of the handlers.
88+
89+
.. code-block:: yaml
90+
91+
tasks:
92+
- name: Template services
93+
ansible.builtin.template:
94+
src: "{{ item }}.j2"
95+
dest: /etc/systemd/system/{{ item }}.service
96+
# Note: if *any* loop iteration triggers a change, *all* handlers are run
97+
notify: Restart {{ item }}
98+
loop:
99+
- memcached
100+
- apache
101+
102+
handlers:
103+
- name: Restart memcached
104+
ansible.builtin.service:
105+
name: memcached
106+
state: restarted
107+
108+
- name: Restart apache
109+
ansible.builtin.service:
110+
name: apache
111+
state: restarted
112+
113+
In the above example both memcached and apache will be restarted if either template file is changed, neither will be restarted if no file changes.
114+
115+
82116
Naming handlers
83117
---------------
84118

0 commit comments

Comments
 (0)