通过 Terraform 自动在 KVM 上部署一台机器(带有 qcow2 镜像)

deploy a machine (with qcow2 image) on KVM automatically via Terraform

我是 Terraform 的新手,我正在尝试通过 Terraform 在 KVM 上自动部署一台机器(带有 qcow2 映像)。 我找到了这个 tf 文件:

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

#provider "libvirt" {
#  alias = "server2"
#  uri   = "qemu+ssh://root@192.168.100.10/system"
#}

resource "libvirt_volume" "centos7-qcow2" {
  name = "centos7.qcow2"
  pool = "default"
  source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2"
  #source = "./CentOS-7-x86_64-GenericCloud.qcow2"
  format = "qcow2"
}

# Define KVM domain to create
resource "libvirt_domain" "db1" {
  name   = "db1"
  memory = "1024"
  vcpu   = 1

  network_interface {
    network_name = "default"
  }

  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
  }
}

我的问题是:

  1. (source) 我的 qcow 文件的路径必须在我的计算机上本地?
  2. 我有一个 KVM 机器 ip,我通过它的 ip 远程连接了它。我应该把这个 ip 放在这个 tf 文件的什么地方?
  3. 当我手动完成时,我 运行“virt manager”,我需要在这里写在任何地方吗?

非常感谢。

  1. 没有。也可以是https
  2. 您是指将创建 VM 的 KVM 主机吗?然后您需要在该主机上配置远程 kvm 访问,并在 provider 块的 uri 部分中您需要写入其 ip。
    uri = "qemu+ssh://username@IP_OF_HOST/system"
  3. 使用 terraform 时不需要 virt-manager。您应该使用 terraform 资源来管理 VM。

https://registry.terraform.io/providers/dmacvicar/libvirt/latest/docs

https://github.com/dmacvicar/terraform-provider-libvirt/tree/main/examples/v0.13