Lets have a discussion:
I'm trying to figure out how to optionally assign more than one network to VMs created with Terraform using my module. I created a module for Xen-Orchestra VM creation (vm.tf and variables.tf below) that lets me create any number of VMs (with a set number of network connections, currently just 1) by just defining the required variables in a VM definition terraform file.
I'm trying to test air gap systems so I need to have an internal private network as well as the the primary LAN network.
I'd like to keep everything as much in the module as possible and continue to use variables only in the Terraform VM definition. I would like to share this module at some point in the future and allowing multiple networks would be nice.
If possible, I would like to have the ability to specify 1 or more networks and use count or a foreach loop on a variable declared in the module's variables file and populated by the variable definition in the VM definition. I can't figure out how to only define the specified networks to attach.
**As I'm writing this I thought of maybe just defining a list of network names and use count to create a data object for each one entered. Then I guess a network block can run through a foreach loop over the network name data object. Would that work?
I may have overlooked a simple solution or I am just overthinking this.
Any ideas? Is anyone using a VM creation module like this and defining more than one network?
VM definition:
module "xo_lab_vm"{
source = "../modules/xenorchestra-lab-vm"
pool_name = "xcp_pool_1"
vm_template_name = "01-rhel-9_1-noswap-tpl-NAS"
sr_name = "Local storage"
network_name = "Pool-wide network associated with eth1"
vm_count = 1
vm_name = "bastion"
vm_domain = "localtest.lan"
vm_hostname = "bastion"
vm_username = "user"
ip_wait = true
memory_max = 4294967296
num_cpus = 2
if_mac = ["2e:cd:84:b5:15:fe"]
vdisk_info = {
name = "root01"
size = 10737418240
}
}
Module:
# vm.tf
data "xenorchestra_pool" "pool" {
name_label = "${var.pool_name}"
}
data "xenorchestra_template" "vm_template" {
name_label = "${var.vm_template_name}"
}
data "xenorchestra_sr" "sr" {
name_label = "${var.sr_name}"
pool_id = data.xenorchestra_pool.pool.id
}
data "xenorchestra_network" "network" {
name_label = "${var.network_name}"
pool_id = data.xenorchestra_pool.pool.id
}
resource "xenorchestra_vm" "vm" {
count = "${var.vm_count}"
memory_max = "${var.memory_max}"
cpus = "${var.num_cpus}"
name_label = "${var.vm_name}${count.index}"
template = data.xenorchestra_template.vm_template.id
affinity_host = data.xenorchestra_pool.pool.master
wait_for_ip = "${var.ip_wait}"
network {
network_id = data.xenorchestra_network.network.id
mac_address = "${var.if_mac[count.index]}"
}
disk {
sr_id = data.xenorchestra_sr.sr.id
name_label = "${var.vm_name}${count.index}-${var.vdisk_info.name}"
size = "${var.vdisk_info.size}"
}
timeouts {
create = "10m"
}
}
# Input variable definitiions.
# Variables for data sources
variable "pool_name" {
description = "Name of the Xen Pool to create VM in."
type = string
}
variable "vm_template_name" {
description = "Name of template. Must already exist in pool."
type = string
}
variable "sr_name" {
description = "Name of SR for vdisk."
type = string
}
variable "network_name" {
description = "Name of network for vif."
type = string
}
# Variables for VM
variable "vm_count" {
description = "Number of VMs to create."
type = number
default = 1
}
variable "vm_name" {
description = "Name of VM."
type = string
}
variable "memory_max" {
description = "Maximum memory for VM."
type = number
}
variable "num_cpus" {
description = "Number of vCPUs for VM."
type = number
}
variable "if_mac" {
description = "MAC address to assign to the vif."
type = list(string)
}
variable "ip_wait" {
description = "Should the terraform wait until an IP is assigned to complete creation?"
type = bool
}
variable "vdisk_info" {
description = "Object list containing the name and size of the vdisk."
type = object({
name = string
size = number
})
}
## TODO - utilize these variables via remote-exec and local-exec provisioners
# Variables for OS configuration
variable "vm_hostname" {
description = "Hostname to assign to the VM."
type = string
}
variable "vm_domain" {
description = "Domain name of VM."
type = string
}
variable "vm_username" {
description = "User name to create on the VM."
type = string
}