将 IP 分配给桥接器上的 kvm guest (terrafom)

将 IP 分配给桥接器上的 kvm guest (terrafom)

我正在尝试通过 KVM 上的 terraform 部署 VM guest。

来宾虚拟机已部署但未获取 IP。

我究竟做错了什么?

这是我的 main.tf :

terraform {
  required_providers {
    libvirt  = {
      source = "dmacvicar/libvirt"
    }
  }
}

provider "libvirt" {
    uri = "qemu:///system"
}

resource "libvirt_volume" "centos7-qcow2" {
    name   = "centos7.qcow2"
    pool   = "default"
    source = "http://......qcow2"
    format = "qcow2"

}

data "template_file" "user_data" {
  template = "${file("${path.module}/cloud_init.cfg")}"
}

data "template_file" "cloudinit_network" {
  template = "${file("${path.module}/network.cfg")}"
  vars = {}
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name         = "commoninit.iso"
  user_data    = "${data.template_file.user_data.rendered}"
  network_config = data.template_file.cloudinit_network.rendered
}


resource "libvirt_domain" "gw2" {
  name   = "gw2"
  memory = "8192"
  vcpu   = 4
  cloudinit = libvirt_cloudinit_disk.commoninit.id
  qemu_agent = true
  
  network_interface {
    addresses = ["10.100.86.201"]
    bridge = "br0"
    #wait_for_lease = true
  }

  boot_device {
   dev = [ "hd", "network"]
  }

  disk {
    volume_id = "${libvirt_volume.centos7-qcow2.id}"
  }

  console {
    type        = "pty"
    target_type = "serial"
    target_port = "0"
  }

  graphics {
    type         = "spice"
    listen_type  = "address"
    autoport     = true
  }
}

output "ips" {
  value = libvirt_domain.gw2.*.network_interface.0.addresses
} 

我的网络.cfg 文件:

version: 2
ethernets:
  eth0:
    dhcp4: true
    dhcp6: false

感谢您的帮助!


我的 terraform 版本: linux_amd64 上的 Terraform v1.0.10

  • 提供商registry.terraform.io/dmacvicar/libvirt v0.6.11
  • 提供商registry.terraform.io/hashicorp/template v2.2.0

答案1

libvirt_network您应该使用下:

mode = "bridge"

network.cfg文件下你应该使用这样的东西:

network:
  version: 1
  config:
    - type: physical
      name: eth0
      subnets:
        - type: static
          address:  10.94.13.27/22
          gateway: 10.94.13.1
          dns_nameservers: [ 10.94.244.11 ]

这意味着cloud init 将设置静态IP。

相关内容