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

如何检测Terraform脚本是以计划模式还是应用模式运行?,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,使用Terraform local exec provisioner运行powershell脚本在Azure中创建ILB ASE(在Terraform Azure provider中还不直接支持ILB ASE,所以我们在powershell中手动执行)。我们不希望在本地exec以计划模式运行时创建ILB ASE,因为此操作需要2小时。本地exec是否有办法检测terraform脚本是否在计划或应用模式下运行?Provisioner在计划阶段不运行 ~\projects\test ⚡

使用Terraform local exec provisioner运行powershell脚本在Azure中创建ILB ASE(在Terraform Azure provider中还不直接支持ILB ASE,所以我们在powershell中手动执行)。我们不希望在本地exec以计划模式运行时创建ILB ASE,因为此操作需要2小时。本地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
▶

我不太明白你的问题。当您运行
terraform plan
时,它处于计划模式;当您运行
terraform apply
时,它处于应用模式。这里有问题吗?
local exec
provisioner不会在计划模式下运行,因此这不是问题。