From 07ed487427ce8ea42f3f8a188728e48e2e18dd54 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 (cherry picked from commit a7a99c5fd432d92d5a220d394d80cf7627e9deb4) --- .../fragments/reboot-var-fix-bare-linux.yaml | 2 ++ lib/ansible/plugins/action/reboot.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/reboot-var-fix-bare-linux.yaml diff --git a/changelogs/fragments/reboot-var-fix-bare-linux.yaml b/changelogs/fragments/reboot-var-fix-bare-linux.yaml new file mode 100644 index 00000000000..4d7b7947e02 --- /dev/null +++ b/changelogs/fragments/reboot-var-fix-bare-linux.yaml @@ -0,0 +1,2 @@ +bugfixes: + - reboot - use correct syntax for fetching a value from a dict and account for bare Linux systems (https://github.com/ansible/ansible/pull/45607#issuecomment-422403177) diff --git a/lib/ansible/plugins/action/reboot.py b/lib/ansible/plugins/action/reboot.py index 9abdd63b45e..1db2d16e583 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()