From a7a99c5fd432d92d5a220d394d80cf7627e9deb4 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Mon, 17 Sep 2018 20:04:03 +0200 Subject: [PATCH] reboot: Fix typo and support bare Linux systems (#45607) * reboot: Fix typo and support bare Linux systems This fixes a problem for bare Linux systems that do not support 'who -b' or 'uptime -s'. * Accumulate stdout and stderr information --- lib/ansible/plugins/action/reboot.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/action/reboot.py b/lib/ansible/plugins/action/reboot.py index 9abdd63b45..1db2d16e58 100644 --- a/lib/ansible/plugins/action/reboot.py +++ b/lib/ansible/plugins/action/reboot.py @@ -94,6 +94,8 @@ class ActionModule(ActionBase): return reboot_command def get_system_boot_time(self): + stdout = '' + stderr = '' command_result = self._low_level_execute_command(self.DEFAULT_BOOT_TIME_COMMAND, sudoable=self.DEFAULT_SUDOABLE) # For single board computers, e.g., Raspberry Pi, that lack a real time clock and are using fake-hwclock @@ -101,11 +103,22 @@ class ActionModule(ActionBase): # Fall back to using uptime -s for those systems. # https://github.com/systemd/systemd/issues/6057 if '1970-01-01 00:00' in command_result['stdout']: + stdout += command_result['stdout'] + stderr += command_result['stderr'] command_result = self._low_level_execute_command('uptime -s', sudoable=self.DEFAULT_SUDOABLE) + # This is a last resort for bare Linux systems (e.g. OpenELEC) where 'who -b' or 'uptime -s' are not supported. + # Other options like parsing /proc/uptime or default uptime output are less reliable than this if command_result['rc'] != 0: + stdout += command_result['stdout'] + stderr += command_result['stderr'] + command_result = self._low_level_execute_command('cat /proc/sys/kernel/random/boot_id', sudoable=self.DEFAULT_SUDOABLE) + + if command_result['rc'] != 0: + stdout += command_result['stdout'] + stderr += command_result['stderr'] raise AnsibleError("%s: failed to get host boot time info, rc: %d, stdout: %s, stderr: %s" - % (self._task.action, command_result.rc, to_native(command_result['stdout']), to_native(command_result['stderr']))) + % (self._task.action, command_result['rc'], to_native(stdout), to_native(stderr))) return command_result['stdout'].strip()