#!/bin/bash if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi keys=(NEBULA_CA API_KEY HOSTNAME NEBULA_CRT NEBULA_KEY SSH_PRIVATE SSH_PUBLIC) NEBULA_CA=$(<"$host_vars_dir/ca.crt") localfile="$1" remotefile="$2" remotetmp_base="/var/tmp/4server" while read -r host; do echo "Processing host: $host" host_env_file="$host_vars_dir/$host/$host.env" if [ ! -f "$host_env_file" ]; then echo "Warning: env file for host '$host' not found at $host_env_file. Skipping." continue fi # Load host environment variables (supports multi-line) set -a source "$host_env_file" set +a NEBULA_KEY=$(<"$host_vars_dir/$host/$host.key") NEBULA_CRT=$(<"$host_vars_dir/$host/$host.crt") SSH_PRIVATE=$(<"$host_vars_dir/$host/$host") SSH_PUBLIC=$(<"$host_vars_dir/$host/$host.pub") content=$(<"$localfile") for key in "${keys[@]}"; do value="${!key}" # indirect reference # Replace placeholder {{KEY}} with value using Bash's parameter expansion content="${content//\{\{$key\}\}/$value}" done # Copy content to remote temporary file remotetmp="${remotetmp_base}_${host}" echo "Copying to $host:$remotefile" echo "$content" | ssh "$host" "cat > '$remotetmp'" # Move temporary file to final location with doas ssh "$host" "doas mv '$remotetmp' '$remotefile'" done < /app/host_vars/hosts