skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. To deploy an Ubuntu VM on Azure and automatically execute a few lines of Bash code right after the VM is deployed:

    I tried to create a Linux VM and used run command to install PowerShell inside the VM while deployment and was able to achieve the desired results by running below bicep file.

    @description('Name of the Network Security Group')
    param  networkSecurityGroupName  string = 'SecGroupNet'
    var  publicIPAddressName = '${vmName}PublicIP'
    var  networkInterfaceName = '${vmName}NetInt'
    var  osDiskType = 'Standard_LRS'
    var  subnetAddressPrefix = '10.1.0.0/24'
    var  addressPrefix = '10.1.0.0/16'
    var  linuxConfiguration = {
    disablePasswordAuthentication: true
    ssh: {
    publicKeys: [
     {
    path: '/home/${adminUsername}/.ssh/authorized_keys'
    keyData: adminPassword
     }
    ]
    }
    }
    resource  nic 'Microsoft.Network/networkInterfaces@2021-05-01' = {
    name: networkInterfaceName
    location: location
    properties: {
    ipConfigurations: [
    {
    name: 'ipconfig1'
    properties: {
    subnet: {
    id: subnet.id
    }
    privateIPAllocationMethod: 'Dynamic'
    publicIPAddress: {
    id: publicIP.id
    }
    }
    }
    ]
    networkSecurityGroup: {
    id: nsg.id
    }
    }
    }
    resource  nsg  'Microsoft.Network/networkSecurityGroups@2021-05-01' = {
    name: networkSecurityGroupName
    location: location
    properties: {
    securityRules: [
    {
     name: 'SSH'
     properties: {
     priority: 1000
     protocol: 'Tcp'
     access: 'Allow'
     direction: 'Inbound'
     sourceAddressPrefix: '*'
     sourcePortRange: '*'
     destinationAddressPrefix: '*'
     destinationPortRange: '22'
      }
     }
    ]
    }
    }
    resource  vnet  'Microsoft.Network/virtualNetworks@2021-05-01' = {
    name: virtualNetworkName
    location: location
    properties: {
    addressSpace: {
    addressPrefixes: [
    addressPrefix
    ]
    }
    }
    }
    resource  subnet  'Microsoft.Network/virtualNetworks/subnets@2021-05-01' = {
    parent: vnet
    name: subnetName
    properties: {
    addressPrefix: subnetAddressPrefix
    privateEndpointNetworkPolicies: 'Enabled'
    privateLinkServiceNetworkPolicies: 'Enabled'
    }
    }
    resource  publicIP  'Microsoft.Network/publicIPAddresses@2021-05-01' = {
    name: publicIPAddressName
    location: location
    sku: {
    name: 'Basic'
    }
    properties: {
    publicIPAllocationMethod: 'Dynamic'
    publicIPAddressVersion: 'IPv4'
    dnsSettings: {
    domainNameLabel: dnsLabelPrefix
    }
    idleTimeoutInMinutes: 4
    }
    }
    resource  vm  'Microsoft.Compute/virtualMachines@2021-11-01' = {
    name: vmName
    location: location
    properties: {
    hardwareProfile: {
    vmSize: vmSize
    }
    storageProfile: {
    osDisk: {
    createOption: 'FromImage'
    managedDisk: {
    storageAccountType: osDiskType
    }
    }
    imageReference: {
    publisher: 'Canonical'
    offer: 'UbuntuServer'
    sku: ubuntuOSVersion
    version: 'latest'
    }
    }
    networkProfile: {
    networkInterfaces: [
    {
    id: nic.id
    }
    ]
    }
    osProfile: {
    computerName: vmName
    adminUsername: adminUsername
    adminPassword: adminPassword
    linuxConfiguration: ((authenticationType == 'password') ? null : linuxConfiguration)
    }
    }
    }
    resource  deploymentscript  'Microsoft.Compute/virtualMachines/runCommands@2022-03-01' = {
    parent: vm
    name: 'linuxscript'
    location: location
    properties: {
    source: {
    script: '''# Update the list of packages
    sudo apt-get update;
    #Install pre-requisite packages.
    sudo apt-get install -y wget apt-transport-https software-properties-common;
    #Download the Microsoft repository GPG keys
    wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb";
    #Register the Microsoft repository GPG keys
    sudo dpkg -i packages-microsoft-prod.deb;
    #Update the list of packages after we added packages.microsoft.com
    sudo apt-get update;
    #Install PowerShell
    sudo apt-get install -y powershell;
    #Start PowerShell
    pwsh'''
    }
    }
    }
    output  adminUsername  string = adminUsername
    output  hostname  string = publicIP.properties.dnsSettings.fqdn
    output  sshCommand  string = 'ssh $ {adminUsername}@${publicIP.properties.dnsSettings.fqdn}'
    

    Deployed Successfully:

    enter image description here

    enter image description here

    From Azure Portal:

    enter image description here

    After the deployment, When I ssh’d into my VM and ran Pwsh to check if PowerShell was installed

    Installed successfully:

    enter image description here

    Refer MSDoc, run command template-MSDoc

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search