Terraform 变量使用

LinuxBeginner
立即练习

介绍

Terraform 是一个强大工具,可用于安全、高效地构建、更改和维护基础设施的版本。使 Terraform 配置灵活且可重用的一个关键特性是使用输入变量(input variables)。变量允许你参数化(parameterize)配置,这样你就可以避免硬编码(hardcoding)值,并在不更改源代码的情况下轻松自定义部署。

在这个 Lab 中,你将学习使用 Terraform 变量的基础知识。你将创建一个生成本地文件的简单配置。该文件的名称和内容将由你在执行 Terraform 命令时定义和传入的变量决定。

完成本 Lab 后,你将了解:

  • 如何在 .tf 文件中声明变量。
  • 如何在资源块(resource block)中引用变量。
  • 在运行 terraform apply 时如何为变量提供值。
这是一个实验(Guided Lab),提供逐步指导来帮助你学习和实践。请仔细按照说明完成每个步骤,获得实际操作经验。根据历史数据,这是一个 初级 级别的实验,完成率为 98%。获得了学习者 97% 的好评率。

创建 variables.tf 文件以定义变量

在此步骤中,你将创建一个专门的文件来存放变量声明。虽然你可以在任何 .tf 文件中声明变量,但通常的约定是将它们放在名为 variables.tf 的文件中。这种做法有助于保持项目整洁,并让其他人轻松了解你的配置需要哪些输入值。

你所有的操作都将在 ~/project 目录下进行。首先,使用 touch 命令创建 variables.tf 文件。

touch variables.tf

你可以通过列出目录内容来验证文件是否已创建:

ls

你应该在输出中看到 variables.tf

variables.tf

目前,该文件是空的。在接下来的步骤中,你将向其中添加变量定义。

定义用于文件名的字符串变量

在此步骤中,你将定义第一个变量。此变量将用于指定 Terraform 将创建的文件的名称。

变量使用 variable 块进行声明。每个块定义一个输入变量。让我们定义一个名为 filename 的变量。

使用 nano 编辑器打开你在上一步中创建的 variables.tf 文件。

nano variables.tf

现在,向文件中添加以下代码。此代码声明了一个名为 filename 的变量,将其类型指定为 string,并包含一个有用的描述。

variable "filename" {
  description = "The name of the file to create."
  type        = string
}
  • variable "filename": 这启动了名为 filename 的变量的声明。
  • description: 此可选参数提供了变量用途的人类可读描述。
  • type: 此参数指定变量的数据类型。这里,我们使用 string 来表示它将接受文本值。

添加代码后,按 Ctrl+X,然后按 Y,最后按 Enter 保存文件并退出 nano

定义用于内容的字符串变量

在此步骤中,你将定义第二个变量,用于保存将写入文件中的内容。这遵循与上一步相同的模式。

再次使用 nano 打开 variables.tf 文件。

nano variables.tf

在之前添加的 filename 变量定义下方,添加以下 variable 块。

variable "content" {
  description = "The content to write into the file."
  type        = string
}

你完整的 variables.tf 文件现在应该如下所示:

variable "filename" {
  description = "The name of the file to create."
  type        = string
}

variable "content" {
  description = "The content to write into the file."
  type        = string
}

这为你的 Terraform 配置定义了两个输入参数:一个用于文件名,一个用于其内容。

保存文件并退出 nanoCtrl+XYEnter)。

在 local_file 资源块中引用变量

在此步骤中,你将创建主要的 Terraform 配置文件 main.tf,并使用你刚刚定义的变量。你将使用 local_file 资源,它是 hashicorp/local provider 的一部分,用于在本地文件系统上创建文件。

首先,创建一个名为 main.tf 的新文件。

nano main.tf

现在,将以下配置添加到 main.tf 文件中。

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.4.0"
    }
  }
}

resource "local_file" "my_file" {
  filename = var.filename
  content  = var.content
}

我们来分解一下这个配置:

  • terraform { ... }: 此块配置 Terraform 设置,包括所需的 provider。我们告诉 Terraform 我们的配置需要 local provider。
  • resource "local_file" "my_file": 这定义了一个类型为 local_file 的资源,并为其指定了本地名称 my_file
  • filename = var.filename: 这是你引用变量的地方。var. 前缀后跟变量名(filename)告诉 Terraform 将 filename 变量的值用于此参数。
  • content = var.content: 类似地,这会将 content 变量的值赋给资源的 content 参数。

添加代码后,保存文件并退出 nanoCtrl+XYEnter)。

使用变量值运行 terraform apply

在最后一步中,你将执行你的 Terraform 配置并为变量提供值。

首先,你需要初始化 Terraform 工作目录。terraform init 命令会下载并安装配置中定义的 provider(在我们的例子中是 local provider)。

运行以下命令:

terraform init

你应该会看到输出,表明 Terraform 已成功初始化。

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/local versions matching "2.4.0"...
- Installing hashicorp/local v2.4.0...
- Installed hashicorp/local v2.4.0 (signed by HashiCorp)

Terraform has been successfully initialized!
...

现在,你可以应用配置了。要向变量传递值,你可以使用 -var 命令行标志。我们还将使用 -auto-approve 标志来跳过交互式确认提示。

使用 filenamecontent 的值运行 terraform apply 命令:

terraform apply -var="filename=hello.txt" -var="content=Hello, Terraform Variables." -auto-approve

Terraform 现在将执行计划并创建文件。输出将与以下内容类似:

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:

  ## local_file.my_file will be created
  + resource "local_file" "my_file" {
      + content              = "Hello, Terraform Variables."
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "hello.txt"
      + id                   = "..."
    }

Plan: 1 to add, 0 to change, 0 to destroy.

local_file.my_file: Creating...
local_file.my_file: Creation complete after 0s [id=...]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

为了验证一切是否正常工作,使用 cat 命令显示新创建的文件内容。

cat hello.txt

你应该会看到你在变量中提供的内容:

Hello, Terraform Variables.

恭喜!你已成功使用 Terraform 变量创建了一个文件。

总结

在此次实验中,你学习了在 Terraform 中使用变量的基本工作流程。你成功地对配置进行了参数化,使其更具动态性和可重用性。

你已经学会了如何:

  • 在专用的 variables.tf 文件中组织变量声明。
  • 使用 variable 块声明变量,并指定它们的 type(类型)和 description(描述)。
  • 在资源块中使用 var.<variable_name> 语法引用变量。
  • 使用 terraform init 初始化 Terraform 项目以下载必要的 provider。
  • terraform apply 命令中使用 -var 标志在运行时提供变量值。

这些知识是使用 Terraform 创建更复杂和模块化的基础设施即代码(Infrastructure as Code)的关键基础。