簡體   English   中英

將多個 static IP 添加到 terraform 提供者/資源 vSphere vsphere_virtual_machine

[英]add multiple static IPs to terraform provider/resource vSphere vsphere_virtual_machine

我有 Terraform 代碼可以將 50 個以上的 Ubuntu 虛擬機平均分配到兩個數據中心。 每個 VM 都有自己的 static IP 作為其主要(通過cidrhost設置)並且DHCP 不是一個選項 該 Terraform 代碼運行良好。

現在,我必須為每個 VM 分配一個特定的 40 個 static IP 塊,最好分配給同一個 NIC,我不確定如何處理 go。 第一個塊從 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
}

我找到的一種解決方法是編寫一個腳本來生成 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"
                ]
        }
}

不像我希望的那樣充滿活力,但這可以完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM