如何检测 Terraform 脚本是否在计划或应用模式下运行


使用 Terraform

local-exec provisioner 运行 powershell 脚本以在 Azure 中创建 ILB ASE(Terraform Azure 提供程序尚未直接支持 ILB ASE,因此我们在 Powershell 中手动执行此操作(。 我们不希望在本地执行程序以计划模式运行时创建 ILB ASE,因为此操作需要 2 小时。 本地执行是否有办法检测地形脚本是否在计划或应用模式下运行?

预配程序在计划阶段不运行。

~projectstest ⚡                                                                                                                             ◷ 3:31:16 PM
▶ cat .main.tf
resource "null_resource" "localtest" {
  provisioner "local-exec" {
    command = "New-Item -Name test"
    interpreter = ["pwsh", "-Command"]
  }
}
~projectstest ⚡                                                                                                                             ◷ 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.
~projectstest ⚡                                                                                                                             ◷ 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.

~projectstest ⚡                                                                                                                             ◷ 3:31:32 PM
▶ dir test
Get-ChildItem: Cannot find path 'C:Userspearcecprojectstesttest' because it does not exist.
~projectstest ⚡ ⨯                                                                                                                           ◷ 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:Userspearcecprojectstest
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.
~projectstest ⚡                                                                                                                             ◷ 3:32:22 PM
▶ dir test
-a---         7/22/2020   3:32 PM        0   test
~projectstest ⚡                                                                                                                             ◷ 3:32:25 PM
▶

最新更新