Search code examples
azureuser-datacloud-init

Why does Azure VM not run cloud-init script specified in userData parameter?


Using the ARM template https://pastebin.com/ShjazbpR with the cloud-init script:

#cloud-config

# Install additional packages on first boot
#
# Default: none
#
# if packages are specified, this apt_update will be set to true
#
# packages may be supplied as a single package name or as a list
# with the format [<package>, <version>] wherein the specifc
# package version will be installed.
packages:
 - pwgen

Does not install pwgen package nor does it update package list, as documentation https://cloudinit.readthedocs.io/en/latest/topics/examples.html#install-arbitrary-packages states it should update package list: # if packages are specified, this apt_update will be set to true

If I use the customData property under osProfile in this template https://pastebin.com/27s4pgiD everything works as expected, pwgen is installed and package list is updated at first boot.

Does anyone know why userData is not working? Azure documentation https://learn.microsoft.com/azure/virtual-machines/user-data states userData is supported in Azure.


Solution

  • Automatic cloud init is only supported for custom data. If you use user data then it is seen as raw data, and you have to fetch it and handle it yourself. For example you can have a script in your image that runs on startup (only first time, or restarts too), that fetches and processes this data.

    For example:

    curl -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/userData?api-version=2021-01-01&format=text" | base64 --decode >> somefile
    #process somefile...
    

    https://learn.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service?tabs=linux#get-user-data