I have a private Azure Linux VM means it can be accessed only from jumpbox(access) vm. I need to deploy a script to this private VM. As this VM cannot even access any storage account/repo, I can't use Custom Script Extension
for script deployment. So I thought of deploying the script using az vm run-command invoke
by converting the SomeScript.sh
to strings and echo
it to the virtual machine. Different pieces of my code as below:
SomeScript.sh
#!/bin/bash
#
# CommandToExecute: ./SomeScript.sh ${CUST_NO}
#
#some more code
Function that converts the .sh file to strings:
function getCommandToExecute()
{
local scriptName=$1
local commandToExecute
local currentLocation=$(dirname "$0")
local scriptFullPath="$currentLocation/Environment/VmScripts/$scriptName"
mapfile < $scriptFullPath
printf -v escapedContents "%q\n" "${MAPFILE[@]}"
commandToExecute+="echo "$escapedContents" > /usr/myapps/$scriptName"
echo "$commandToExecute"
}
vm run command:
az vm run-command invoke -g $resourceGroupName \
-n $vmName --command-id RunShellScript \
--scripts "#!/bin/bash\n ${commandToExecute}"
If I use the "#!/bin/bash\n ${commandToExecute}"
part (commandToExecute
replaced with string scripts) in the RunCommand
window in azure portal, the script works fine, but I can't make it work via run command due to this exception:
\n[stdout]\n\n[stderr]\n/bin/sh: 1: /var/lib/waagent/run-command/download/133/script.sh: not found\n"
Any idea what is missing here? Or if there is a better alternative to handle this.