From 0c3216c5652411549ce32234a18c6ba2329d35ec Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 29 Aug 2018 07:25:37 +1000 Subject: [PATCH] win_reboot: fix minor issues with the latest refactor (#44740) * win_reboot: fix minor issues with the latest refactor * Always return elapsed with win_reboot --- lib/ansible/plugins/action/reboot.py | 11 ++++++++++- lib/ansible/plugins/action/win_reboot.py | 19 ++++++++++--------- .../targets/win_reboot/tasks/main.yml | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/ansible/plugins/action/reboot.py b/lib/ansible/plugins/action/reboot.py index ac3bf23f88..bd9f1f60dd 100644 --- a/lib/ansible/plugins/action/reboot.py +++ b/lib/ansible/plugins/action/reboot.py @@ -134,7 +134,16 @@ class ActionModule(ActionBase): def run_test_command(self, **kwargs): test_command = self._task.args.get('test_command', self.DEFAULT_TEST_COMMAND) display.vvv("%s: attempting post-reboot test command '%s'" % (self._task.action, test_command)) - command_result = self._low_level_execute_command(test_command, sudoable=self.DEFAULT_SUDOABLE) + try: + command_result = self._low_level_execute_command(test_command, sudoable=self.DEFAULT_SUDOABLE) + except Exception: + # may need to reset the connection in case another reboot occurred + # which has invalidated our connection + try: + self._connection.reset() + except AttributeError: + pass + raise result = {} if command_result['rc'] != 0: diff --git a/lib/ansible/plugins/action/win_reboot.py b/lib/ansible/plugins/action/win_reboot.py index 1bb25fb843..7ffcf044c8 100644 --- a/lib/ansible/plugins/action/win_reboot.py +++ b/lib/ansible/plugins/action/win_reboot.py @@ -50,33 +50,34 @@ class ActionModule(RebootActionModule, ActionBase): remote_command = self.construct_command() reboot_result = self._low_level_execute_command(remote_command, sudoable=self.DEFAULT_SUDOABLE) + result = {} + result['start'] = datetime.utcnow() pre_reboot_delay = int(self._task.args.get('pre_reboot_delay', self._task.args.get('pre_reboot_delay_sec', self.DEFAULT_PRE_REBOOT_DELAY))) # Test for "A system shutdown has already been scheduled. (1190)" and handle it gracefully - if reboot_result['rc'] == 1190 or (reboot_result['rc'] != 0 and b"(1190)" in reboot_result['stderr']): + stdout = reboot_result['stdout'] + stderr = reboot_result['stderr'] + if reboot_result['rc'] == 1190 or (reboot_result['rc'] != 0 and "(1190)" in reboot_result['stderr']): display.warning('A scheduled reboot was pre-empted by Ansible.') # Try to abort (this may fail if it was already aborted) result1 = self._low_level_execute_command('shutdown /a', sudoable=self.DEFAULT_SUDOABLE) - # (reboot_result['rc'], stdout1, stderr1) = self._connection.exec_command('shutdown /a') # Initiate reboot again - result2 = self._connection.exec_command('shutdown /r /t %d' % pre_reboot_delay) - # (reboot_result['rc'], stdout2, stderr2) = self._connection.exec_command('shutdown /r /t %d' % pre_reboot_delay) + result2 = self._low_level_execute_command('shutdown /r /t %d' % pre_reboot_delay, sudoable=self.DEFAULT_SUDOABLE) - stdout = reboot_result['stdout'] + result1['stdout'] + result2['stdout'] - stderr = reboot_result['stderr'] + result1['stderr'] + result2['stderr'] + reboot_result['rc'] = result2['rc'] + stdout += result1['stdout'] + result2['stdout'] + stderr += result1['stderr'] + result2['stderr'] - result = {} if reboot_result['rc'] != 0: result['failed'] = True result['rebooted'] = False - result['msg'] = "Shutdown command failed, error was: %s %s" % (to_native(stdout.strip()), to_native('stderr'.strip())) + result['msg'] = "Shutdown command failed, error was: %s %s" % (to_native(stdout.strip()), to_native(stderr.strip())) return result result['failed'] = False - result['start'] = datetime.utcnow() # Get the original connection_timeout option var so it can be reset after try: diff --git a/test/integration/targets/win_reboot/tasks/main.yml b/test/integration/targets/win_reboot/tasks/main.yml index da2010339d..c827e6bfc3 100644 --- a/test/integration/targets/win_reboot/tasks/main.yml +++ b/test/integration/targets/win_reboot/tasks/main.yml @@ -66,7 +66,7 @@ ansible_password: '{{standard_pass}}' ansible_winrm_transport: ntlm register: fail_shutdown - failed_when: fail_shutdown.msg != "Shutdown command failed, error text was 'Access is denied.(5)\n'" + failed_when: "fail_shutdown.msg != 'Shutdown command failed, error was: Access is denied.(5)'" always: - name: set the original SDDL to the WinRM listener