在 Azure 上的 Linux VM 中使用 cloud-init 运行 bash 脚本Use cloud-init to run a bash script in a Linux VM in Azure

本文介绍如何在 Azure 中使用 cloud-init 在预配时在 Linux 虚拟机 (VM) 或虚拟机规模集 (VMSS) 中运行现有的 bash 脚本。This article shows you how to use cloud-init to run an existing bash script on a Linux virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. Azure 预配资源后,这些 cloud-init 脚本即会在第一次启动时运行。These cloud-init scripts run on first boot once the resources have been provisioned by Azure. 有关 cloud-init 如何在 Azure 以及受支持的 Linux 发行版中本机工作的详细信息,请参阅 cloud-init 概述For more information about how cloud-init works natively in Azure and the supported Linux distros, see cloud-init overview

使用 cloud-init 运行 bash 脚本Run a bash script with cloud-init

使用 cloud-init 时,不需要将现有的脚本转换为 cloud-config,因为 cloud-init 接受多种输入类型,其中一种类型就是 bash 脚本。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.

如果已在使用 Linux 自定义脚本 Azure 扩展运行脚本,则可以迁移它们以使用 cloud-init。If you have been using the Linux Custom Script Azure Extension to run your scripts, you can migrate them to use cloud-init. 但是,Azure 扩展已集成报告功能,当脚本失败时会发出警报,因此如果脚本失败,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.

若要了解此功能的工作原理,请创建一个简单的 bash 脚本用于测试。To see this functionality in action, create a simple bash script for testing. 与 cloud-init #cloud-config 文件一样,此脚本必须位于预配虚拟机时运行 AzureCLI 命令的本地位置。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 on your local machine. 可使用任何想要使用的编辑器。You can use any editor you wish. 输入 sensible-editor simple_bash.sh 以创建文件并查看可用编辑器的列表。Enter sensible-editor simple_bash.sh to create the file and see a list of available editors. 选择 #1 以使用 nano 编辑器 。Choose #1 to use the nano editor. 请确保已正确复制整个 cloud-init 文件,尤其是第一行。Make sure that the whole cloud-init file is copied correctly, especially the first line.

#!/bin/sh
echo "this has been written via cloud-init" + $(date) >> /tmp/myScript.txt

在部署此映像之前,需要使用 az group create 命令创建资源组。Before deploying this image, you need to create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“chinaeast”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the chinaeast location.

az group create --name myResourceGroup --location chinaeast

现在,使用 az vm create 创建 VM,并通过 --custom-data simple_bash.sh 指定 bash 脚本文件,如下所示:Now, create a VM with az vm create and specify the bash script file with --custom-data simple_bash.sh as follows:

az vm create \
  --resource-group myResourceGroup \
  --name centos74 \
  --image OpenLogic:CentOS:7-CI:latest \
  --custom-data simple_bash.sh \
  --generate-ssh-keys 

验证 bash 脚本是否已运行Verify bash script has run

通过 SSH 连接到 VM 的公共 IP 地址显示在先前命令的输出中。SSH to the public IP address of your VM shown in the output from the preceding command. 按如下所示输入自己的 publicIpAddressEnter your own publicIpAddress as follows:

ssh <publicIpAddress>

切换到 /tmp 目录,检查该 myScript.txt 文件是否存在并且包含相应的文本。Change to the /tmp directory and verify that myScript.txt file exists and has the appropriate text inside of it. 如果不是,请检查 /var/log/cloud-init.log 了解更多详细信息。If it does not, you can check the /var/log/cloud-init.log for more details. 搜索以下条目:Search for the following entry:

Running config-scripts-user using lock Running command ['/var/lib/cloud/instance/scripts/part-001']

后续步骤Next steps

有关配置更改的其他 cloud-init 示例,请参阅以下文章:For additional cloud-init examples of configuration changes, see the following: