diff --git a/alpine/template b/alpine/template index 6c0a841..3bdc469 100755 --- a/alpine/template +++ b/alpine/template @@ -15,6 +15,11 @@ localfile="$1" remotefile="$2" remotetmp_base="/var/tmp/4server" +# If the destination is a directory (trailing slash), keep the local file name +case "$remotefile" in + */) remotefile="${remotefile}$(basename "$localfile")" ;; +esac + # Read hosts from file descriptor 3 to prevent ssh from consuming stdin while read -r host <&3; do host_env_file="$host_vars_dir/$host/$host.env" @@ -34,7 +39,6 @@ while read -r host <&3; do SSH_PRIVATE=$(<"$host_vars_dir/$host/$host") SSH_PUBLIC=$(<"$host_vars_dir/$host/$host.pub") - content=$(<"$localfile") for key in "${keys[@]}"; do @@ -42,7 +46,6 @@ while read -r host <&3; do # 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" @@ -52,4 +55,3 @@ while read -r host <&3; do ssh "$host" "doas mv '$remotetmp' '$remotefile'" done 3< /app/host_vars/hosts -