Terraform
定义输入变量
你现在已经掌握了足够的 Terraform 知识来创建有用的配置,但到目前为止的示例都使用了硬编码值。Terraform 配置可以包含变量,使你的配置更具动态性和灵活性。
定义你的变量
在你的 learn-terraform-azure 目录下,创建一个名为 variables.tf 的新文件。复制并粘贴以下变量声明。
variables.tf
variable "resource_group_name" {
default = "myTFResourceGroup"
}
此声明包括变量的默认值,因此 resource_group_name 变量将不会是必需的输入。
使用你的变量更新你的 Terraform 配置
更新你的 azurerm_resource_group 配置,以使用输入变量来表示资源组名称。修改虚拟机块如下
main.tf
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = "westus2"
tags = {
Environment = "Terraform Getting Started"
Team = "DevOps"
}
}
应用你的配置
重新运行 terraform apply 以使用输入变量重新创建你的基础设施。回复 yes 以确认操作。
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
##...
Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
azurerm_resource_group.rg: Creating...
azurerm_resource_group.rg: Creation complete after 1s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_virtual_network.vnet: Creating...
azurerm_virtual_network.vnet: Creation complete after 7s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
现在再次应用配置,这次通过使用 -var 标志传递变量来覆盖默认资源组名称。更新资源组名称是一项破坏性更新,它会强制 Terraform 重创建资源,进而重创建依赖于它的虚拟网络。在确认提示中输入 yes 来重命名资源组并创建新资源。
$ terraform apply -var "resource_group_name=myNewResourceGroupName"
azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_virtual_network.vnet: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# azurerm_resource_group.rg must be replaced
-/+ resource "azurerm_resource_group" "rg" {
~ id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" -> (known after apply)
~ name = "myTFResourceGroup" -> "myNewResourceGroupName" # forces replacement
tags = {
"Environment" = "Terraform Getting Started"
"Team" = "DevOps"
}
# (1 unchanged attribute hidden)
}
# azurerm_virtual_network.vnet must be replaced
-/+ resource "azurerm_virtual_network" "vnet" {
- dns_servers = [] -> null
~ guid = "260a1a64-08ed-4693-84d8-eda800326faa" -> (known after apply)
~ id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet" -> (known after apply)
name = "myTFVnet"
~ resource_group_name = "myTFResourceGroup" -> "myNewResourceGroupName" # forces replacement
~ subnet = [] -> (known after apply)
- tags = {} -> null
# (3 unchanged attributes hidden)
}
Plan: 2 to add, 0 to change, 2 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
azurerm_virtual_network.vnet: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
azurerm_virtual_network.vnet: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...osoft.Network/virtualNetworks/myTFVnet, 10s elapsed]
azurerm_virtual_network.vnet: Destruction complete after 11s
azurerm_resource_group.rg: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...dfb29/resourceGroups/myTFResourceGroup, 10s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...dfb29/resourceGroups/myTFResourceGroup, 20s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...dfb29/resourceGroups/myTFResourceGroup, 30s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...dfb29/resourceGroups/myTFResourceGroup, 40s elapsed]
azurerm_resource_group.rg: Destruction complete after 48s
azurerm_resource_group.rg: Creating...
azurerm_resource_group.rg: Creation complete after 1s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myNewResourceGroupName]
azurerm_virtual_network.vnet: Creating...
azurerm_virtual_network.vnet: Creation complete after 6s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myNewResourceGroupName/providers/Microsoft.Network/virtualNetworks/myTFVnet]
Apply complete! Resources: 2 added, 0 changed, 2 destroyed.
通过命令行设置变量不会保存它们的值。Terraform 支持多种使用和设置变量的方式,以便您避免在执行命令时反复输入它们。要了解更多信息,请遵循我们的深入教程,使用变量自定义 Terraform 配置。