1.6.1. Terraform命令行

我们在前面的的章节中主要介绍了如何书写和组织Terraform代码,下面我们要介绍一下如何使用Terraform命令行工具来应用这些代码,并且管理和操作我们的云端基础设施。

Terraform是用Go语言编写的,所以它的交付物只有一个可执行命令行文件:terraform。在Terraform执行发生错误时,terraform进程会返回一个非零值,所以在脚本代码中我们可以轻松判断执行是否成功。

我们可以在命令行中输入terraform来查看所有可用的子命令:

$ terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    0.13upgrade        Rewrites pre-0.13 module source code for v0.13
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

1.6.1.1. 通过 -chdir 参数切换工作目录

运行Terraform时一般要首先切换当前工作目录到包含有想要执行的根模块.tf代码文件的目录下(比如使用cd命令),这样Terraform才能够自动发现要执行的代码文件以及参数文件。

在某些情况下——尤其是将Terraform封装进某些自动化脚本时,如果能够从其他路径直接执行特定路径下的根模块代码将会十分的方便。为了达到这一目的,Terraform目前支持一个全局参数-chdir=...,你可以在任意子命令的参数中使用该参数指定要执行的代码路径:

$ terraform -chdir=environments/production apply

-chdir参数指引Terraform在执行具体子命令之前切换工作目录,这意味着使用该参数后Terraform将会在指定路径下读写文件,而非当前工作目录下的文件。

在两种场景下Terraform会坚持使用当前工作目录而非指定的目录,即使是我们通过-chdir指定了一个目标路径:

  • Terraform处理命令行配置文件中的设置而非执行某个具体的子命令时,该阶段发生在解析-chdir参数之前
  • 如果你需要使用当前工作目录下的文件作为你配置的一部分时,你可以通过在代码中使用path.cwd变量获得对当前工作路径的引用,而不是-chdir所指定的路径。可以通过使用path.root来获取代表根模块所在的路径。

1.6.1.2. 自动补全

如果你使用的是bash或是zsh,那么可以轻松安装自动补全:

$ terraform -install-autocomplete

卸载自动补全也很容易:

$ terraform -uninstall-autocomplete

目前自动补全并没有覆盖到所有子命令。

1.6.1.3. 版本信息

Terraform命令行会与HashiCorp的Checkpoint服务交互来检查当前版本是否有更新或是关键的安全公告。

可以通过执行terraform version命令来检查是否有新版本可用。

1.6.1.4. Checkpoint服务

Terraform会收集一些不涉及用户身份信息或是主机信息的数据发送给Checkpoint服务。一个匿名ID会被发送到Checkpoint来消除重复的告警信息。我们可以关闭与Checkpoint的交互。

我们可以设置CHECKPOINT_DISABLE环境变量的值为任意非空值来完全关闭HashiCorp所有产品与Checkpoint的交互。另外,我们也可以通过设置命令行配置文件来关闭这些功能:

  • disable_checkpoint:设置为true可以完全关闭与Checkpoint的交互
  • disable_checkpoint_signature:设置为true可以阻止向Checkpoint发送匿名ID

results matching ""

    No results matching ""