You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/virtual-machines/linux/cloudinit-bash-script.md
+26-9Lines changed: 26 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ author: mattmcinnes
5
5
ms.service: virtual-machines
6
6
ms.collection: linux
7
7
ms.topic: how-to
8
-
ms.date: 11/29/2017
8
+
ms.date: 03/29/2023
9
9
ms.author: mattmcinnes
10
10
ms.subservice: cloud-init
11
11
---
@@ -16,11 +16,12 @@ ms.subservice: cloud-init
16
16
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)
17
17
18
18
## Run a bash script with cloud-init
19
+
19
20
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.
20
21
21
22
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.
22
23
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.
24
25
25
26
```bash
26
27
#!/bin/sh
@@ -29,34 +30,50 @@ echo "this has been written via cloud-init" + $(date) >> /tmp/myScript.txt
29
30
30
31
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.
31
32
32
-
```azurecli-interactive
33
+
```azurecli-interactive
33
34
az group create --name myResourceGroup --location eastus
34
35
```
35
36
36
37
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:
37
38
38
-
```azurecli-interactive
39
+
```azurecli-interactive
39
40
az vm create \
40
41
--resource-group myResourceGroup \
41
-
--name centos74 \
42
-
--image OpenLogic:CentOS:7-CI:latest \
42
+
--name vmName \
43
+
--image imageCIURN \
43
44
--custom-data simple_bash.sh \
44
45
--generate-ssh-keys
45
46
```
47
+
48
+
> [!NOTE]
49
+
> Replace **myResourceGroup**, **vmName**, and **imageCIURN** values accordingly. Make sure an image with Cloud-init is chosen.
50
+
46
51
## 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:
48
54
49
55
```bash
50
-
ssh <publicIpAddress>
56
+
ssh <user>@<publicIpAddress>
51
57
```
52
58
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.
54
60
55
61
```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
56
72
Running config-scripts-user using lock Running command ['/var/lib/cloud/instance/scripts/part-001']
57
73
```
58
74
59
75
## Next steps
76
+
60
77
For additional cloud-init examples of configuration changes, see the following:
61
78
62
79
-[Add an additional Linux user to a VM](cloudinit-add-user.md)
0 commit comments