您如何检测 Terraform 脚本是 运行 处于计划还是应用模式?

How do you detect if Terraform script is running in plan or apply mode?

使用 Terraform local-exec provisioner 运行 powershell 脚本在 Azure 中创建 ILB ASE(ILB ASE 在 Terraform Azure 提供程序中尚不直接支持,因此我们在 Powershell 中手动执行)。我们不想在 local-exec 运行 处于计划模式时创建 ILB ASE,因为此操作需要 2 小时。 local-exec 有没有办法检测 terraform 脚本是 运行 计划模式还是应用模式?

Provisioner 在计划阶段不运行。

~\projects\test ⚡                                                                                                                             ◷ 3:31:16 PM
▶ cat .\main.tf
resource "null_resource" "localtest" {
  provisioner "local-exec" {
    command = "New-Item -Name test"
    interpreter = ["pwsh", "-Command"]
  }
}

~\projects\test ⚡                                                                                                                             ◷ 3:31:19 PM
▶ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "null" (hashicorp/null) 2.1.2...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.null: version = "~> 2.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

~\projects\test ⚡                                                                                                                             ◷ 3:31:26 PM
▶ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.localtest will be created
  + resource "null_resource" "localtest" {
      + id = (known after apply)
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.


~\projects\test ⚡                                                                                                                             ◷ 3:31:32 PM
▶ dir test
Get-ChildItem: Cannot find path 'C:\Users\pearcec\projects\test\test' because it does not exist.

~\projects\test ⚡ ⨯                                                                                                                           ◷ 3:32:01 PM
▶ terraform apply -auto-approve
null_resource.localtest: Creating...
null_resource.localtest: Provisioning with 'local-exec'...
null_resource.localtest (local-exec): Executing: ["pwsh" "-Command" "New-Item -Name test"]


null_resource.localtest (local-exec):     Directory: C:\Users\pearcec\projects\test

null_resource.localtest (local-exec): Mode                 LastWriteTime         Length Name
null_resource.localtest (local-exec): ----                 -------------         ------ ----
null_resource.localtest (local-exec): -a---           7/22/2020  3:32 PM              0 test

null_resource.localtest: Creation complete after 0s [id=5060708809844859353]

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

~\projects\test ⚡                                                                                                                             ◷ 3:32:22 PM
▶ dir test
-a---         7/22/2020   3:32 PM        0   test

~\projects\test ⚡                                                                                                                             ◷ 3:32:25 PM
▶