From db75e69d99752cfd791eeee36e3049057eb146e4 Mon Sep 17 00:00:00 2001 From: Chris Hammer Date: Fri, 1 Mar 2024 16:59:32 -0500 Subject: [PATCH] Update motd role to no longer require Perl dependancy or script --- galaxy.yml | 2 +- roles/motd/defaults/main.yml | 8 +++++++- roles/motd/scripts/get_uptime.pl | 21 --------------------- roles/motd/tasks/main.yml | 23 +++++++++-------------- roles/motd/templates/motd.j2 | 6 +++--- 5 files changed, 20 insertions(+), 40 deletions(-) delete mode 100644 roles/motd/scripts/get_uptime.pl diff --git a/galaxy.yml b/galaxy.yml index 4d64558..37131e2 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -8,7 +8,7 @@ namespace: jchristianh name: baseos # The version of the collection. Must be compatible with semantic versioning -version: 1.0.27 +version: 1.0.28 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection readme: README.md diff --git a/roles/motd/defaults/main.yml b/roles/motd/defaults/main.yml index 497e09c..8efa069 100644 --- a/roles/motd/defaults/main.yml +++ b/roles/motd/defaults/main.yml @@ -1,5 +1,11 @@ --- -motd_motd_file : /etc/motd +motd_motd_file: /etc/motd + +motd_host_ip : "{{ ansible_default_ipv4.address | default('127.0.0.1') }}" +motd_host_uptime : "{{ now().replace(microsecond=0) - now().fromtimestamp(now(fmt='%s') | int - ansible_uptime_seconds) }}" + +motd_day_filter : "(.* day[s]?),\\s+(\\d+):(\\d+):(\\d+)" +motd_nonday_filter : "(\\d+):(\\d+):(\\d+)" ... diff --git a/roles/motd/scripts/get_uptime.pl b/roles/motd/scripts/get_uptime.pl deleted file mode 100644 index 5f7294f..0000000 --- a/roles/motd/scripts/get_uptime.pl +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/perl - -use strict; - -my $seconds = shift; -my $string = sprintf "%02d::%02d::%02d::%02d", (gmtime($seconds))[7,2,1,0]; - -# days [0] :: hours [1] :: mins [2] :: seconds [3] -my @ups = split(/::/, $string); - -my $return_uptime = ""; - -my $i = 0; -for (@ups) { $ups[$i] =~ s/^0//g; $i++; } - -$return_uptime .= "$ups[0] days " if $ups[0] != 0; -$return_uptime .= "$ups[1] hours " if $ups[1] != 0; -$return_uptime .= "$ups[2] minutes " if $ups[2] != 0; -# $return_uptime .= "$ups[3] seconds" if $ups[3] != 0; - -print "$return_uptime\n"; diff --git a/roles/motd/tasks/main.yml b/roles/motd/tasks/main.yml index 3c0a65c..24144be 100644 --- a/roles/motd/tasks/main.yml +++ b/roles/motd/tasks/main.yml @@ -1,20 +1,16 @@ --- -- name: Set host IP address or set a default +- name: Format uptime containing days ansible.builtin.set_fact: - host_ip : "{{ ansible_default_ipv4.address | default('127.0.0.1') }}" + uptime_formatted: "{{ motd_host_uptime | regex_replace(motd_day_filter, '\\1 \\2 hours \\3 minutes') }}" + when: + - "'day' in motd_host_uptime" -- name: Install Perl if needed - ansible.builtin.package: - name : perl - state : present - - -- name: Get system uptime from script - ansible.builtin.script: - cmd : "scripts/get_uptime.pl {{ ansible_uptime_seconds }}" - register : node_uptime - changed_when : false +- name: Format uptime not containing days + ansible.builtin.set_fact: + uptime_formatted: "{{ motd_host_uptime | regex_replace(motd_nonday_filter, '\\1 hours \\2 minutes \\3 seconds') }}" + when: + - "'day' not in motd_host_uptime" - name: Update MOTD @@ -26,4 +22,3 @@ ... - diff --git a/roles/motd/templates/motd.j2 b/roles/motd/templates/motd.j2 index 2dcfbaa..7a1cc41 100644 --- a/roles/motd/templates/motd.j2 +++ b/roles/motd/templates/motd.j2 @@ -14,11 +14,11 @@ |___/ -Host : {{ ansible_fqdn }} -IP : {{ host_ip }} +Host : {{ inventory_hostname }} +IP : {{ motd_host_ip }} CPU : {{ ansible_processor_vcpus }} x {{ ansible_processor[2] }} Memory : {{ "{0:0.2f}".format(ansible_memfree_mb/1024) }} GB free of {{ (ansible_memtotal_mb/1024)|round}} GB Platform : {{ ansible_distribution }} {{ ansible_distribution_version }} {{ ansible_kernel }} -Up : {{ node_uptime.stdout }} +Up : {{ uptime_formatted }} -- 2.47.1