简体   繁体   中英

Looping over list(object) using for_each

I am currently trying to provision multiple proxmox VM's using a variable of type list(object) by referring to a template name. For each template that exists provision a VM and create a nat resource to allow SSH connections into it.

Here's what my current terraform looks like

module "instance" {
  source = "./proxmox-instance"

  template_name = "ubuntu-20.04"

  host_port  = "5522"
  guest_port = "22"
}

Here's what the module looks like

########################
#   Proxmox-Instance
########################

resource "proxmox_vm_qemu" "vm" {
  for_each = var.template_name

  # template to clone from
  clone = var.template_name

  # vm target node / naming convention
  name        = var.vm_name
  desc        = "Created by Terraform - ${var.vm_name}"
  target_node = "proxmox7node01"

  # check if the QEMU agent is on > VM > Options > QEMU Guest Agent
  agent = 1

  # display settings
  vga {
    type = "qxl" # qxl = Enables Spice console
  }

  # networking settings
  network {
    bridge = "vmbr0"
    model  = "virtio"
  }

  # cloud-init settings
  os_type    = "cloud-init"
  ipconfig0  = var.ipconfig0
  ciuser     = var.cloud_init_user
  cipassword = var.cloud_init_password
}

########################
#   Port-forwarding
########################

resource "iptables_nat" "dnat-front" {
  # Creates an entry for each template specified
  for_each = var.template.name

  name           = "iptable-${var.vm_name}"
  on_cidr_blocks = ["foo"] # Proxmox network device IP here

  dnat {
    iface    = "enp7s0"
    protocol = "tcp"
    to_port  = var.guest_port                                    # Custom SSH Port for ingress connections
    nat_ip   = "${proxmox_vm_qemu.vm.default_ipv4_address}:${var.host_port}" # VM Internal IP here
  }
}

I am trying to make it loop over each template specified and create an additional instance plus its associated nat entry,

Here's my goal.

module "instance" {
  source = "./proxmox-instance"

  template_name = [
    "ubuntu-20.04",
    "windows-server-2019"
  ]

 port_forwarding = [
  {
  host_port  = "5522"
  guest_port = "80"
  },
  {
  host_port  = "5511"
  guest_port = "443"
  }]
}

I've created a single variable called template_name with the following structure

variable "template_name" {
  description = "Your Template name - Will be cloned from this template"
  type = list(object({
    name       = string
    host_port  = number
    guest_port = number
  }))
}

After trying to apply using the following i've received the following error

Error: Invalid value for input variable
│
│   on main.tf line 9, in module "instance":
│    9:   template_name = ["ubuntu-20.04", "windows-server-2019"]
│
│ The given value is not suitable for module.instance.var.template_name declared at
│ proxmox-instance\variables.tf:6,1-25: element 0: object required.

How will one go by to achieve such a thing?

"gcp_zone": "us-central1-a", "image_name": "centos-cloud/centos-7", "vms": [ { "hostname": "test1-srfe", "cpu": 1, "ram": 4, "hdd": 15, "log_drive": 300, "template": "Template-New", "service_types": [ "sql", "db01", "db02" ] }, { "hostname": "test1-second", "cpu": 1, "ram": 4, "hdd": 15, "template": "APPs-Template", "service_types": [ "configs" ] } ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM