LoginSignup
7
1

More than 1 year has passed since last update.

terraform planをファイル出力する方法

Posted at

ある程度規模のある環境を Terraform で管理しているとterraform planの結果をファイル出力したいケースが出てくるかと思います。例えば、一回の変更差分が長いのでファイルとして保存するだったり、CIで出力した結果をどこかに通知、転送したりといったケースです。

自分は当初「結果をリダイレクトするだけでしょ?」と思っていたのですが、少しハマったので備忘録を残します。

NG例

以下のように単純にリダイレクトするだけだとterraform plan > tfplan.txtとやると以下のように不要な文字列がファイルに追加されてしまいます。

$ terraform plan > tfplan.txt
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  [31m-[0m destroy
[0m
Terraform will perform the following actions:

[1m  # google_container_cluster.primary[0m will be [1m[31mdestroyed[0m[0m
[0m  [31m-[0m[0m resource "google_container_cluster" "primary" {
      [31m-[0m [0m[1m[0mcluster_ipv4_cidr[0m[0m           = "10.96.0.0/14" [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0mdefault_max_pods_per_node[0m[0m   = 110 [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_autopilot[0m[0m            = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_binary_authorization[0m[0m = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_intranode_visibility[0m[0m = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_kubernetes_alpha[0m[0m     = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_legacy_abac[0m[0m          = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_shielded_nodes[0m[0m       = false [90m->[0m [0m[90mnull[0m[0m
      [31m-[0m [0m[1m[0menable_tpu[0m[0m                  = false [90m->[0m [0m[90mnull[0m[0m
...

OK例

調べたところ[1m[31mは装飾用の文字コードとのことで、ファイルに出力する場合は環境変数TF_CLI_ARGS-no-colorを追加するか、以下のように実行コマンドでオプションとして渡してやる必要があります。

$ terraform plan -no-color > tfplan.txt
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # google_container_cluster.primary will be destroyed
  - resource "google_container_cluster" "primary" {
      - cluster_ipv4_cidr           = "10.96.0.0/14" -> null
      - default_max_pods_per_node   = 110 -> null
      - enable_autopilot            = false -> null
      - enable_binary_authorization = false -> null
      - enable_intranode_visibility = false -> null
      - enable_kubernetes_alpha     = false -> null
      - enable_legacy_abac          = false -> null
...

以上「terraform planをファイル出力する方法」でした。

7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1