From 0cc0bacad33b26b8c79caa41ca674ef603a02d65 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sat, 4 Nov 2023 18:12:35 +1100 Subject: [PATCH] feat: add motd and facts - use parameters created by the enc to create external facts - use external facts to generate the motd - use features from https://git.unkin.net/unkinben/puppet-enc/pulls/22 --- site/profiles/manifests/base.pp | 4 +++ site/profiles/manifests/base/facts.pp | 29 +++++++++++++++++++ site/profiles/manifests/base/motd.pp | 20 +++++++++++++ .../profiles/templates/base/facts/enc_env.erb | 1 + .../templates/base/facts/enc_role.erb | 1 + site/profiles/templates/base/motd/motd.erb | 13 +++++++++ 6 files changed, 68 insertions(+) create mode 100644 site/profiles/manifests/base/facts.pp create mode 100644 site/profiles/manifests/base/motd.pp create mode 100644 site/profiles/templates/base/facts/enc_env.erb create mode 100644 site/profiles/templates/base/facts/enc_role.erb create mode 100644 site/profiles/templates/base/motd/motd.erb diff --git a/site/profiles/manifests/base.pp b/site/profiles/manifests/base.pp index 35874eb..e1a98c0 100644 --- a/site/profiles/manifests/base.pp +++ b/site/profiles/manifests/base.pp @@ -52,4 +52,8 @@ class profiles::base ( # default users include profiles::accounts::sysadmin + # add a motd + include profiles::base::facts + include profiles::base::motd + } diff --git a/site/profiles/manifests/base/facts.pp b/site/profiles/manifests/base/facts.pp new file mode 100644 index 0000000..e234625 --- /dev/null +++ b/site/profiles/manifests/base/facts.pp @@ -0,0 +1,29 @@ +# a class to define some global facts +class profiles::base::facts { + + # The path where external facts are stored + $facts_d_path = '/opt/puppetlabs/facter/facts.d' + + # Ensure the directory exists + file { $facts_d_path: + ensure => directory, + owner => 'root', + group => 'root', + mode => '0755', + } + + # facts to create + $fact_list = [ 'enc_role', 'enc_env' ] + + # Manage the external fact file with content from the template + $fact_list.each | String $item | { + file { "${facts_d_path}/${item}.txt": + ensure => file, + owner => 'root', + group => 'root', + mode => '0644', + content => template("profiles/base/facts/${item}.erb"), + require => File[$facts_d_path], + } + } +} diff --git a/site/profiles/manifests/base/motd.pp b/site/profiles/manifests/base/motd.pp new file mode 100644 index 0000000..e1dd5ca --- /dev/null +++ b/site/profiles/manifests/base/motd.pp @@ -0,0 +1,20 @@ +# set the motd +class profiles::base::motd ( + String $enc_role = pick($facts['enc_role'], 'undefined'), + String $enc_env = pick($facts['enc_env'], 'undefined'), + String $fqdn = $facts['networking']['fqdn'], + String $addr = $facts['networking']['ip'], + String $nic = $facts['networking']['primary'], + String $os_name = $facts['os']['name'], + String $os_release = $facts['os']['release']['full'], +) { + + # Use the regsubst function to remove the 'roles::' prefix from the role name + $clean_role = regsubst($enc_role, '^roles::', '') + + # Manage the content of the /etc/motd file + file { '/etc/motd': + ensure => file, + content => template('profiles/base/motd/motd.erb'), + } +} diff --git a/site/profiles/templates/base/facts/enc_env.erb b/site/profiles/templates/base/facts/enc_env.erb new file mode 100644 index 0000000..7695e4d --- /dev/null +++ b/site/profiles/templates/base/facts/enc_env.erb @@ -0,0 +1 @@ +enc_env=<%= @enc_env %> diff --git a/site/profiles/templates/base/facts/enc_role.erb b/site/profiles/templates/base/facts/enc_role.erb new file mode 100644 index 0000000..d59acdf --- /dev/null +++ b/site/profiles/templates/base/facts/enc_role.erb @@ -0,0 +1 @@ +enc_role=<%= @enc_role[0] %> diff --git a/site/profiles/templates/base/motd/motd.erb b/site/profiles/templates/base/motd/motd.erb new file mode 100644 index 0000000..7ca06df --- /dev/null +++ b/site/profiles/templates/base/motd/motd.erb @@ -0,0 +1,13 @@ +<% +# calculate padding for the longest word +max_length = ['fqdn:', 'os:', 'role:', 'branch:', 'addr:', 'nic:'].max_by(&:length).length +# helper lambda to right-align text +align = ->(word) { word.ljust(max_length) } +%> +<%= align.call('fqdn:') %> <%= @fqdn %> +<%= align.call('os:') %> <%= @os_name %> <%= @os_release %> +<%= align.call('role:') %> <%= @clean_role %> +<%= align.call('branch:') %> <%= @enc_env %> +<%= align.call('addr:') %> <%= @addr %> +<%= align.call('nic:') %> <%= @nic %> +