Skip to content

Commit e0c8c77

Browse files
Freshness pass.
1 parent b7d2da8 commit e0c8c77

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

articles/virtual-machines/extensions/custom-script-linux.md

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ ms.date: 03/29/2023
1414

1515
The Custom Script Extension Version 2 downloads and runs scripts on Azure virtual machines (VMs). Use this extension for post-deployment configuration, software installation, or any other configuration or management task. You can download scripts from Azure Storage or another accessible internet location, or you can provide them to the extension runtime.
1616

17-
The Custom Script Extension integrates with Azure Resource Manager templates. You can also run it by using the Azure CLI, PowerShell, or the Azure Virtual Machines REST API.
17+
The Custom Script Extension integrates with Azure Resource Manager templates. You can also run it by using the Azure CLI, Azure PowerShell, or the Azure Virtual Machines REST API.
1818

19-
This article details how to use the Custom Script Extension from the Azure CLI, and how to run the extension by using an Azure Resource Manager template. This article also provides troubleshooting steps for Linux systems.
19+
This article describes how to use the Custom Script Extension from the Azure CLI, and how to run the extension by using an Azure Resource Manager template. This article also provides troubleshooting steps for Linux systems.
2020

2121
There are two versions of the Custom Script Extension:
2222

@@ -37,14 +37,14 @@ You can set the extension to use your Azure Blob Storage credentials so that it
3737

3838
### Internet connectivity
3939

40-
If you need to download a script externally, such as from GitHub or Azure Storage, then you need to open other firewall or network security group (NSG) ports. For example, if your script is located in Azure Storage, you can allow access by using Azure NSG [service tags for Storage](../../virtual-network/network-security-groups-overview.md#service-tags).
40+
To download a script externally, such as from GitHub or Azure Storage, you need to open other firewall or network security group (NSG) ports. For example, if your script is located in Azure Storage, you can allow access by using Azure NSG [service tags for Storage](../../virtual-network/network-security-groups-overview.md#service-tags).
4141

4242
If your script is on a local server, you might still need to open other firewall or NSG ports.
4343

4444
### Tips and tricks
4545

46-
- The highest failure rate for this extension is due to syntax errors in the script. Test that the script runs without errors. Put more logging into the script to make it easier to find failures.
47-
- Write scripts that are idempotent, so running them more than once accidentally doesn't cause system changes.
46+
- The highest failure rate for this extension is due to syntax errors in the script. Verify that the script runs without errors. Put more logging into the script to make it easier to find failures.
47+
- Write scripts that are idempotent, so that running them more than once accidentally doesn't cause system changes.
4848
- Ensure that the scripts don't require user input when they run.
4949
- The script is allowed 90 minutes to run. Anything longer results in a failed provision of the extension.
5050
- Don't put reboots inside the script. Restarting causes problems with other extensions that are being installed, and the extension doesn't continue after the reboot.
@@ -54,14 +54,14 @@ If your script is on a local server, you might still need to open other firewall
5454
- You can have only one version of an extension applied to the VM. To run a second custom script, update the existing extension with a new configuration. Alternatively, you can remove the Custom Script Extension and reapply it with the updated script.
5555
- If you want to schedule when a script runs, use the extension to create a cron job.
5656
- When the script is running, you only see a *transitioning* extension status from the Azure portal or CLI. If you want more frequent status updates for a running script, create your own solution.
57-
- The Custom Script Extension doesn't natively support proxy servers. However, you can use a file transfer tool that supports proxy servers within your script, such as `Curl`.
57+
- The Custom Script Extension doesn't natively support proxy servers. However, you can use a file transfer tool, such as `Curl`, that supports proxy servers within your script.
5858
- Be aware of nondefault directory locations that your scripts or commands might rely on. Have logic to handle this situation.
5959

6060
## Extension schema
6161

6262
The Custom Script Extension configuration specifies things like script location and the command to be run. You can store this information in configuration files, specify it on the command line, or specify it in an Azure Resource Manager template.
6363

64-
You can store sensitive data in a protected configuration, which is encrypted and only decrypted on the target virtual machine. The protected configuration is useful when the execution command includes secrets such as a password. Here's an example:
64+
You can store sensitive data in a protected configuration, which is encrypted and only decrypted on the target VM. The protected configuration is useful when the execution command includes secrets such as a password. Here's an example:
6565

6666
```json
6767
{
@@ -123,26 +123,26 @@ You can store sensitive data in a protected configuration, which is encrypted an
123123
| `apiVersion` | Not applicable | You can find the most up-to-date API version by using [Resource Explorer](https://resources.azure.com/) or by using the command `az provider list -o json` in the Azure CLI. |
124124
| `fileUris` | Optional | URLs for files to be downloaded. |
125125
| `commandToExecute` | Required if `script` isn't set | The entry point script to run. Use this property instead of `script` if your command contains secrets such as passwords. |
126-
| `script` | Required if `commandToExecute` isn't set | A Base64-encoded (and optionally gzip'ed) script run by `/bin/sh`. |
126+
| `script` | Required if `commandToExecute` isn't set | A Base64-encoded and optionally gzip'ed script run by `/bin/sh`. |
127127
| `skipDos2Unix` | Optional | Set this value to `false` if you want to skip dos2unix conversion of script-based file URLs or scripts. |
128128
| `timestamp` | Optional | Change this value only to trigger a rerun of the script. Any integer value is acceptable, as long as it's different from the previous value. |
129129
| `storageAccountName` | Optional | The name of storage account. If you specify storage credentials, all `fileUris` values must be URLs for Azure blobs. |
130130
| `storageAccountKey` | Optional | The access key of the storage account. |
131-
| `managedIdentity` | Optional | The [managed identity](../../active-directory/managed-identities-azure-resources/overview.md) for downloading files: `clientId` (optional, string): The client ID of the managed identity. `objectId` (optional, string): The object ID of the managed identity.|
131+
| `managedIdentity` | Optional | The [managed identity](../../active-directory/managed-identities-azure-resources/overview.md) for downloading files. Values are `clientId` (optional, string), which is the client ID of the managed identity, and `objectId` (optional, string), which is the object ID of the managed identity.|
132+
133+
*Public settings* are sent in clear text to the VM where the script runs. *Protected settings* are encrypted through a key known only to Azure and the VM. The settings are saved to the VM as they were sent. That is, if the settings were encrypted, they're saved encrypted on the VM. The certificate that's used to decrypt the encrypted values is stored on the VM. The certificate is also used to decrypt settings, if necessary, at runtime.
134+
135+
Using public settings might be useful for debugging, but we strongly recommend that you use protected settings.
132136

133137
You can set the following values in either public or protected settings. The extension rejects any configuration where these values are set in both public and protected settings.
134138

135139
- `commandToExecute`
136140
- `script`
137141
- `fileUris`
138142

139-
Using public settings might be useful for debugging, but we strongly recommend that you use protected settings.
140-
141-
Public settings are sent in clear text to the VM where the script runs. Protected settings are encrypted through a key known only to Azure and the VM. The settings are saved to the VM as they were sent. That is, if the settings were encrypted, they're saved encrypted on the VM. The certificate that's used to decrypt the encrypted values is stored on the VM. The certificate is also used to decrypt settings, if necessary, at runtime.
142-
143143
#### Property: skipDos2Unix
144144

145-
The previous version of the Custom Script Extension, `Microsoft.OSTCExtensions.CustomScriptForLinux`, would automatically convert DOS files to UNIX files by translating `\r\n` to `\n`. This translation still exists and is on by default. This conversion is applied to all files downloaded from `fileUris` or the script setting based on either of the following criteria:
145+
The previous version of the Custom Script Extension, `Microsoft.OSTCExtensions.CustomScriptForLinux`, automatically converts DOS files to UNIX files by translating `\r\n` to `\n`. This translation still exists and is on by default. This conversion is applied to all files downloaded from `fileUris` or the script setting based on either of the following criteria:
146146

147147
- The extension is *.sh*, *.txt*, *.py*, or *.pl*. The script setting always matches this criterion because it's assumed to be a script run with */bin/sh*. The script setting is saved as *script.sh* on the VM.
148148
- The file starts with `#!`.
@@ -199,18 +199,18 @@ cat script | gzip -9 | base64 -w 0
199199

200200
The Custom Script Extension uses the following algorithm to run a script:
201201

202-
1. Assert that the length of the script's value doesn't exceed 256 KB.
203-
1. Base64 decode the script's value.
204-
1. _Try_ to gunzip the Base64-decoded value.
205-
1. Write the decoded and optionally decompressed value to disk (*/var/lib/waagent/custom-script/#/script.sh*).
206-
1. Run the script by using `_/bin/sh -c /var/lib/waagent/custom-script/#/script.sh`.
202+
1. Assert that the length of the script's value doesn't exceed 256 KB.
203+
1. Base64 decode the script's value.
204+
1. _Try_ to gunzip the Base64-decoded value.
205+
1. Write the decoded and optionally decompressed value to disk: */var/lib/waagent/custom-script/#/script.sh*.
206+
1. Run the script by using `_/bin/sh -c /var/lib/waagent/custom-script/#/script.sh`.
207207

208208
#### Property: managedIdentity
209209

210210
> [!NOTE]
211211
> This property *must* be specified in protected settings only.
212212
213-
The Custom Script Extension version 2.1 and later supports [managed identities](../../active-directory/managed-identities-azure-resources/overview.md) for downloading files from URLs provided in the `fileUris` setting. This approach allows the Custom Script Extension to access Azure Storage private blobs or containers without the user having to pass secrets like shared access signature (SAS) tokens or storage account keys.
213+
The Custom Script Extension, version 2.1 and later, supports [managed identities](../../active-directory/managed-identities-azure-resources/overview.md) for downloading files from URLs provided in the `fileUris` setting. This approach allows the Custom Script Extension to access Azure Storage private blobs or containers without the user having to pass secrets like shared access signature (SAS) tokens or storage account keys.
214214

215215
To use this feature, add a [system-assigned](../../app-service/overview-managed-identity.md?tabs=dotnet#add-a-system-assigned-identity) or [user-assigned](../../app-service/overview-managed-identity.md?tabs=dotnet#add-a-user-assigned-identity) identity to the VM or Virtual Machine Scale Set where the Custom Script Extension is expected to run. Then [grant the managed identity access to the Azure Storage container or blob](../../active-directory/managed-identities-azure-resources/tutorial-vm-windows-access-storage.md#grant-access).
216216

@@ -282,7 +282,7 @@ You can deploy Azure VM extensions by using Azure Resource Manager templates. Th
282282
283283
## Azure CLI
284284

285-
When you're using the Azure CLI to run the Custom Script Extension, create a configuration file or files. At a minimum, you must have `commandToExecute`.
285+
When you use the Azure CLI to run the Custom Script Extension, create a configuration file or files. At a minimum, the configuration file must contain `commandToExecute`. The `az vm extension set` command refers to the configuration file:
286286

287287
```azurecli
288288
az vm extension set \
@@ -292,7 +292,7 @@ az vm extension set \
292292
--protected-settings ./script-config.json
293293
```
294294

295-
Optionally, you can specify the settings in the command as a JSON-formatted string. This approach allows the configuration to be specified during execution and without a separate configuration file.
295+
Alternatively, you can specify the settings in the command as a JSON-formatted string. This approach allows the configuration to be specified during execution and without a separate configuration file.
296296

297297
```azurecli
298298
az vm extension set \
@@ -316,53 +316,43 @@ This example uses the following script file named *script-config.json*:
316316

317317
1. Create the script file by using the text editor of your choice or by using the following CLI command:
318318

319-
```azurecli
320-
cat <<EOF > script-config.json
321-
{
322-
"fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"],
323-
"commandToExecute": "./config-music.sh"
324-
}
325-
EOF
326-
```
319+
```azurecli
320+
cat <<EOF > script-config.json
321+
{
322+
"fileUris": ["https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-linux/scripts/config-music.sh"],
323+
"commandToExecute": "./config-music.sh"
324+
}
325+
EOF
326+
```
327327

328328
1. Run the following command:
329329

330-
```azurecli
331-
az vm extension set \
332-
--resource-group myResourceGroup \
333-
--vm-name myVM --name customScript \
334-
--publisher Microsoft.Azure.Extensions \
335-
--settings ./script-config.json
336-
```
330+
```azurecli
331+
az vm extension set \
332+
--resource-group myResourceGroup \
333+
--vm-name myVM --name customScript \
334+
--publisher Microsoft.Azure.Extensions \
335+
--settings ./script-config.json
336+
```
337337

338338
### Example: Public configuration with no script file
339339

340-
This example uses the following script file named *script-config.json*:
340+
This example uses the following JSON-formatted content:
341341

342342
```json
343343
{
344344
"commandToExecute": "apt-get -y update && apt-get install -y apache2"
345345
}
346346
```
347347

348-
1. Create the script file by using the text editor of your choice or by using the following CLI command:
349-
350-
```azurecli
351-
cat <<EOF > script-config.json
352-
{
353-
"commandToExecute": "apt-get -y update && apt-get install -y apache2"
354-
}
355-
EOF
356-
```
357-
358-
1. Run the following command:
348+
Run the following command:
359349

360350
```azurecli
361351
az vm extension set \
362-
--resource-group myResourceGroup \
363-
--vm-name myVM --name customScript \
352+
--resource-group tim0329vmRG \
353+
--vm-name tim0329vm --name customScript \
364354
--publisher Microsoft.Azure.Extensions \
365-
--settings ./script-config.json
355+
--settings '{"commandToExecute": "apt-get -y update && apt-get install -y apache2"}'
366356
```
367357

368358
### Example: Public and protected configuration files
@@ -379,7 +369,7 @@ Use a protected configuration file to specify the command to be run:
379369

380370
```json
381371
{
382-
"commandToExecute": "./config-music.sh <param1>"
372+
"commandToExecute": "./config-music.sh"
383373
}
384374
```
385375

@@ -398,7 +388,7 @@ EOF
398388
```azurecli
399389
cat <<EOF > protected-config.json
400390
{
401-
"commandToExecute": "./config-music.sh <param1>"
391+
"commandToExecute": "./config-music.sh"
402392
}
403393
EOF
404394
```
@@ -408,7 +398,7 @@ EOF
408398
```azurecli
409399
az vm extension set \
410400
--resource-group myResourceGroup \
411-
--vm-name myVM \
401+
--vm-name myVM \
412402
--name customScript \
413403
--publisher Microsoft.Azure.Extensions \
414404
--settings ./script-config.json \

0 commit comments

Comments
 (0)