《Windows Azure Platform 系列文章目录》
熟悉Azure Template的读者都知道:Azure ARM (5) ARM Template初探 - 本地JSON Template文件(1)
Azure Template分为两种类型的文件,资源和变量
(1)Template:资源文件,表示我们需要创建的资源是什么,比如Azure VM,Azure Storage
(2)Parameter:变量文件,表示资源所使用到的参数,比如虚拟机的名字,登陆虚拟机所需要的用户名和密码。存储名称等等。
在使用Terraform时,也会有两种类型的文件。我们以Terraform例子为例:
Terraform 运行时会读取工作目录中所有的 *.tf, *.tfvars 文件,所以我们不必把所有的东西都写在单个文件中去,应按职责分列在不同的文件中,例如:
provider.tf -- provider 配置
terraform.tfvars -- 配置 provider 要用到的变量
varable.tf -- 通用变量
resource.tf -- 资源定义
data.tf -- 包文件定义
output.tf -- 输出
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { #这里会读取variables.tf文件 name = "${var.prefix}-resources" location = "${var.location}" }
上面的var.prefix和var.location,会读取到变量文件
具体的变量内容,在这里被定义:https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/examples/virtual-networks/multiple-subnets/variables.tf
variable "prefix" { description = "The prefix used for all resources in this example" } variable "location" { description = "The Azure location where all resources in this example should be created" }
好了,现在我们用两种方式执行Terraform
第一种:通过读取variable.tf中的变量值,来创建Azure资源
第二种:通过执行terraform -apply 过程中,直接设置变量值
我们开始第一种方法:通过读取variable.tf中的变量值,来创建Azure资源
1.修改varible.tf文件,设置default值。在Terraform执行过程中
我们可以在variable.tf中,设置default值,定义这些变量值。
variable "prefix" { default="leizha" description = "The prefix used for all resources in this example" } variable "location" { default="chinaeast2" description = "The Azure location where all resources in this example should be created" }
2.terraform init,初始化工作目录:
terraform init
Terraform init做的事情就像是git init加上npm install,执行完trraform init之后,会在当前目录中生成.terraform目录,并依照*.tf文件中的执行下载相应的插件
3.terraform plan
该命令让terraform在正式执行之前,提供了预览执行计划的机会,让我们清楚的了解将要做什么
4.terraform apply
这句语句就是真正执行terraform,并显示结果。
terraform apply -auto-approve
注意:上面的auto-approve是跳过交互式批准流程。
因为我们在步骤1中,设置了prefix值,则我们在terraform执行完毕后,通过读取variable.tf中prefix值,创建相应的资源。
执行的结果如下:
第二种:通过执行terraform apply 过程中,直接设置变量值
1.首先我们把上面已经创建的Azure资源删除。图略。
2.观察variable.tf中,设置default值,定义这些变量值。
variable "prefix" { default="leizha" description = "The prefix used for all resources in this example" } variable "location" { default="chinaeast2" description = "The Azure location where all resources in this example should be created" }
3.在terraform apply中,直接设置变量值
下面的-var里面,分别设置了prefix的值和location的值
terraform apply -var 'prefix=contoso' -var 'location=chinaeast2'
4.执行结果:
可以看到,虽然我们在variable.tf中,设置default值。但是在terraform apply中,直接设置了变量值,所以会显示不一样的结果:
var.prefix
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2012-05-05 Windows 8 Platform (三) Windows 8 Developer Preview