If statement error inside of vsphere module

Would like to set a conditional statement where if true it sets the value from a variable. However, receiving the following error and not sure what I am doing wrong. Any suggestions are greatly appreciated.


terraform validate -var-file=variables.tfvars

Warning: The -var and -var-file flags are not used in validate. Setting them has no effect.

These flags will be removed in a future version of Terraform.

Warning: Interpolation-only expressions are deprecated

  on resource.tf line 23, in resource "vsphere_folder" "vm_folder":
  23:   datacenter_id = "${data.vsphere_datacenter.dc.id}"

Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.

Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

(and 4 more similar warnings elsewhere)

Error: Invalid block definition

  on resource.tf line 36, in resource "vsphere_virtual_machine" "vm":
  36:   if var.control {

Either a quoted string block label or an opening brace ("{") is expected here.

Resource configuration

resource "vsphere_virtual_machine" "vm" {
  name             = var.vmname
  folder	   = "${vsphere_folder.vm_folder.path}"
  resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus = var.cpu
  memory   = var.memory
  guest_id = var.guestid

  if var.control {
    ignition_config = var.control_ign
  } else {
    ignition_config = var.compute_ign

  extra_config = {
    "guestinfo.ignition.config.data" = "${var.ignition_config}"
    "guestinfo.ignition.config.data.encoding" = "base64"
    "disk.EnableUUID" = "True"

  network_interface {
    network_id = "${data.vsphere_network.network.id}"
  disk {
    label = "disk0"
    size  = var.disk_size

Hi @canit00,

Terraform uses conditional expressions rather than imperative control structures, so you can express that intent this way:

  ignition_config = var.control ? var.control_ign : var.compute.ign

This is a conditional expression.

One of the other message is also warning you that during validation Terraform checks whether the configuration is valid for any possible value of each input variable, and so it ignores your given variables file and simply verifies that both var.control_ign and var.compute_ign are both of a suitable type to be assigned to the ignition_config argument and that var.control is a boolean value.

You can use terraform plan to see the effect of selecting specific values for those variables, because specified variable values are integrated during planning rather than during validation.

1 Like

@apparentlymart thank you for the prompt response. It is passing validation and will run plan shortly.

I did happen to see the conditional expressions as noted in the URL, but thought that it was replaced with the if statement. Will need to spend more time learning as this is only my third day. Out of curiosity, when would one use an if statement in Terraform, if at all?