Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

terraform dynamic block to create vmware disk

I have the following resource code excerpt:

resource "vsphere_virtual_machine" "standalone" {
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id

  for_each = var.virtual_machines
  name       = each.value.name
  memory     = each.value.ram
  num_cpus   = each.value.logical_cpu
  guest_id   = "ubuntu64Guest"

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
  }

  disk {
    unit_number      = 0
    label            = "OS"
    size             = each.value.disk0_size
    eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
  }

  dynamic "disk" {
    for_each each.value.worker_node ? [1] : []
    content {
      unit_number      = 1
      label            = "PX"
      size             = each.value.disk1_size
      eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
      thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
    }
  }

If each.value.worker_node is true I want to create a second disk for the VM, however when I run terraform plan, I get:

Error: Invalid block definition

  on main.tf line 68, in resource "vsphere_virtual_machine" "standalone":
  68:     for_each each.value.worker_node ? [1] : []

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

Its obvious that there is something wrong in my code, by I cannot fathom what it is.

question from:https://stackoverflow.com/questions/65860264/terraform-dynamic-block-to-create-vmware-disk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Trawling the internet has revealed that I was missing an '=' after the for_each, this is the fix:

resource "vsphere_virtual_machine" "standalone" {
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id

  for_each = var.virtual_machines
  name       = each.value.name
  memory     = each.value.ram
  num_cpus   = each.value.logical_cpu
  guest_id   = "ubuntu64Guest"

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
  }

  disk {
    unit_number      = 0
    label            = "OS"
    size             = each.value.disk0_size
    eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
  }

  dynamic "disk" {
    for_each = each.value.worker_node ? [1] : []
    content {
      unit_number      = 1
      label            = "PX"
      size             = each.value.disk1_size
      eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
      thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...