diff --git a/changelogs/fragments/23958-cleanup.yml b/changelogs/fragments/23958-cleanup.yml new file mode 100644 index 00000000000..a973886532f --- /dev/null +++ b/changelogs/fragments/23958-cleanup.yml @@ -0,0 +1,3 @@ +bugfixes: +- Ensure if a traceback halts ``strategy.run`` that we still attempt to clean up + (https://github.com/ansible/ansible/issues/23958) diff --git a/lib/ansible/executor/task_queue_manager.py b/lib/ansible/executor/task_queue_manager.py index 02a75cd07f5..691f65acf2a 100644 --- a/lib/ansible/executor/task_queue_manager.py +++ b/lib/ansible/executor/task_queue_manager.py @@ -289,14 +289,16 @@ class TaskQueueManager: self._start_at_done = True # and run the play using the strategy and cleanup on way out - play_return = strategy.run(iterator, play_context) + try: + play_return = strategy.run(iterator, play_context) + finally: + strategy.cleanup() + self._cleanup_processes() # now re-save the hosts that failed from the iterator to our internal list for host_name in iterator.get_failed_hosts(): self._failed_hosts[host_name] = True - strategy.cleanup() - self._cleanup_processes() return play_return def cleanup(self):