Skip to content

Instantly share code, notes, and snippets.

@WhatsARanjit
Created November 6, 2023 13:22
Show Gist options
  • Save WhatsARanjit/6577c98e9bfc81a667f0f2d77efbe124 to your computer and use it in GitHub Desktop.
Save WhatsARanjit/6577c98e9bfc81a667f0f2d77efbe124 to your computer and use it in GitHub Desktop.
TF conditional provisioner
variable "switch" {
default = true
}
locals {
prov_cmd = var.switch ? "echo 'YES'" : "echo 'NO'"
}
resource "null_resource" "test" {
provisioner "local-exec" {
command = local.prov_cmd
}
}
$ TF_VAR_switch=false terraform apply --auto-approve
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# null_resource.test will be created
+ resource "null_resource" "test" {
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
null_resource.test: Creating...
null_resource.test: Provisioning with 'local-exec'...
null_resource.test (local-exec): Executing: ["/bin/sh" "-c" "echo 'NO'"]
null_resource.test (local-exec): NO
null_resource.test: Creation complete after 0s [id=8525017274064731153]
$ terraform apply --auto-approve
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# null_resource.test will be created
+ resource "null_resource" "test" {
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
null_resource.test: Creating...
null_resource.test: Provisioning with 'local-exec'...
null_resource.test (local-exec): Executing: ["/bin/sh" "-c" "echo 'YES'"]
null_resource.test (local-exec): YES
null_resource.test: Creation complete after 0s [id=1591540309584719601]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment