【问题标题】:How do you detect if Terraform script is running in plan or apply mode?您如何检测 Terraform 脚本是否在计划或应用模式下运行?
【发布时间】:2019-10-14 12:53:59
【问题描述】:

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

【问题讨论】:

  • 我不太明白你的问题。运行terraform plan 时它处于计划模式,运行terraform apply 时它处于应用模式。这里有什么问题吗?
  • local-exec 供应商不会在计划模式下运行,所以这不是问题。

标签: terraform terraform-provider-azure


【解决方案1】:

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
▶

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 2019-03-21
    • 2015-11-26
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多