Terraform: mudanças entre as edições
Ir para navegação
Ir para pesquisar
Sem resumo de edição |
(→Links) |
||
(13 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
=DEBUG= | |||
<syntaxhighlight lang=bash line=1> | |||
export TF_LOG=TRACE | |||
export TF_LOG=DEBUG | |||
export TF_LOG=INFO | |||
export TF_LOG=WARN | |||
export TF_LOG=ERROR | |||
</syntaxhighlight> | |||
=IF Statement= | =IF Statement= | ||
I want an module in terraform which is able to execute two different codes. <br> | I want an module in terraform which is able to execute two different codes. <br> | ||
Linha 44: | Linha 53: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Example with Null Resource== | |||
<syntaxhighlight lang=text> | |||
resource "null_resource" "zero" { | |||
count = "${signum(format("%.1s", var.yes) == "y" ? 1 : 0) * signum(format("%.1s", var.no) == "y" ? 1 : 0)}" | |||
provisioner "local-exec" { | |||
command = "echo zero}" | |||
} | |||
} | |||
resource "null_resource" "one" { | |||
count = "${signum(format("%.1s", var.yes) == "y" ? 1 : 0) * signum(format("%.1s", var.yes) == "y" ? 1 : 0)}" | |||
provisioner "local-exec" { | |||
command = "echo one}" | |||
} | |||
} | |||
variable "yes" { | |||
default = "yes" | |||
} | |||
variable "no" { | |||
default = "no" | |||
} | |||
</syntaxhighlight> | |||
Execution: | |||
<syntaxhighlight lang=text highlight=15-18,20> | |||
$ 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.um[0] will be created | |||
+ resource "null_resource" "one" { | |||
+ 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. | |||
</syntaxhighlight> | |||
==AND== | |||
{|class=wikitable align="center" | |||
!foo | |||
!bar | |||
!result | |||
|- | |||
|0 | |||
|0 | |||
|0 | |||
|- | |||
|0 | |||
|1 | |||
|0 | |||
|- | |||
|1 | |||
|1 | |||
|1 | |||
|} | |||
<syntaxhighlight lang=text> | |||
count = "${var.foo * var.bar}" | |||
</syntaxhighlight> | |||
==OR== | |||
{|class=wikitable align="center" | |||
!foo | |||
!bar | |||
!result | |||
|- | |||
|0 | |||
|0 | |||
|0 | |||
|- | |||
|0 | |||
|1 | |||
|1 | |||
|- | |||
|1 | |||
|1 | |||
|1 | |||
|} | |||
<syntaxhighlight lang=text> | |||
count = "${signum(var.foo + var.bar)}" | |||
</syntaxhighlight> | |||
=Links= | |||
*[https://www.terraform.io/docs/configuration-0-11/interpolation.html Interpolation Syntax] | |||
*[https://stackoverflow.com/questions/39479849/is-there-a-way-and-or-conditional-operator-in-terraform Source here] | |||
*[https://www.terraform.io/docs/internals/debugging.html DEBUG] | |||
*[https://www.terraform.io/docs/providers/null/resource.html Null Resource] | |||
*[https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9 Terraform tips & tricks: loops, if-statements, and gotchas] | |||
*[https://www.terraform.io/docs/configuration/resources.html Terraform Resource languages] |
Edição atual tal como às 19h15min de 22 de janeiro de 2020
DEBUG
export TF_LOG=TRACE
export TF_LOG=DEBUG
export TF_LOG=INFO
export TF_LOG=WARN
export TF_LOG=ERROR
IF Statement
I want an module in terraform which is able to execute two different codes.
One with a image from a Publisher and other with a custom image.
Codes above show how to specify both operations in the same module:
module "virtualmachine" {
source = "git@github.com:accountname/git.git///path?ref=xxxxxxxx"
vm_name = "virtualmachine"
location = "East US 2"
vm_image_reference = "no"
image_publisher = "MicrosoftSQLServer"
image_offer = "SQL2016SP1-WS2016"
image_sku = "Enterprise"
image_version = "latest"
vm_size = "Standard_DS11_v2"
vm_disk_type = "Premium_LRS"
vm_data_disks = ["1023"]
}
module "virtualmachine" {
source = "git@github.com:accountname/git.git///path?ref=xxxxxxxx"
vm_name = "virtualmachine"
location = "East US 2"
vm_image_reference = "yes"
vm_image = "/subscriptions/xxxxxxxxxxxxx/resourceGroups/Images/providers/Microsoft.Compute/images/centos_720201230"
vm_size = "Standard_DS11_v2"
vm_disk_type = "Premium_LRS"
vm_data_disks = ["1023"]
}
resource "azurerm_virtual_machine" "image" {
count = "${format("%.1s", var.vm_image_reference) == "y" ? 1 : 0}"
.
.
.
}
resource "azurerm_virtual_machine" "market" {
count = "${format("%.1s", var.vm_image_reference) == "n" ? 1 : 0}"
.
.
.
}
Example with Null Resource
resource "null_resource" "zero" {
count = "${signum(format("%.1s", var.yes) == "y" ? 1 : 0) * signum(format("%.1s", var.no) == "y" ? 1 : 0)}"
provisioner "local-exec" {
command = "echo zero}"
}
}
resource "null_resource" "one" {
count = "${signum(format("%.1s", var.yes) == "y" ? 1 : 0) * signum(format("%.1s", var.yes) == "y" ? 1 : 0)}"
provisioner "local-exec" {
command = "echo one}"
}
}
variable "yes" {
default = "yes"
}
variable "no" {
default = "no"
}
Execution:
$ 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.um[0] will be created
+ resource "null_resource" "one" {
+ 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.
AND
foo | bar | result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
count = "${var.foo * var.bar}"
OR
foo | bar | result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 1 | 1 |
count = "${signum(var.foo + var.bar)}"