[stable-2.16] run_once: unnotify hosts on handlers that are not run (#81667) (#81920)

Fixes #81666
(cherry picked from commit 2d5861c)
pull/81942/head
Martin Krizek 1 year ago committed by GitHub
parent f3f0e6a0f8
commit 9c91e578d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Fix ``run_once`` being incorrectly interpreted on handlers (https://github.com/ansible/ansible/issues/81666)

@ -53,6 +53,9 @@ class Handler(Task):
def remove_host(self, host):
self.notified_hosts = [h for h in self.notified_hosts if h != host]
def clear_hosts(self):
self.notified_hosts = []
def is_host_notified(self, host):
return host in self.notified_hosts

@ -800,10 +800,6 @@ class StrategyBase:
ret_results.append(task_result)
if isinstance(original_task, Handler):
for handler in (h for b in iterator._play.handlers for h in b.block if h._uuid == original_task._uuid):
handler.remove_host(original_host)
if one_pass or max_passes is not None and (cur_pass + 1) >= max_passes:
break
@ -1091,9 +1087,6 @@ class StrategyBase:
header = skip_reason if skipped else msg
display.vv(f"META: {header}")
if isinstance(task, Handler):
task.remove_host(target_host)
res = TaskResult(target_host, task, result)
if skipped:
self._tqm.send_callback('v2_runner_on_skipped', res)

@ -146,6 +146,8 @@ class StrategyModule(StrategyBase):
# advance the host, mark the host blocked, and queue it
self._blocked_hosts[host_name] = True
iterator.set_state_for_host(host.name, state)
if isinstance(task, Handler):
task.remove_host(host)
try:
action = action_loader.get(task.action, class_only=True, collection_list=task.collections)

@ -242,6 +242,12 @@ class StrategyModule(StrategyBase):
self._queue_task(host, task, task_vars, play_context)
del task_vars
if isinstance(task, Handler):
if run_once:
task.clear_hosts()
else:
task.remove_host(host)
# if we're bypassing the host loop, break out now
if run_once:
break

@ -195,3 +195,6 @@ ansible localhost -m include_role -a "name=r1-dep_chain-vars" "$@"
ansible-playbook test_include_tasks_in_include_role.yml "$@" 2>&1 | tee out.txt
[ "$(grep out.txt -ce 'handler ran')" = "1" ]
ansible-playbook test_run_once.yml -i inventory.handlers "$@" 2>&1 | tee out.txt
[ "$(grep out.txt -ce 'handler ran once')" = "1" ]

@ -0,0 +1,10 @@
- hosts: A,B,C
gather_facts: false
tasks:
- command: echo
notify: handler
handlers:
- name: handler
run_once: true
debug:
msg: handler ran once
Loading…
Cancel
Save