Skip to content

Commit bd3d10d

Browse files
authored
Update custom-data.md
1 parent 496c0f8 commit bd3d10d

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed
Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Custom Data and Cloud-Init on Azure Virtual Machines
2+
title: Custom Data and Azure Virtual Machines
33
description: Details on using Custom Data and Cloud-Init on Azure Virtual Machines
44
services: virtual-machines
55
author: mimckitt
@@ -13,10 +13,79 @@
1313

1414
## What is Custom Data?
1515

16-
Customers often ask how they can inject a script or other metadata into a Microsoft Azure virtual machine at provision time. In other clouds this concept is often referred to as user data. In Microsoft Azure we have a similar feature called custom data. Custom data is sent to the VM along with the other provisioning configuration information such as the new hostname, username, password, certificates and keys, etc. This data is passed to the Azure API as base64-encoded data. On Windows this data ends up in %SYSTEMDRIVE%\AzureData\CustomData.bin as a binary file. On Linux, this data is passed to the VM via the ovf-env.xml file, which is copied to the /var/lib/waagent directory during provisioning. Newer versions of the Microsoft Azure Linux Agent will also copy the base64-encoded data to /var/lib/waagent/CustomData as well for convenience.
16+
Customers often ask how they can inject a script or other metadata into a Microsoft Azure virtual machine at provision time. In other clouds this concept is often referred to as user data. In Microsoft Azure we have a similar feature called custom data.
1717

18-
## What About Cloud-Init?
19-
Currently, only the Ubuntu images in the Microsoft Azure Gallery have cloud-init preinstalled and configured to act on the custom data sent during provisioning. This means that for Ubuntu you can use custom data to provision a VM using a cloud-init configuration file, or just simply send a script that will be executed by cloud-init during provisioning. See the cloud-init documentation for more information. If cloud-init is not available on the image, then you can still make use of the custom data provided you preinstall a script or other tool on the system that can read the data. In this case a script could be installed to run at boot time that can read in the custom data via the %SYSTEMDRIVE%\AzureData\CustomData.bin (Windows) or the /var/lib/waagent/ovf-env.xml (Linux) file, decode it and act on the data. Once the script is installed the Windows or Linux image can then be deprovisioned and captured for reuse.
18+
Custom data is only made available to the VM during first boot/initial setup, we call this 'provisioning'. Provisioning is the process where VM Create parameters (e.g. hostname, username, password, certificates, custom data, keys etc.) are made available to the VM and a provisioning agent processes them, such as the [Linux Agent](https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/agent-linux) and [cloud-init](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#troubleshooting-cloud-init).
2019

21-
## How Does It Work?
22-
Currently, the easiest way to inject custom data into an IaaS VM is to use the Windows Azure command-line tools. Support for this feature in the Microsoft Azure Powershell cmdlets is not yet available, but is coming soon in an upcoming release. As of version 0.7.5 of the CLI tools there is a new parameter called --custom-data. This parameter takes a file name as an argument, and the tools will then base64 encode the contents of the file and send it along with the provisioning configuration information. The only limit here is that the file must be less than 64KB or the Azure API will not accept the request. The following is a simple example of how to provision and pass custom data to an Ubuntu Linux VM: In this example, custom-data.txt could be a cloud-init configuration file, or simply a shell script (as long as it starts with #!, then cloud-init will execute it). Give it a try.
20+
21+
## Passing custom data to the VM
22+
To use custom data, you must base64 encode the contents first before passing it to the API, unless you are using a CLI tool that does the conversion for you, such as AZ CLI. The size cannot exceed 64KB.
23+
24+
This is a CLI example, where you can pass your custom data as a file, and it will be converted to base64.
25+
```bash
26+
az vm create \
27+
--resource-group myResourceGroup \
28+
--name centos74 \
29+
--image OpenLogic:CentOS-CI:7-CI:latest \
30+
--custom-data cloud-init.txt \
31+
--generate-ssh-keys
32+
```
33+
34+
In Azure Resource Manager (ARM), there is a [base64 function](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-string#base64).
35+
36+
```json
37+
"name": "[parameters('virtualMachineName')]",
38+
"type": "Microsoft.Compute/virtualMachines",
39+
"apiVersion": "2019-07-01",
40+
"location": "[parameters('location')]",
41+
"dependsOn": [
42+
..],
43+
"variables": {
44+
"customDataBase64": "[base64(parameters('stringData'))]"
45+
},
46+
"properties": {
47+
..
48+
"osProfile": {
49+
"computerName": "[parameters('virtualMachineName')]",
50+
"adminUsername": "[parameters('adminUsername')]",
51+
"adminPassword": "[parameters('adminPassword')]",
52+
"customDataBase64": "[variables('customData')]"
53+
},
54+
```
55+
56+
## Processing custom data
57+
The provisioning agents installed on the VMs handle interfacing with the platform and placing it on the file system.
58+
59+
### Windows
60+
Custom data is placed in %SYSTEMDRIVE%\AzureData\CustomData.bin as a binary file, but it is not processed. If you wish to process this file, you will need to build a custom image, and write code to process the CustomData.bin.
61+
62+
### Linux
63+
On Linux OS's, custom data is passed to the VM via the ovf-env.xml file, which is copied to the /var/lib/waagent directory during provisioning. Newer versions of the Microsoft Azure Linux Agent will also copy the base64-encoded data to /var/lib/waagent/CustomData as well for convenience.
64+
65+
Azure currently supports two provisioning agents:
66+
* Linux Agent - By default the agent will not process custom data, you will need to build a custom image with this enabled. These are the relevent settings, as per the [documentation](https://github.com/Azure/WALinuxAgent#configuration):
67+
* Provisioning.DecodeCustomData
68+
* Provisioning.ExecuteCustomData
69+
70+
When you enable custom data, and execute a script, it will delay the VM reporting that is it ready or that provisioning has succeeded until the script has completed. If the script exceeds the total VM provisioning time allowance of 40mins, the VM Create will fail. Note, if the script fails to execute, or errors during executing, this is not deemed a fatal provisioning failure, you will need create a notification path to alert you for the completion state of the script.
71+
72+
To troubleshooting custom data execution, review */var/log/waagent.log*
73+
74+
* cloud-init - By default will process custom data by default, cloud-init accepts [multiple formats](https://cloudinit.readthedocs.io/en/latest/topics/format.html) of custom data, such as cloud-init configuration, scripts etc. Similar to the Linux Agent, when cloud-init processes the custom data. If there are errors during execution of the configuration processing or scripts, this is not deemed a fatal provisioning failure, and you will need create a notification path to alert you for the completion state of the script. However, different to the Linux Agent, cloud-init does not wait on user custom data configurations to complete before reporting to the platform that the VM is ready. For more information on cloud-init on azure, please review the [documentation](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init).
75+
76+
77+
To troubleshooting custom data execution, review the troubleshooting [documentation](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#troubleshooting-cloud-init).
78+
79+
80+
## FAQ
81+
### Can I update custom data after the VM has been created?
82+
For single VMs, custom data in the VM model cannot be updated, but for VMSS, you can update VMSS custom data via REST API (this does not work for the PS or AZ CLI clients). When you update custom data in the VMSS model, this will happen:
83+
* Existing instances in the VMSS will not get the updated custom data, only until they are reimaged.
84+
* Existing instances in the VMSS that are upgraded will not get the updated custom data.
85+
* New instances will recieve the new custom data.
86+
87+
### Can I place sensitive values in custom data?
88+
<We need to get a CELA answer for this>
89+
90+
### Is custom data made available in IMDS?
91+
No, this not currently available.

0 commit comments

Comments
 (0)