From 674d8287fc0f7a3bb5c340222cc7449130f021cf Mon Sep 17 00:00:00 2001 From: Tobi <59912621+schroeert@users.noreply.github.com> Date: Mon, 18 Nov 2024 09:33:18 +0100 Subject: [PATCH] Update lib/ansible/module_utils/facts/hardware/aix.py Tested change on AIX. Result as expected. Co-authored-by: Abhijeet Kasurde --- .../module_utils/facts/hardware/aix.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/facts/hardware/aix.py b/lib/ansible/module_utils/facts/hardware/aix.py index 93ba97dee10..643d93e078a 100644 --- a/lib/ansible/module_utils/facts/hardware/aix.py +++ b/lib/ansible/module_utils/facts/hardware/aix.py @@ -130,15 +130,27 @@ class AIXHardware(Hardware): # On AIX, there are no options to get the uptime directly in seconds. # Your options are to parse the output of "who", "uptime", or "ps". # Only "ps" always provides a field with seconds. - rc, out, err = self.module.run_command("/usr/bin/ps -p 1 -o etime=") + ps_bin = self.module.get_bin_path("ps") + if ps_bin is None: + return uptime_facts + + ps_cmd = [ps_bin, "-p", "1", "-o", "etime="] + + rc, out, err = self.module.run_command(ps_cmd) + if rc != 0: + return uptime_facts + # Parse out if out: lines = out.splitlines() data = lines[0].replace(':', '-').split('-') - days = int(data[0]) - hours = int(data[1]) - minutes = int(data[2]) - seconds = int(data[3]) + try: + days = int(data[0]) + hours = int(data[1]) + minutes = int(data[2]) + seconds = int(data[3]) + except (IndexError, ValueError): + return uptime_facts # Calculate uptime in seconds uptime_seconds = (days * 86400) + (hours * 3600) + (minutes * 60) + seconds uptime_facts['uptime_seconds'] = int(uptime_seconds)