From 116342bdaa47c59ef1d375105e1c61630c6de462 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sat, 26 Aug 2023 16:11:53 +1000 Subject: [PATCH] Added class to manage a default set of scripts - included scripts into profiles::base - updated hiera with list of scripts to create and their template name - created template for a puppet wrapper --- hieradata/common.yaml | 3 +++ site/profiles/manifests/base.pp | 4 +++ site/profiles/manifests/base/scripts.pp | 26 +++++++++++++++++++ .../base/scripts/puppetwrapper.py.erb | 22 ++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 site/profiles/manifests/base/scripts.pp create mode 100644 site/profiles/templates/base/scripts/puppetwrapper.py.erb diff --git a/hieradata/common.yaml b/hieradata/common.yaml index 5c004ed..569a796 100644 --- a/hieradata/common.yaml +++ b/hieradata/common.yaml @@ -21,6 +21,9 @@ profiles::base::packages::common: - wget - zsh +profiles::base::scripts::scripts: + puppet: puppetwrapper.py + profiles::puppet::autosign::subnet_ranges: - '198.18.17.0/24' diff --git a/site/profiles/manifests/base.pp b/site/profiles/manifests/base.pp index 5a5493c..079b8d3 100644 --- a/site/profiles/manifests/base.pp +++ b/site/profiles/manifests/base.pp @@ -17,8 +17,12 @@ class profiles::base ( } } + # install common required applications class { 'profiles::base::packages': packages => hiera('profiles::base::packages::common'), ensure => 'installed', } + + # include admin scripts + include profiles::base::scripts } diff --git a/site/profiles/manifests/base/scripts.pp b/site/profiles/manifests/base/scripts.pp new file mode 100644 index 0000000..fc0b3e8 --- /dev/null +++ b/site/profiles/manifests/base/scripts.pp @@ -0,0 +1,26 @@ +# This class can be included directly in node definitions or other classes. +# The preferred method for declaring the scripts is via Hiera. +# +# Here's an example Hiera configuration: +# +# profiles::base::scripts::scripts: +# script1: script1 +# script2: script2 +# +# This would deploy 'script1' and 'script2' to /usr/local/bin using their +# respective ERB templates in the profiles/base/scripts directory. +# +class profiles::base::scripts ( + Hash $scripts = {}, +) { + $scripts.each |$script_name, $template_name| { + file { "/usr/local/bin/${script_name}": + ensure => file, + owner => 'root', + group => 'root', + mode => '0755', + content => template("profiles/base/scripts/${template_name}.erb"), + } + } +} + diff --git a/site/profiles/templates/base/scripts/puppetwrapper.py.erb b/site/profiles/templates/base/scripts/puppetwrapper.py.erb new file mode 100644 index 0000000..57ca8b1 --- /dev/null +++ b/site/profiles/templates/base/scripts/puppetwrapper.py.erb @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys +import subprocess + +def main(): + # If "-E" is in the arguments, modify the following argument + args = sys.argv[1:] + if "-E" in args: + index = args.index("-E") + if index + 1 < len(args): # Check if there's another argument after "-E" + environment_value = args[index + 1] + # Replace \ and - with _ + modified_environment_value = environment_value.replace("\\", "_").replace("-", "_").replace("/","_") + args[index + 1] = modified_environment_value + + # Construct the full puppet command with the modified args + command = ["/opt/puppetlabs/bin/puppet"] + args + subprocess.run(command) + +if __name__ == "__main__": + main()