I want to deploy an Ubuntu VM on Azure and automatically execute a few lines of Bash code right after the VM is deployed. The Bash code is supposed to install PowerShell on the VM. To do this, I use this Bicep file. Below you can see an extract of that Bicep file where I specify what Bash code I want to be executed post deployment.
resource deploymentscript 'Microsoft.Compute/virtualMachines/runCommands@2022-08-01' = {
parent: virtualMachine
name: 'postDeploymentPSInstall'
location: location
properties: {
source: {
script: '''sudo apt-get update &&
sudo apt-get install -y wget apt-transport-https software-properties-common &&
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb" &&
sudo dpkg -i packages-microsoft-prod.deb &&
sudo apt-get update &&
sudo apt-get install -y powershell &&
pwsh'''
}
}
}
I searched for solutions on the web but only found conflicting explanations. I made the code above with the help of this tutorial. The only difference I see is that I’m using Bash and not PowerShell like the blog post author. Thanks for your help.
2
Answers
Your problem is misunderstanding what the && does.
The shell will attempt to run semi-simultaneously all parts, some possibly clobbering others or not having necessary preconditions in place before starting!
Replace all instances of "&&" with ";" and your script should work, meaning the commands will run sequentially, waiting for the previous line to complete before attempting the subsequent lines.
I tried to create a
Linux
VM and usedrun
command to install PowerShell inside the VM while deployment and was able to achieve the desired results by running belowbicep
file.Deployed Successfully:
From Azure Portal:
After the deployment, When I
ssh’d
into my VM and ranPwsh
to check if PowerShell was installedInstalled successfully:
Refer MSDoc, run command template-MSDoc