Search code examples
azureazure-virtual-machine

run bash script automatically when Azure VM starts


in aws ec2 I would paste my script in the user data textbox, here I tried the Custom Data to set a script that installs docker, but it didn't work, I also tried encoding it like this:

certutil -encode installDockerBash.txt encoded.txt

before pasting it into the textbox, but docker is still not present when I connect to it, so I presume the script didn't execute.

update: this is my script I tried pasting (raw and encoded) in the custom data textbox on portal.azure.com -> Create VM page (os debian 11):

#!/bin/bash

echo "** Updating package lists..."
sudo apt update

echo "** Installing dependencies..."
sudo apt install -y apt-transport-https ca-certificates curl gnupg software-properties-common

echo "** Adding Docker GPG key..."
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

echo "** Adding Docker repository..."
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian bullseye stable" 

echo "** Updating package lists again..."
sudo apt update

echo "** Installing Docker..."
sudo apt install -y docker-ce

echo "** Docker installation complete!"

update 2, the script executed (didn't work though), my cloud-init-ouput:

** Updating package lists...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 700 (apt-get)
E: Unable to lock directory /var/lib/apt/lists/
** Installing dependencies...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
Package gnupg is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'gnupg' has no installation candidate
E: Unable to locate package software-properties-common
** Adding Docker GPG key...
E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation
(23) Failed writing body
** Adding Docker repository...
sudo: add-apt-repository: command not found
** Updating package lists again...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 700 (apt-get)
E: Unable to lock directory /var/lib/apt/lists/
** Installing Docker...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package docker-ce
** Docker installation complete!

Solution

  • I'm intrigued as to what method you're using to deploy this VM, given your mention of a text box.

    Custom Data can either specify a local file, or inline script enclosed in quotes, when using the Azure CLI. When passing an inline script, line breaks should be passed using \n.

    In both cases, you must also pass the --extensions customScript parameter.

    Script File Example

    az vm create \
      --resource-group <resource_group_name> \
      --name <vm_name> \
      --image UbuntuLTS \
      --admin-username <username> \
      --admin-password <password> \
      --custom-data myscript.sh \
      --extensions customScript \
      --no-wait
    

    Inline Script Example

    az vm create \
      --resource-group <resource_group_name> \
      --name <vm_name> \
      --image UbuntuLTS \
      --admin-username <username> \
      --admin-password <password> \
      --custom-data "#!/bin/bash\nsudo apt-get update\nsudo apt-get install -y docker.io" \
      --extensions customScript \
      --no-wait
    

    To troubleshoot the script, try redirecting the output of each command to a file:

    # /bin/bash
    sudo apt-get install -y docker.io >> /tmp/customScript_stdout.txt
    etc...
    

    Portal Deployment

    Scripts can be run at initialisation time using the Custom data field.

    VM Deployment -> Advanced -> Custom data and cloud init -> Custom data

    You must write or paste your script here in plain text. Encoding is carried out by the Portal automatically.


    Ensure in all cases that any commands which normally require user input are dealt with accordingly, such as passing confirmation to apt-get or yum with the -y switch.