From 4eca3ab10123606f19aa5789d9d2c9252fcf8096 Mon Sep 17 00:00:00 2001 From: David Tulloh Date: Fri, 13 Oct 2023 10:05:01 +1100 Subject: [PATCH 1/2] Add details of interactions between notifications and loops This clarifies behaviour raised in https://github.com/ansible/ansible/issues/81950 and https://github.com/ansible/ansible/issues/77550 as expected and documented. --- .../rst/playbook_guide/playbooks_handlers.rst | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/docsite/rst/playbook_guide/playbooks_handlers.rst b/docs/docsite/rst/playbook_guide/playbooks_handlers.rst index 1f54ce2968f..3b680596566 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_handlers.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_handlers.rst @@ -79,6 +79,35 @@ Tasks can instruct one or more handlers to execute using the ``notify`` keyword. 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. +Notifying and loops +------------------- + +Tasks can use loops to notify handlers, this is particularly useful when combined with variables to trigger multiple dynamic notifications. + +.. code-block:: yaml + + tasks: + - name: Template services + ansible.builtin.template: + src: "{{ item }}.j2" + dest: /etc/systemd/system/{{ item }}.service + notify: Restart {{ item }} + + handlers: + - name: Restart memcached + ansible.builtin.service: + name: memcached + state: restarted + + - name: Restart apache + ansible.builtin.service: + name: apache + state: restarted + +It is important to note that the notifications are triggered if the task as a whole is changed, for loops that is if any loop item changes. +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. + + Naming handlers --------------- From 8eb45619850da5df8b9c7db04e1b528749e676e5 Mon Sep 17 00:00:00 2001 From: Sandra McCann Date: Thu, 18 Jan 2024 13:53:20 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Maxwell G --- docs/docsite/rst/playbook_guide/playbooks_handlers.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/docsite/rst/playbook_guide/playbooks_handlers.rst b/docs/docsite/rst/playbook_guide/playbooks_handlers.rst index 3b680596566..3f536c44ee6 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_handlers.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_handlers.rst @@ -82,7 +82,7 @@ In the above example the handlers are executed on task change in the following o Notifying and loops ------------------- -Tasks can use loops to notify handlers, this is particularly useful when combined with variables to trigger multiple dynamic notifications. +Tasks can use loops to notify handlers. This is particularly useful when combined with variables to trigger multiple dynamic notifications. .. code-block:: yaml @@ -92,6 +92,9 @@ Tasks can use loops to notify handlers, this is particularly useful when combine src: "{{ item }}.j2" dest: /etc/systemd/system/{{ item }}.service notify: Restart {{ item }} + loop: + - memcached + - apache handlers: - name: Restart memcached @@ -104,7 +107,7 @@ Tasks can use loops to notify handlers, this is particularly useful when combine name: apache state: restarted -It is important to note that the notifications are triggered if the task as a whole is changed, for loops that is if any loop item changes. +Note that the handlers are triggered if the task as a whole is changed, for loops that is if any loop item changes. 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.