我有Terraform代码可以向上旋转50个Ubuntu VM,它们在两个数据中心之间平均分配。每个VM都有自己的静态IP作为其主要IP地址(通过cidrhost
进行设置), DHCP不可选。 Terraform代码运行良好。
现在,我必须为每个VM(最好是同一个NIC)分配一个包含40个静态IP的特定块,而且我不确定该如何处理。 第一个区块始于10.32.128.64 。
provider "vsphere" {
version = "~> 1.17"
alias = "jdcProvider"
}
### Acceptable values: 1-25.
variable "Counter" {
default = 1
description = "How many load-testing VMs should we create PER DATACENTER?"
}
### 1st datacenter IP range: 10.32.128.12 - .37
### 2nd datacenter IP range: 10.32.128.38 - .63
variable "jdcLastOctet" {
default = 12
description = "10.32.128.x where x is the number above"
}
这是第一个数据中心的资源。第二个数据中心的代码非常相似。
resource "vsphere_virtual_machine" "jdcResource" {
provider = vsphere.jdcProvider
count = var.Counter
name = "loadtst-J-${count.index}"
clone {
customize {
dns_server_list = ["1.1.1.1", "2.2.2.2"]
dns_suffix_list = ["example.com", "example.org"]
ipv4_gateway = "10.32.128.1"
linux_options {
host_name = "loadtst-J-${count.index}"
domain = "example.com"
}
network_interface {
ipv4_address = cidrhost("10.32.128.0/24", var.jdcLastOctet+count.index )
ipv4_netmask = 21
}
}
linked_clone = false
template_uuid = data.vsphere_virtual_machine.JDC.id
}
network_interface {
network_id = data.vsphere_network.JDC.id
adapter_type = data.vsphere_virtual_machine.JDC.network_interface_types[0]
}
wait_for_guest_net_timeout = 1
}
答案 0 :(得分:0)
我遇到的一种解决方法是编写一个脚本以将IP块生成为外部文件,然后使用Terraform provisioner file
将适当的文件发送到适当的服务器。
伪代码
# split 2000 IPs into blocks of 40, into J & W files. ex: J-0, W-0, J-1, W-1, ...
# J files go to one datacenter; W files to the other datacenter.
# starting IP is 10.32.128.64
$jCount=0
$wCount=0
$Octet3=128
$Octet4=64
$IPCount=0
$Filename=""
$BlocksOf=40
$TotalIPCount=2000
while (1) {
for ($Octet4; $Octet4 <= 255; $Octet4++) {
exit if ($IPCount equals $TotalIPCount)
if ($IPCount notEqualTo 0 -and (0 equals $IPCount%(2*$BlocksOf)) ) {
$jCount++
$wCount++
}
if ( ($IPCount/$BlocksOf)%2 ) {
$Filename="W-" + $wCount
}
else {
$Filename="J-" + $jCount
}
open("IP_block_ranges/$Filename", append)
write(filehandle "ip addr add 10.32.$Octet3.$Octet4/21 dev ens192\n" )
close(filehandle)
$IPCount++
}
$Octet3++
$Octet4=0
}
更新的Terraform代码
resource "vsphere_virtual_machine" "jdcResource" {
provider = vsphere.jdcProvider
count = var.Counter
name = "loadtst-J-${count.index}"
clone {
customize {
dns_server_list = ["1.1.1.1", "2.2.2.2"]
dns_suffix_list = ["example.com", "example.org"]
ipv4_gateway = "10.32.128.1"
linux_options {
host_name = "loadtst-J-${count.index}"
domain = "example.com"
}
network_interface {
ipv4_address = cidrhost("10.32.128.0/24", var.jdcLastOctet+count.index )
ipv4_netmask = 21
}
}
linked_clone = false
template_uuid = data.vsphere_virtual_machine.JDC.id
}
network_interface {
network_id = data.vsphere_network.JDC.id
adapter_type = data.vsphere_virtual_machine.JDC.network_interface_types[0]
}
wait_for_guest_net_timeout = 1
provisioner "file" {
source = "IP_block_ranges/J-${count.index}"
destination = "/tmp/extra_IPs"
}
provisioner "remote-exec" {
inline = [
"sudo sh /tmp/extra_IPs",
"sudo rm /tmp/extra_IPs"
]
}
}
不像我希望的那样动态,但这可以完成工作。