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.
Use @{file} to load script from a file. It is unclear whetherfilehas to be on the client side or server side. Could you give it a shot and report the results?az vm run-command invoke ... --scripts '@{scriptFileOnYourLocalMachine}'.@{scriptFileOnYourLocalMachine}is for runnning a script on vm, but I am looking for deploying theSomeScript.shon the VM so that other authorized users execute commands likeSomeScript.sh ${CUST_NO}on same vm.@{deployMyScript.sh}and not@{scriptToBeDeployed.sh}. I just needed to know whether@{file}references files from the local host or files from the remote host. The name(any)scriptFileOnYourLocalMachinewas a bit misleading, sorry for the confusion.