我们正在尝试修改我们自己没有构建的 Terraform 基础设施。当前代码确实支持多个磁盘,但只支持一个 NIC。我们尝试相应地修改 NIC 部分,但失败了。不幸的是,我们没有保存我们尝试的代码或错误消息。我希望有人能告诉我这些更改必须是什么。
这就是我们所拥有的:
相关代码片段
module "tf_file_1_module" {
[...]
vm_ipnet = "1.2.3."
vm_ipstart = "4"
network = "staging"
[...]
}
应改为
module "tf_file_1_module" {
[...]
vm_ipnet = ["5.6.7", "1.2.3"]
vm_ipstart = ["8", "10"]
network = ["foo-network", "bar-network"]
[...]
}
对于主磁盘,我们在模块中没有明确的配置。附加磁盘使用可选参数配置
thin_provisioned = ["true", "true"]
data_disk_size_gb = ["100", "5"]
这些由以下代码处理:
dynamic "disk" {
for_each = var.data_disk_size_gb
content {
label = "disk${disk.key + 1}"
size = var.data_disk_size_gb[disk.key]
unit_number = disk.key + 1
thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[disk.key] : true
eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[disk.key] : false
}
}
这就是我们尝试为 NIC 重现的内容。
(或多或少)完整的代码
我们在 Terraform 工作流中定义了以下结构:
├── config
│ ├── backend.conf
│ └── backend.tfvars
├── tf_file_1.tf
在 .tf 文件中我们有这样的定义:
module "tf_file_1_module" {
source = "[email protected]?ref=master"
datacenter = "DC"
cluster = "Linux-Cluster"
network = "staging"
datastore_cluster = "Netapp"
vm_template = "packer-template"
vm_setname = "tf_file_1"
vm_setcount = 1
vm_folder = "Linuxhosts/staging"
vm_cpu = 2
cpu_hot_add_enabled = "true"
cpu_hot_remove_enabled = "true"
vm_memory = 4096
memory_hot_add_enabled = "true"
vm_domain = "my.fancy.domain"
vm_ipnet = "1.2.3."
vm_ipstart = "4"
logging_enabled = "true"
anti_affinity = "false"
}
在内部映射network
到 vCenter 中的名称,因此我们不必输入没人能记住的长网络名称。
因此,每个文件都使用自己的模块,该模块源自 Git 存储库,然后根据需要进行修改。结构如下所示:
.
├── main.tf
├── output.tf
├── provider.tf
├── README.md
├── variables.tf
└── versions.tf
模块内部main.tf
有一些定义,其中包括网络的定义:
data "vsphere_network" "network" {
name = "/${var.datacenter}/network/${var.network_map[var.network]}"
datacenter_id = data.vsphere_datacenter.dc.id
}
(→ Here you can also see the mapping for the network name)
resource "vsphere_virtual_machine" "vm" {
count = var.vm_setcount
name = "${var.vm_setname}${format("%02d", count.index + 1)}${var.append_fqdn == true ? ".my.fancy.domain" : ""}"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_cluster_id = data.vsphere_datastore_cluster.datastore_cluster.id
annotation = "Deployed via Terraform"
num_cpus = var.vm_cpu
cpu_hot_add_enabled = var.cpu_hot_add_enabled
cpu_hot_remove_enabled = var.cpu_hot_remove_enabled
memory = var.vm_memory
memory_hot_add_enabled = var.memory_hot_add_enabled
folder = var.vm_folder
guest_id = data.vsphere_virtual_machine.small.guest_id
scsi_type = data.vsphere_virtual_machine.small.scsi_type
enable_logging = var.logging_enabled
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.small.network_interface_types[0]
}
clone {
template_uuid = data.vsphere_virtual_machine.small.id
customize {
linux_options {
host_name = "${var.vm_setname}${format("%02d", count.index + 1)}"
domain = var.vm_domain
}
network_interface {
ipv4_address = "${var.vm_ipnet}.${count.index + var.vm_ipstart}"
ipv4_netmask = 24
}
ipv4_gateway = "${var.vm_ipnet}.1"
dns_server_list = ["8.8.8.8"]
dns_suffix_list = ["my.fancy.domain"]
}
}
}
重要变量定义如下:
variable "network" {
description = "Network in which the VM will be created."
type = string
}
variable "vm_ipnet" {
description = "Host VM IP address network part prefix."
type = string
}
variable "vm_ipstart" {
description = "VM host part IP address configuration/start address. Will automatially increase when more than one vm are to be created."
type = string
}