在 Azure 中将基础结构自动化工具与虚拟机配合使用Use infrastructure automation tools with virtual machines in Azure

若要以一致的方式大规模创建和管理 Azure 虚拟机 (VM),通常需要某种形式的自动化。To create and manage Azure virtual machines (VMs) in a consistent manner at scale, some form of automation is typically desired. 可以通过许多工具和解决方案来自动完成整个 Azure 基础结构部署和管理生命周期。There are many tools and solutions that allow you to automate the complete Azure infrastructure deployment and management lifecycle. 本文介绍了一些可以在 Azure 中使用的基础结构自动化工具。This article introduces some of the infrastructure automation tools that you can use in Azure. 这些工具通常适合以下某个方法:These tools commonly fit in to one of the following approaches:


Ansible 是适用于配置管理、VM 创建或应用程序部署的自动化引擎。Ansible is an automation engine for configuration management, VM creation, or application deployment. Ansible 通常将无代理的模型与 SSH 密钥配合使用,对目标计算机进行身份验证和管理。Ansible uses an agent-less model, typically with SSH keys, to authenticate and manage target machines. 配置任务在 playbook 中定义,可以使用多个 Ansible 模块来执行特定任务。Configuration tasks are defined in playbooks, with a number of Ansible modules available to carry out specific tasks. 有关详细信息,请参阅 How Ansible works(Ansible 工作原理)。For more information, see How Ansible works.


Chef 是一个自动化平台,用于定义基础结构的配置、部署和管理方式。Chef is an automation platform that helps define how your infrastructure is configured, deployed, and managed. 其他组件包括:Chef Habitat,用于应用程序生命周期自动化而不是基础结构;Chef InSpec,用于自动遵循安全和策略要求。Additional components included Chef Habitat for application lifecycle automation rather than the infrastructure, and Chef InSpec that helps automate compliance with security and policy requirements. Chef 客户端安装在目标计算机上,通过一个或多个中心 Chef 服务器来存储和管理配置。Chef Clients are installed on target machines, with one or more central Chef Servers that store and manage the configurations. 有关详细信息,请参阅 An Overview of Chef(Chef 概述)。For more information, see An Overview of Chef.


Puppet 是可供企业使用的自动化平台,用于处理应用程序交付和部署过程。Puppet is an enterprise-ready automation platform that handles the application delivery and deployment process. 代理安装在目标计算机上,因此 Puppet Master 可以通过运行清单来定义 Azure 基础结构和 VM 的所需配置。Agents are installed on target machines to allow Puppet Master to run manifests that define the desired configuration of the Azure infrastructure and VMs. Puppet 可以通过集成其他解决方案(例如 Jenkins 和 GitHub)来改进 DevOps 工作流。Puppet can integrate with other solutions such as Jenkins and GitHub for an improved devops workflow. 有关详细信息,请参阅 How Puppet works(Puppet 工作原理)。For more information, see How Puppet works.


Cloud-init 是一种广泛使用的方法,用于在首次启动 Linux VM 时对其进行自定义。Cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. 可使用 cloud-init 安装程序包和写入文件,或者配置用户和安全。You can use cloud-init to install packages and write files, or to configure users and security. 由于是在初始启动过程中调用 cloud-init,因此无需额外的步骤且无需代理来应用配置。Because cloud-init is called during the initial boot process, there are no additional steps or required agents to apply your configuration. 有关如何正确设置 #cloud-config 文件格式的详细信息,请参阅 cloud-init 文档站点For more information on how to properly format your #cloud-config files, see the cloud-init documentation site. #cloud-config 文件是采用 base64 编码的文本文件。#cloud-config files are text files encoded in base64.

Cloud-init 还支持不同的发行版。Cloud-init also works across distributions. 例如,不要使用 apt-get 安装或 yum 安装来安装包。For example, you don't use apt-get install or yum install to install a package. 可定义要安装的程序包的列表。Instead you can define a list of packages to install. Cloud-init 将为所选发行版自动使用本机包管理工具。Cloud-init automatically uses the native package management tool for the distro you select.

我们正在积极地与我们认可的 Linux 发行版合作伙伴合作,以便在 Azure 市场中提供已启用 cloud-init 的映像。We are actively working with our endorsed Linux distro partners in order to have cloud-init enabled images available in the Azure Marketplace. 这些映像可使 cloud-init 部署和配置无缝地应用于 VM 和虚拟机规模集。These images make your cloud-init deployments and configurations work seamlessly with VMs and virtual machine scale sets. 了解有关 Azure 上的 cloud-init 的更多详细信息:Learn more details about cloud-init on Azure:

PowerShell DSCPowerShell DSC

PowerShell Desired State Configuration (DSC) 是一个管理平台,用于定义目标计算机的配置。PowerShell Desired State Configuration (DSC) is a management platform to define the configuration of target machines. 也可通过 Open Management Infrastructure (OMI) 服务器在 Linux 上使用 DSC。DSC can also be used on Linux through the Open Management Infrastructure (OMI) server.

DSC 配置定义要在计算机上安装的内容,以及如何配置主机。DSC configurations define what to install on a machine and how to configure the host. 本地配置管理器 (LCM) 引擎在每个目标节点上运行,此类节点根据推送的配置处理请求的操作。A Local Configuration Manager (LCM) engine runs on each target node that processes requested actions based on pushed configurations. 拉取服务器是一项在中心主机上运行的 Web 服务,用于存储 DSC 配置和关联的资源。A pull server is a web service that runs on a central host to store the DSC configurations and associated resources. 该拉取服务器与每个目标主机上的 LCM 引擎通信,提供所需的配置并报告符合性情况。The pull server communicates with the LCM engine on each target host to provide the required configurations and report on compliance.

了解如何:Learn how to:

Azure 自定义脚本扩展Azure Custom Script Extension

适用于 LinuxWindows 的 Azure 自定义脚本扩展在 Azure VM 上下载和执行脚本。The Azure Custom Script Extension for Linux or Windows downloads and executes scripts on Azure VMs. 可以在创建 VM 时使用该扩展,也可以在 VM 处于使用状态后随时使用该扩展。You can use the extension when you create a VM, or any time after the VM is in use.

可以从 Azure 存储或任何公共位置(例如 GitHub 存储库)下载脚本。Scripts can be downloaded from Azure storage or any public location such as a GitHub repository. 使用自定义脚本扩展时,可以通过在源 VM 上运行的任何语言来编写脚本。With the Custom Script Extension, you can write scripts in any language that runs on the source VM. 可以根据需要使用这些脚本来安装应用程序或配置该 VM。These scripts can be used to install applications or configure the VM as desired. 若要确保凭据的安全,可将密码之类的敏感信息存储在受保护配置中。To secure credentials, sensitive information such as passwords can be stored in a protected configuration. 这些凭据只在 VM 内解密。These credentials are only decrypted inside the VM.

了解如何:Learn how to:


在 Azure 中创建自定义 VM 映像时,Packer 会自动完成生成过程。Packer automates the build process when you create a custom VM image in Azure. 可以使用 Packer 来定义 OS 并运行配置后脚本,根据具体需求来自定义 VM。You use Packer to define the OS and run post-configuration scripts that customize the VM for your specific needs. 配置完成后,会将 VM 作为托管磁盘映像捕获。Once configured, the VM is then captured as a Managed Disk image. Packer 自动完成创建源 VM、网络和存储资源,运行配置脚本,然后创建 VM 映像这一过程。Packer automates the process to create the source VM, network and storage resources, run configuration scripts, and then create the VM image.

了解如何:Learn how to:


Terraform 是一项自动化工具,用于通过 HashiCorp Configuration Language (HCL) 这个单一模板格式语言来定义和创建整个 Azure 基础结构。Terraform is an automation tool that allows you to define and create an entire Azure infrastructure with a single template format language - the HashiCorp Configuration Language (HCL). 可以使用 Terraform 来定义模板,自动完成为给定应用程序解决方案创建网络、存储和 VM 资源这一过程。With Terraform, you define templates that automate the process to create network, storage, and VM resources for a given application solution. 可以将适合其他平台的现有 Terraform 模板用于 Azure 以确保一致性并简化基础结构部署,不需转换为 Azure 资源管理器模板。You can use your existing Terraform templates for other platforms with Azure to ensure consistency and simplify the infrastructure deployment without needing to convert to an Azure Resource Manager template.

Azure 自动化Azure Automation

Azure 自动化使用 Runbook 在目标 VM 上处理一组任务。Azure Automation uses runbooks to process a set of tasks on the VMs you target. Azure 自动化用于管理现有 VM,而不是创建基础结构。Azure Automation is used to manage existing VMs rather than to create an infrastructure. Azure 自动化可以跨 Linux 和 Windows VM 运行,还可以通过混合 Runbook 辅助角色在本地虚拟机或物理计算机上运行。Azure Automation can run across both Linux and Windows VMs, as well as on-premises virtual or physical machines with a hybrid runbook worker. 可以将 Runbook 存储在源代码管理存储库(例如 GitHub)中。Runbooks can be stored in a source control repository, such as GitHub. 然后即可手动运行或按定义的计划运行这些 Runbook。These runbooks can then run manually or on a defined schedule.

Azure 自动化还提供 Desired State Configuration (DSC) 服务,用于针对给定的一组 VM 的配置方式创建定义。Azure Automation also provides a Desired State Configuration (DSC) service that allows you to create definitions for how a given set of VMs should be configured. 然后,DSC 就可以确保所需配置得到应用且 VM 保持一致。DSC then ensures that the required configuration is applied and the VM stays consistent. Azure 自动化 DSC 可以在 Windows 和 Linux 计算机上运行。Azure Automation DSC runs on both Windows and Linux machines.

了解如何:Learn how to:

Azure DevOps ServicesAzure DevOps Services

Azure DevOps Services 是一套工具,用于共享和跟踪代码、使用自动化生成,以及创建完整的持续集成和开发 (CI/CD) 管道。Azure DevOps Services is a suite of tools that help you share and track code, use automated builds, and create a complete continuous integration and development (CI/CD) pipeline. Azure DevOps Services 集成了 Visual Studio 和其他编辑器,简化了使用过程。Azure DevOps Services integrates with Visual Studio and other editors to simplify usage. Azure DevOps Services 还可以创建和配置 Azure VM,然后向其部署代码。Azure DevOps Services can also create and configure Azure VMs and then deploy code to them.

了解详细信息:Learn more about:


Jenkins 是一种持续集成服务器,用于部署和测试应用程序,以及创建进行代码交付的自动化管道。Jenkins is a continuous integration server that helps deploy and test applications, and create automated pipelines for code delivery. 可以通过成百上千的插件来扩展核心 Jenkins 平台,还可以通过 Webhook 集成许多其他的产品和解决方案。There are hundreds of plugins to extend the core Jenkins platform, and you can also integrate with many other products and solutions through webhooks. 可以在 Azure VM 上手动安装 Jenkins,从 Docker 容器内部运行 Jenkins,或者使用预建的 Azure 市场映像。You can manually install Jenkins on an Azure VM, run Jenkins from within a Docker container, or use a pre-built Azure Marketplace image.

Azure Resource Manager 模板Azure Resource Manager template

Azure 资源管理器是 Azure 的部署和管理服务。Azure Resource Manager is the deployment and management service for Azure. 它提供一个管理层用于在 Azure 订阅中创建、更新和删除资源。It provides a management layer that enables you to create, update, and delete resources in your Azure subscription. 部署后,可以使用访问控制、锁和标记等管理功能来保护和组织资源。You use management features, like access control, locks, and tags, to secure and organize your resources after deployment.

了解如何:Learn how to:

后续步骤Next steps

可以通过许多不同的选项,在 Azure 中使用基础结构自动化工具。There are many different options to use infrastructure automation tools in Azure. 可以自由使用最适合自己的需求和环境的解决方案。You have the freedom to use the solution that best fits your needs and environment. 若要开始尝试一些内置到 Azure 的工具,请了解如何自动完成 LinuxWindows VM 的自定义。To get started and try some of the tools built-in to Azure, see how to automate the customization of a Linux or Windows VM.