更新时间:2023-05-23 GMT+08:00
分享

其他命令

terraform validate

该命令用于快速检查配置文件中的语法错误,无需通过 plan/apply 命令便可定位错误的详细位置和原因。

  • 检验正确
    $ terraform validate
    The configuration is valid.
  • 检验错误
    $ terraform validate
    Error: Missing newline after argument
      on main.tf line 34, in data "huaweicloud_identity_role" "auth_admin":
      34:     name = "system_all"]
    An argument definition must end with a newline.

terraform fmt

该命令用于将当前目录及其子目录下的所有的 .tf 文件进行格式化,使其代码风格统一。

terraform graph

该命令用于根据配置文件或执行计划输出资源的可视化依赖关系,命令的输出为 DOT 格式数据。

$ terraform graph
digraph {
  compound = "true"
  newrank = "true"
  subgraph "root" {
    "[root] huaweicloud_vpc.vpc_1 (expand)" [label = "huaweicloud_vpc.vpc_1", shape = "box"]
    "[root] huaweicloud_vpc_subnet.subnet_1 (expand)" [label = "huaweicloud_vpc_subnet.subnet_1", shape = "box"]
    "[root] provider[\"registry.terraform.io/huaweicloud/huaweicloud\"]" [label = "provider[\"registry.terraform.io/huaweicloud/huaweicloud\"]", shape = "diamond"]
    "[root] huaweicloud_vpc.vpc_1 (expand)" -> "[root] provider[\"registry.terraform.io/huaweicloud/huaweicloud\"]"
    "[root] huaweicloud_vpc_subnet.subnet_1 (expand)" -> "[root] huaweicloud_vpc.vpc_1 (expand)"
    "[root] meta.count-boundary (EachMode fixup)" -> "[root] huaweicloud_vpc_subnet.subnet_1 (expand)"
    "[root] provider[\"registry.terraform.io/huaweicloud/huaweicloud\"] (close)" -> "[root] huaweicloud_vpc_subnet.subnet_1 (expand)"
    "[root] root" -> "[root] meta.count-boundary (EachMode fixup)"
    "[root] root" -> "[root] provider[\"registry.terraform.io/huaweicloud/huaweicloud\"] (close)"
  }
}

我们通过 dot 命令将结果转换为可视化的图表:

$ apt instal graphviz
$ terraform graph | dot -Tsvg > graph.svg

terraform import

该命令用于将存量的资源导入到 state文件中,命令格式为:terraform import <资源类型>.<资源名称> <资源ID>

$ terraform import huaweicloud_nat_dnat_rule.my_dnat_rule 130fcccf-a587-414f-b51f-e3b2dfa06b92
huaweicloud_nat_dnat_rule.my_dnat_rule: Importing from ID "130fcccf-a587-414f-b51f-e3b2dfa06b92"...
  Prepared huaweicloud_nat_dnat_rule for import
huaweicloud_nat_dnat_rule.my_dnat_rule: Refreshing state... [id=130fcccf-a587-414f-b51f-e3b2dfa06b92]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

terraform taint

该命令用于手动地将资源标记为污染状态 (tainted)。一旦资源被标记为“tainted”,当再次执行 terraform apply 命令时,Terraform 会将该资源被销毁并重新创建。命令格式为:terraform taint <资源类型>.<资源名称>
$ terraform taint huaweicloud_vpc_subnet.subnet_1
Resource instance huaweicloud_vpc_subnet.subnet_1 has been marked as tainted.

标记后使用 terraform show 命令可以看到相应资源状态已改变:

$ terraform show
...
# huaweicloud_vpc_subnet.subnet_1: (tainted)
resource "huaweicloud_vpc_subnet" "subnet_1" {
  ...
}

terraform untaint

该命令用于手动地取消资源的污染状态,使其恢复到正常状态,是 taint 的逆操作。在某些异常情况下,Terraform 会将资源标记为污染状态,我们可以手工处理后,使用 terraform untaint 命令取消被污染标记,这样可以避免资源的重新创建。命令格式为:terraform untaint <资源类型>.<资源名称>

$ terraform untaint huaweicloud_vpc_subnet.subnet_1
Resource instance huaweicloud_vpc_subnet.subnet_1 has been successfully untainted.

terraform console

该命令可以打开一个交互式的控制台,我们可以使用该控制台进行表达式及内置函数的体验和测试。

$ terraform console
> length("Hello, cloud!")
13
分享:

    相关文档

    相关产品