在本快速入门中,将创建 Azure 自动化帐户,并使用 Terraform 为该帐户分配“读者”角色。 自动化账户是一种基于云的服务,可为运行运行手册提供安全的环境,而运行手册是用于自动执行流程的脚本。 帐户可以自动化在云中管理的频繁、耗时和易出错的任务。 此自动化帐户是在 Azure 资源组中创建的,该资源组是包含 Azure 解决方案相关资源的容器。 此外,“读者”角色被分配给自动化帐户,授予订阅权限以查看自动化帐户中的所有资源,但不进行任何更改。
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 HCL 语法允许你指定云提供商(如 Azure)和构成云基础结构的元素。 创建配置文件后,可以创建 一个执行计划 ,以便在部署基础结构更改之前预览这些更改。 验证更改后,应用执行计划来部署基础结构。
在这篇文章中,你将学会如何:
- 创建具有唯一名称的 Azure 资源组。
- 为 Azure 资源的唯一命名生成随机字符串。
- 创建自动化帐户,并启用公用网络访问。
- 检索当前 Azure 订阅。
- 检索“读者”的角色定义。
- 将“读取者”角色分配给自动化帐户。
- 输出创建的资源组和自动化帐户的名称。
先决条件
创建具有活动订阅的 Azure 帐户。 可以创建帐户。 新自动化帐户的选项组织在 Azure 门户的“创建自动化帐户”页面中的选项卡中。
实现 Terraform 代码
Note
本文的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
main.tf的文件并插入以下代码:
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
resource "random_string" "azurerm_automation_account_name" {
length = 13
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_automation_account" "example" {
name = coalesce(var.automation_account_name, "autoacc-${random_string.azurerm_automation_account_name.result}")
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku_name = "Basic"
identity {
type = "SystemAssigned"
}
public_network_access_enabled = true
}
data "azurerm_subscription" "current" {}
data "azurerm_role_definition" "contributor" {
name = "Contributor"
}
resource "azurerm_role_assignment" "example" {
scope = data.azurerm_subscription.current.id
role_definition_name = "Contributor"
principal_id = azurerm_automation_account.example.identity[0].principal_id
}
- 创建名为
outputs.tf的文件并插入以下代码:
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "automation_account_name" {
value = azurerm_automation_account.example.name
}
- 创建名为
providers.tf的文件并插入以下代码:
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}
- 创建名为
variables.tf的文件并插入以下代码:
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
variable "resource_group_location" {
type = string
default = "chinanorth3"
description = "Location of the resource group."
}
variable "automation_account_name" {
type = string
description = "The name of the Automation Account resource. The value will be randomly generated if blank."
default = ""
}
初始化 Terraform
运行 terraform init,将 Terraform 部署进行初始化。 此命令下载 Azure 提供程序,以便管理您的 Azure 资源。
terraform init -upgrade
要点:
- 参数
-upgrade可将必要的提供程序插件升级到符合配置版本约束的最新版本。
创建 Terraform 执行计划
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
要点:
-
terraform plan命令创建执行计划,但不执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。 - 使用可选
-out参数可以为计划指定输出文件。 使用-out参数可以确保所查看的计划与所应用的计划完全一致。
应用 Terraform 的执行计划
运行 terraform apply 以将执行计划应用到您的云基础架构。
terraform apply main.tfplan
要点:
- 示例
terraform apply命令假设你先前运行了terraform plan -out main.tfplan。 - 如果为
-out参数指定了不同的文件名,请在对terraform apply的调用中使用该相同文件名。 - 如果未使用参数
-out,则无需任何参数即可调用terraform apply。
验证结果
获取 Azure 资源组名称。
resource_group_name=$(terraform output -raw resource_group_name)获取自动化帐户名。
automation_account_name=$(terraform output -raw automation_account_name)运行
az automation account show以查看自动化帐户。az automation account show --name $automation_account_name --resource-group $resource_group_name
清理资源
如果不再需要通过 Terraform 创建的资源,请执行以下步骤:
运行 terraform plan 并指定
destroy标志。terraform plan -destroy -out main.destroy.tfplan要点:
-
terraform plan命令创建执行计划,但不执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。 - 使用可选
-out参数可以为计划指定输出文件。 使用-out参数可以确保所查看的计划与所应用的计划完全一致。
-
运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
解决 Azure 上的 Terraform 问题
解决在 Azure 上使用 Terraform 时的常见问题。
后续步骤
在本快速入门中,你创建了一个自动化帐户。 浏览有关自动化帐户的文章 以了解详细信息。
要在自动化帐户中使用托管标识,请继续执行以下操作:
教程:使用托管标识创建自动化 PowerShell 运行手册