Skip to content

Commit 6a6ed32

Browse files
authored
Merge pull request #232646 from divargas-msft/patch-3
[Doc-a-thon] Updating cloudinit-bash-script
2 parents f34275b + be8a76d commit 6a6ed32

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

articles/virtual-machines/linux/cloudinit-bash-script.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: mattmcinnes
55
ms.service: virtual-machines
66
ms.collection: linux
77
ms.topic: how-to
8-
ms.date: 11/29/2017
8+
ms.date: 03/29/2023
99
ms.author: mattmcinnes
1010
ms.subservice: cloud-init
1111
---
@@ -16,11 +16,12 @@ ms.subservice: cloud-init
1616
This article shows you how to use [cloud-init](https://cloudinit.readthedocs.io) to run an existing bash script on a Linux virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. These cloud-init scripts run on first boot once the resources have been provisioned by Azure. For more information about how cloud-init works natively in Azure and the supported Linux distros, see [cloud-init overview](using-cloud-init.md)
1717

1818
## Run a bash script with cloud-init
19+
1920
With cloud-init you do not need to convert your existing scripts into a cloud-config, cloud-init accepts multiple input types, one of which is a bash script.
2021

2122
If you have been using the Linux Custom Script Azure Extension to run your scripts, you can migrate them to use cloud-init. However, Azure Extensions have integrated reporting to alert to script failures, a cloud-init image deployment will NOT fail if the script fails.
2223

23-
To see this functionality in action, create a simple bash script for testing. Like the cloud-init `#cloud-config` file, this script must be local to where you will be running the AzureCLI commands to provision your virtual machine. For this example, create the file in the Cloud Shell not on your local machine. You can use any editor you wish. Enter `sensible-editor simple_bash.sh` to create the file and see a list of available editors. Choose #1 to use the **nano** editor. Make sure that the whole cloud-init file is copied correctly, especially the first line.
24+
To see this functionality in action, create a simple bash script for testing. Like the cloud-init `#cloud-config` file, this script must be local to where you will be running the AzureCLI commands to provision your virtual machine. For this example, create the file in the Cloud Shell not on your local machine. You can use any editor you wish. Make sure that the whole cloud-init file is copied correctly, especially the first line.
2425

2526
```bash
2627
#!/bin/sh
@@ -29,34 +30,50 @@ echo "this has been written via cloud-init" + $(date) >> /tmp/myScript.txt
2930

3031
Before deploying this image, you need to create a resource group with the [az group create](/cli/azure/group) command. An Azure resource group is a logical container into which Azure resources are deployed and managed. The following example creates a resource group named *myResourceGroup* in the *eastus* location.
3132

32-
```azurecli-interactive
33+
```azurecli-interactive
3334
az group create --name myResourceGroup --location eastus
3435
```
3536

3637
Now, create a VM with [az vm create](/cli/azure/vm) and specify the bash script file with `--custom-data simple_bash.sh` as follows:
3738

38-
```azurecli-interactive
39+
```azurecli-interactive
3940
az vm create \
4041
--resource-group myResourceGroup \
41-
--name centos74 \
42-
--image OpenLogic:CentOS:7-CI:latest \
42+
--name vmName \
43+
--image imageCIURN \
4344
--custom-data simple_bash.sh \
4445
--generate-ssh-keys
4546
```
47+
48+
> [!NOTE]
49+
> Replace **myResourceGroup**, **vmName**, and **imageCIURN** values accordingly. Make sure an image with Cloud-init is chosen.
50+
4651
## Verify bash script has run
47-
SSH to the public IP address of your VM shown in the output from the preceding command. Enter your own **publicIpAddress** as follows:
52+
53+
SSH to the public IP address of your VM shown in the output from the preceding command. Enter your own **user** and **publicIpAddress** as follows:
4854

4955
```bash
50-
ssh <publicIpAddress>
56+
ssh <user>@<publicIpAddress>
5157
```
5258

53-
Change to the **/tmp** directory and verify that myScript.txt file exists and has the appropriate text inside of it. If it does not, you can check the **/var/log/cloud-init.log** for more details. Search for the following entry:
59+
Verify that `/tmp/myScript.txt` file exists and has the appropriate text inside of it.
5460

5561
```bash
62+
sudo cat /tmp/myScript
63+
```
64+
65+
If it does not, you can check the `/var/log/cloud-init.log` for more details. Search for the following entry:
66+
67+
```bash
68+
sudo cat /var/log/cloud-init.log
69+
```
70+
71+
```output
5672
Running config-scripts-user using lock Running command ['/var/lib/cloud/instance/scripts/part-001']
5773
```
5874

5975
## Next steps
76+
6077
For additional cloud-init examples of configuration changes, see the following:
6178

6279
- [Add an additional Linux user to a VM](cloudinit-add-user.md)

0 commit comments

Comments
 (0)