summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2024-10-17 08:33:39 +0200
committerGitHub <noreply@github.com>2024-10-17 08:33:39 +0200
commit42e2f09b97c8b98ef4f069db3b49585245576354 (patch)
treeee6d712124ef79db36e369cdf2a093655b7e9899
parentRemove configuration options that were moved to inventory plugins now that an... (diff)
downloadansible-42e2f09b97c8b98ef4f069db3b49585245576354.tar.xz
ansible-42e2f09b97c8b98ef4f069db3b49585245576354.zip
Faster host removal from a handler (#84091)
In ``Handler.notify_host`` we ensure that ``Handler.notified_hosts`` can contain particular host at most once. Therefore for removing a host it should be faster to use ``list.remove`` which removes the first item in the list, as opposed to using list comprehension removing all such items.
-rw-r--r--lib/ansible/playbook/handler.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/ansible/playbook/handler.py b/lib/ansible/playbook/handler.py
index c9cba6a6c3..125a9cddc7 100644
--- a/lib/ansible/playbook/handler.py
+++ b/lib/ansible/playbook/handler.py
@@ -17,6 +17,7 @@
from __future__ import annotations
+from ansible.errors import AnsibleAssertionError
from ansible.playbook.attribute import NonInheritableFieldAttribute
from ansible.playbook.task import Task
from ansible.module_utils.six import string_types
@@ -59,7 +60,12 @@ class Handler(Task):
return False
def remove_host(self, host):
- self.notified_hosts = [h for h in self.notified_hosts if h != host]
+ try:
+ self.notified_hosts.remove(host)
+ except ValueError:
+ raise AnsibleAssertionError(
+ f"Attempting to remove a notification on handler '{self}' for host '{host}' but it has not been notified."
+ )
def clear_hosts(self):
self.notified_hosts = []