Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  《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例子为例:

  https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/virtual-networks/multiple-subnets

 

  Terraform 运行时会读取工作目录中所有的 *.tf, *.tfvars 文件,所以我们不必把所有的东西都写在单个文件中去,应按职责分列在不同的文件中,例如:

  provider.tf                -- provider 配置

  terraform.tfvars      -- 配置 provider 要用到的变量

  varable.tf                  -- 通用变量

  resource.tf                -- 资源定义

  data.tf                        -- 包文件定义

  output.tf                    -- 输出

 

  在这个例子里,我们先观察:https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/examples/virtual-networks/multiple-subnets/main.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
posted on 2020-05-05 17:29  Lei Zhang的博客  阅读(1826)  评论(0编辑  收藏  举报