From 3ab46cd00d8d9fdcdc04eff8f006e8705447dfa3 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 29 May 2019 10:58:38 -0400 Subject: [PATCH 1/4] handle hosts disappearing mid execution fixes #55669 --- changelogs/fragments/disappearing_hosts.yml | 2 ++ lib/ansible/executor/play_iterator.py | 7 +++++++ lib/ansible/executor/task_queue_manager.py | 11 +++++------ 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/disappearing_hosts.yml diff --git a/changelogs/fragments/disappearing_hosts.yml b/changelogs/fragments/disappearing_hosts.yml new file mode 100644 index 00000000000000..dee38d14b306b6 --- /dev/null +++ b/changelogs/fragments/disappearing_hosts.yml @@ -0,0 +1,2 @@ +bugfixes: + - handle when host disappears form inventory mid execution. diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py index 0fdcb2fedcccfd..319f96fc45698e 100644 --- a/lib/ansible/executor/play_iterator.py +++ b/lib/ansible/executor/play_iterator.py @@ -473,6 +473,13 @@ def mark_host_failed(self, host): self._host_states[host.name] = s self._play._removed_hosts.append(host.name) + def host_not_in_inventory(self, hostname): + display.debug("marking host %s failed, host not found in inventory: %s" % (hostname)) + s = self._set_failed_state(self.FAILED_NONE) + display.debug("^ failed state is now: %s" % s) + self._host_states[hostname] = s + self._play._removed_hosts.append(hostname) + def get_failed_hosts(self): return dict((host, True) for (host, state) in iteritems(self._host_states) if self._check_failed_state(state)) diff --git a/lib/ansible/executor/task_queue_manager.py b/lib/ansible/executor/task_queue_manager.py index fabc4a92370c24..2a3fb8fe8f5e40 100644 --- a/lib/ansible/executor/task_queue_manager.py +++ b/lib/ansible/executor/task_queue_manager.py @@ -30,13 +30,10 @@ from ansible.executor.task_result import TaskResult from ansible.module_utils.six import string_types from ansible.module_utils._text import to_text, to_native -from ansible.playbook.block import Block from ansible.playbook.play_context import PlayContext from ansible.plugins.loader import callback_loader, strategy_loader, module_loader from ansible.plugins.callback import CallbackBase from ansible.template import Templar -from ansible.utils.collection_loader import AnsibleCollectionRef -from ansible.utils.helpers import pct_to_int from ansible.vars.hostvars import HostVars from ansible.vars.reserved import warn_if_reserved from ansible.utils.display import Display @@ -193,8 +190,7 @@ def run(self, play): ) play_context = PlayContext(new_play, self.passwords, self._connection_lockfile.fileno()) - if (self._stdout_callback and - hasattr(self._stdout_callback, 'set_play_context')): + if (self._stdout_callback and hasattr(self._stdout_callback, 'set_play_context')): self._stdout_callback.set_play_context(play_context) for callback_plugin in self._callback_plugins: @@ -227,7 +223,10 @@ def run(self, play): # hosts so we know what failed this round. for host_name in self._failed_hosts.keys(): host = self._inventory.get_host(host_name) - iterator.mark_host_failed(host) + if host is None: + iterator.host_not_in_invenotry(host_name) + else: + iterator.mark_host_failed(host) self.clear_failed_hosts() From a9601490789c0c8928348ad2f3fcd35a1df6abd5 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 29 May 2019 11:00:47 -0400 Subject: [PATCH 2/4] added warning --- lib/ansible/executor/task_queue_manager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ansible/executor/task_queue_manager.py b/lib/ansible/executor/task_queue_manager.py index 2a3fb8fe8f5e40..ac511b0d49c4d3 100644 --- a/lib/ansible/executor/task_queue_manager.py +++ b/lib/ansible/executor/task_queue_manager.py @@ -224,6 +224,7 @@ def run(self, play): for host_name in self._failed_hosts.keys(): host = self._inventory.get_host(host_name) if host is None: + display.warning("Lost host '%s' from inventory, removing from rest of play execution." % host_name) iterator.host_not_in_invenotry(host_name) else: iterator.mark_host_failed(host) From 3102d4bb8915dd034cb63b66bfd4ba2bf3e68e43 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 30 May 2019 15:01:45 -0400 Subject: [PATCH 3/4] correclty use hoststate --- lib/ansible/executor/play_iterator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py index 319f96fc45698e..ff780512b99dad 100644 --- a/lib/ansible/executor/play_iterator.py +++ b/lib/ansible/executor/play_iterator.py @@ -475,7 +475,7 @@ def mark_host_failed(self, host): def host_not_in_inventory(self, hostname): display.debug("marking host %s failed, host not found in inventory: %s" % (hostname)) - s = self._set_failed_state(self.FAILED_NONE) + s = self._set_failed_state(HostState(blocks=[])) display.debug("^ failed state is now: %s" % s) self._host_states[hostname] = s self._play._removed_hosts.append(hostname) From 2a5a5e0dd8743c35d42c5d10dedf951cad4ac5e0 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 26 Apr 2021 12:45:03 -0400 Subject: [PATCH 4/4] Update lib/ansible/executor/task_queue_manager.py Co-authored-by: David Shrewsbury --- lib/ansible/executor/task_queue_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/executor/task_queue_manager.py b/lib/ansible/executor/task_queue_manager.py index ac511b0d49c4d3..90d12107a00a92 100644 --- a/lib/ansible/executor/task_queue_manager.py +++ b/lib/ansible/executor/task_queue_manager.py @@ -225,7 +225,7 @@ def run(self, play): host = self._inventory.get_host(host_name) if host is None: display.warning("Lost host '%s' from inventory, removing from rest of play execution." % host_name) - iterator.host_not_in_invenotry(host_name) + iterator.host_not_in_inventory(host_name) else: iterator.mark_host_failed(host)