Skip to content

1. variable

https://developer.hashicorp.com/terraform/language/values/variables

1.1 使用说明✅

​ 输入变量允许我们自定义Terraform模块的各个方面,而无需更改模块自己的源代码。此功能允许我们跨不同的Terraform配置共享模块,使我们的模块可组合和可重用。

​ 当我们在配置的根模块中声明变量时,我们可以使用CLI选项和环境变量设置它们的值。当我们在子模块中声明它们时,调用的模块应该在模块中传递值。

vim variables.tf

yaml
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}
  • variables.tf文件中定义变量;
  • 在同一个模块的所有变量中必须是唯一的;
  • 可以从环境变量或者文本文件中读取;
  • Terraform默认读取terraform.tfvars;

Variable 可选参数 ,变量块有三个可选参数(加粗)。

  • default 变量的默认值
  • type 变量的类型
  • description 变量的描述信息
  • validation 定义变量验证规则
  • sensitive 限制变量在UI中显示
  • nullable 变量是否可为空

Variable 参数类型

  • any
  • string
  • number
  • bool
  • list()
  • set()
  • map()
  • object([ATTR_NAME = ATTR_TYPE, ...)
  • tuple([, ...])

map

  • 变量读取优先级
  • Environment variables
  • The terraform.tfvars file, if present.
  • The terraform.tfvars.json file, if present.
  • Any *.auto.tfvars or *.auto.tfvars.json files, processed in lexical order of their filenames.
  • Any -var and -var-file options on the command line, in the order they are provided. (This includes variables set by an HCP Terraform workspace.)

1.2 variable block 常用写法✅

vim variables.tf

yaml
variable "region" {

}

variable region {
    type = string
}

variable region {
    default = "cn-beijing"
}

//推荐这个方式
variable "region" {
    type = string
    default = "cn-beijing"
    description = "The name of the ECS zone"
}
variable "region" {

}

variable region {
    type = string
}

variable region {
    default = "cn-beijing"
}

//推荐这个方式
variable "region" {
    type = string
    default = "cn-beijing"
    description = "The name of the ECS zone"
}

1.3 定义变量✅

变量允许自定义 Terraform 模块,而无需更改模块自己的源代码。这可以实现跨不同的 Terraform 配置共享模块,使模缺可组合和可重用。

vim variables.tf

yaml
variable "region" {
    description = "custom region"
    type = string
    default = "cn-shanghai"
}

variable "ak" {
    description = "alicloud access key"
    type = string
    sensitive = false
}

variable "sk" {
    description = "alicloud secret key"
    type = string
    sensitive = false
}
variable "region" {
    description = "custom region"
    type = string
    default = "cn-shanghai"
}

variable "ak" {
    description = "alicloud access key"
    type = string
    sensitive = false
}

variable "sk" {
    description = "alicloud secret key"
    type = string
    sensitive = false
}

1.4 使用变量(传参)✅

vim terraform.tfvars

yaml
#引用变量
region  = "cn-shanghai"
ak      = "xxx"
sk      = "xxx"
#引用变量
region  = "cn-shanghai"
ak      = "xxx"
sk      = "xxx"

使用var.变量名字,来调用该值

比如:

vim provider.tf

yaml
#定义云厂商
provider "alicloud" {
  region     = var.region
  access_key = var.ak   #修改成自己的ak
  secret_key = var.sk  #修改成自己的sk
}
#定义云厂商
provider "alicloud" {
  region     = var.region
  access_key = var.ak   #修改成自己的ak
  secret_key = var.sk  #修改成自己的sk
}

Terraform加载变量值的顺序:

1.环境变量

2.terraform.tfvars 文件,在同一级别创建,否则识别不了

3.terraform.tfvars.json文件。

4.所有的.auto.tfvars或者.auto.tfvars.json文件,以字母顺序排序处理。

5.通过-var或是-var-file命令行参数传递的输入变量,按照在命令行参数中定义的顺序加载。

💡 说明

  • variables.tf通常与terraform.tfvars文件连用,程序会读取同目录下terraform.tfvars文件中的变量,当terraform.tfvars文件中有对应变量,则使用对应变量定义的值作为实参输入。若无对应变量,则在terraformapply时以交互的方式要求手动输入值。

  • terraform.tfvars文件名及扩展名为固定,不可更改为其他如terraform2.tfvars等,否则程序认不到。