From 14a9966984352f66e1649807183c6eabdee20616 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 5 Jun 2014 11:58:00 +0200 Subject: [PATCH 1/2] Added cross-platform uptime fact Having an uptime fact allows you to check after a reboot whether the system was effectively rebooted. ```yaml - name: Safeguard - Was system properly rebooted ? action: fail msg="System was not properly rebooted" when: ansible_uptime > 900 ``` This patch has been tested on Linux, Solaris and HP-UX. --- lib/ansible/module_utils/facts.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index e5866f7bf6e..6df5db7510a 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -117,6 +117,7 @@ class Facts(object): self.facts = {} self.get_platform_facts() self.get_distribution_facts() + self.get_uptime() self.get_cmdline() self.get_public_ssh_host_keys() self.get_selinux_facts() @@ -130,6 +131,20 @@ class Facts(object): def populate(self): return self.facts + def get_uptime(self): + uptime_path = module.get_bin_path('uptime') + if uptime_path: + rc, out, err = module.run_command(uptime_path) + if rc == 0: + raw = out.replace(',','') + days = int(raw.split()[2]) + if 'min' in raw: + hours = 0 + minutes = int(raw[4]) + else: + hours, minutes = map(int, raw.split()[4].split(':')) + self.facts['uptime'] = days*24*60*60 + hours*60*60 + minutes*60 + # Platform # platform.system() can be Linux, Darwin, Java, or Windows def get_platform_facts(self): @@ -537,6 +552,11 @@ class LinuxHardware(Hardware): pass return self.facts + def get_uptime(self): + data = get_file_content('/proc/uptime') + if data: + self.facts['uptime'] = float(data.split()[0]) + def get_memory_facts(self): if not os.access("/proc/meminfo", os.R_OK): return @@ -798,6 +818,11 @@ class SunOSHardware(Hardware): self.get_memory_facts() return self.facts + def get_uptime(self): + rc, out, err = module.run_command("/usr/bin/kstat -p unix:0:system_misc:snaptime") + if out: + self.facts['uptime'] = float(out.split()[1]) + def get_cpu_facts(self): physid = 0 sockets = {} From fd6f0cb4b4a71ef5c142410d351e47c455289bf7 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 5 Jun 2014 15:09:43 +0200 Subject: [PATCH 2/2] Update facts.py --- lib/ansible/module_utils/facts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index 6df5db7510a..4c8d676e23d 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -140,7 +140,7 @@ class Facts(object): days = int(raw.split()[2]) if 'min' in raw: hours = 0 - minutes = int(raw[4]) + minutes = int(raw.split()[4]) else: hours, minutes = map(int, raw.split()[4].split(':')) self.facts['uptime'] = days*24*60*60 + hours*60*60 + minutes*60