fixes
This commit is contained in:
@@ -5,37 +5,57 @@ if [ "$#" -ne 2 ]; then
|
||||
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="/var/tmp/4server"
|
||||
|
||||
|
||||
remotetmp_base="/var/tmp/4server"
|
||||
|
||||
while read -r host; do
|
||||
echo "Processing host: $host"
|
||||
|
||||
host_env_file="$host_vars_dir/$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
|
||||
|
||||
declare -A vars=()
|
||||
while IFS='=' read -r key value; do
|
||||
[[ -z "$key" || -z "$value" ]] && continue
|
||||
vars["$key"]="$value"
|
||||
done < "$host_env_file"
|
||||
# Load host environment variables (supports multi-line)
|
||||
set -a
|
||||
source "$host_env_file"
|
||||
set +a
|
||||
|
||||
content=$(cat "$localfile")
|
||||
|
||||
for key in "${!vars[@]}"; do
|
||||
content=$(echo "$content" | sed "s|{$key}|${vars[$key]}|g")
|
||||
done
|
||||
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"
|
||||
rex doas mv $remotetmp $remotefile
|
||||
echo "$content" | ssh "$host" "cat > '$remotetmp'"
|
||||
|
||||
# Move temporary file to final location with doas
|
||||
ssh "$host" "doas mv '$remotetmp' '$remotefile'"
|
||||
|
||||
done < "$hosts_file"
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user