4

How to Run Cloud Init YAML with Azure Bicep when Creating VM

 1 year ago
source link: https://edi.wang/post/2023/3/5/how-to-run-cloud-init-yaml-with-azure-bicep-when-creating-vm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Run Cloud Init YAML with Azure Bicep when Creating VM

Original

3/5/2023, 1:47:10 AM

6 Reads


Problem


Azure document for "Create an Ubuntu Linux virtual machine using a Bicep file" did not show how to use custom data to run cloud init yaml file when creating VM.

img-388f3048-2c5c-4efe-b6b4-78cdbce6d320.png

After work 996, I finally figured out the undocumented method to run a cloud init yaml file with Azure Bicep. Let's see how to do it.

Solution


Create a YAML file for cloud init

Create a YAML file in the location where your bicep file can access. For example, I created 251.yml in the same directory with 251.bicep.

img-747084f3-7fc0-4878-9b45-3bf6b3a7ad40.png

Write your cloud init content in the YAML file. For example, install docker

#cloud-config
runcmd:
  - curl -fsSL https://get.docker.com | sh

Load YAML file in Bicep

Define a new variable in your bicep file.

Use loadTextContent() to read YAML file content, and then encode it in base64()

var cloudInit = base64(loadTextContent('251.yml'))

Modify osProfile node

Find osProfile node in your bicep file, add customData into it.

osProfile: {
  computerName: vmName
  adminUsername: adminUsername
  adminPassword: adminPassword
  linuxConfiguration: null
  customData: cloudInit // <-- add this line
}

Run Bicep file

Run your bicep file to test it out. Now you can see the cloud init file is working, docker is automatically installed.

img-d49159d2-2a44-44fe-b6d0-3c038a7d7730.png

Reference:


This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK