Jessica Brown Posted December 25, 2024 Posted December 25, 2024 A while back, I was tasked to create a BASH script to onboard Linux machines to Azure Update Manager. Since multiple Business units were going to be using this, I needed it to be easy to use, with plug and play arguments and easy to follow instructions. Overall, this is the script that has been working for over 350 RHEL machines and VMs. #!/bin/bash # linux_aum_onboard.sh # Version 2.1.0 [Dec. 17, 2023] # By Jessica Brown # Added argument parsing for all required parameters. # Added logging and log file argument handling. # Simplified --help for clear instructions. # Aum Arc Onboarding # Key Changes: # Argument Handling: Added getopts-like case parsing for all arguments. # Logging: Enhanced logging functions to reflect levels clearly. # Defaults: Assigned default values for log file and logging level. # Validation: Ensures all required arguments are supplied, else exits with --help. # Default values servicePrincipalClientId="" servicePrincipalSecret="" SUBSCRIPTION_ID="" RESOURCE_GROUP="" TENANT_ID="" LOCATION="" CORRELATION_ID="" logging="info" aum_log_file="aum_install_$(LC_ALL=C date +"%Y-%m-%d_%H-%M-%S")_${USER}.log" CLOUD="AzureCloud" tempdir="/tmp" # Function to print help usage() { cat << EOF Usage: $0 [options] Options: --help, -h Show this help message --servicePrincipalClientId, -spc Service Principal Client ID --servicePrincipalSecret, -sps Service Principal Secret --subscriptionId, -sid Azure Subscription ID --resourceGroup, -rg Azure Resource Group --tenantId, -tid Azure Tenant ID --location, -l Azure Location --correlationId, -cid Correlation ID --logging Logging level (default: info) Options: debug, info, warning, error, critical --logFile Log file name (default: aum_install_<date>_<user>.log) EOF exit 0 } # Logging functions log_level=5 # Default to "info" log() { local level=$1; shift local msg="$*" local timestamp="$(LC_ALL=C date +"%Y-%m-%d %H:%M:%S")" local levels=("" "CRITICAL" "ERROR" "WARNING" "NOTICE" "INFO" "DEBUG") if [ ${level} -le ${log_level} ]; then echo "[${timestamp}]:[${levels[level]}]:${msg}" | tee -a "${aum_log_file}" fi } set_log_level() { case "$logging" in critical) log_level=1 ;; error) log_level=2 ;; warning) log_level=3 ;; info) log_level=5 ;; debug) log_level=6 ;; *) log_level=5; log warning "Unknown logging level: $logging, defaulting to info." ;; esac } # Parse arguments while [[ $# -gt 0 ]]; do key="$1" case $key in -h|--help) usage ;; -spc|--servicePrincipalClientId) servicePrincipalClientId="$2"; shift 2;; -sps|--servicePrincipalSecret) servicePrincipalSecret="$2"; shift 2;; -sid|--subscriptionId) SUBSCRIPTION_ID="$2"; shift 2;; -rg|--resourceGroup) RESOURCE_GROUP="$2"; shift 2;; -tid|--tenantId) TENANT_ID="$2"; shift 2;; -l|--location) LOCATION="$2"; shift 2;; -cid|--correlationId) CORRELATION_ID="$2"; shift 2;; --logging) logging="$2"; shift 2;; --logFile) aum_log_file="$2"; shift 2;; *) log 2 "Unknown argument: $1"; usage;; esac done # Validate required arguments if [[ -z "$servicePrincipalClientId" || -z "$servicePrincipalSecret" || -z "$SUBSCRIPTION_ID" || \ -z "$RESOURCE_GROUP" || -z "$TENANT_ID" || -z "$LOCATION" ]]; then log 2 "Missing required arguments." usage fi # Set logging level set_log_level # Start Logging log 5 "Starting AUM Linux Onboarding Script" log 5 "Log file: ${aum_log_file}" # Dependencies and preparation if ! command -v curl &>/dev/null; then log 2 "curl is not installed. Installing..." sudo -E yum -y install curl || { log 1 "Failed to install curl."; exit 1; } fi log 5 "Downloading install_linux_azcmagent.sh..." curl -o install_linux_azcmagent.sh https://gbl.his.arc.azure.com/azcmagent-linux || { log 1 "Failed to download install_linux_azcmagent.sh."; exit 1; } chmod +x install_linux_azcmagent.sh log 5 "Running installation script..." ./install_linux_azcmagent.sh || { log 1 "Installation failed."; exit 1; } log 5 "Connecting to Azure using azcmagent..." ./azcmagent connect \ --service-principal-id "${servicePrincipalClientId}" \ --service-principal-secret "${servicePrincipalSecret}" \ --resource-group "${RESOURCE_GROUP}" \ --tenant-id "${TENANT_ID}" \ --location "${LOCATION}" \ --subscription-id "${SUBSCRIPTION_ID}" \ --cloud "${CLOUD}" \ --tags "Region=Americas" \ --correlation-id "${CORRELATION_ID}" || { log 1 "Failed to connect using azcmagent."; exit 1; } log 5 "AUM Linux Onboarding completed successfully." CodeName: Jessica 💻 Linux Enthusiast | 🌍 Adventurer | 🦄 Unicorn 🌐 My Site | 📢 Join the Forum
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now