KVMでterraformを用いてVMの詳細設定を行う

## 目的
[前回](https://qiita.com/engishoma/items/cefe059900adc199e676)の記事では、KVMをterraformから操作する事を入門した。今回はVMをプロヴィジョニングの詳細を詰めていこうと思う。

## 要件
以下の要件に沿った様なHCLを記述する
1. libvirt-providerを用いる
1. cloud-imageを用いる
1. cloud-initを用いる
    1. ネットワークの設定
    1. ユーザーの設定 
1. VNCの設定 


## TL;DL
```tf
provider "libvirt" {
  uri = "qemu+ssh://shoma@172.24.20.3/system"
}

resource "libvirt_volume" "os-image" {
  count = 5
  name   = "${format("terraform-vm%02d.qcow2", count.index + 1)}"
#  source = "/home/shoma/terraform/ubuntu1new.qcow2"
  source = "https://cloud-images.ubuntu.com/releases/23.04/release-20230714/ubuntu-23.04-server-cloudimg-amd64.img"
  format = "qcow2"
}


resource "libvirt_cloudinit_disk" "cloudinit_terraform-vm" {
  count = 5
  name = "${format("terraform-vm%02d.iso", count.index + 1)}"
  pool = "default"

  user_data = <<EOF
    #cloud-config
    hostname: "${format("terraform-vm%02d.qcow2", count.index + 1)}"
    user: tmcit
    password: tmcit
    chpasswd: { expire: False }
    ssh_pwauth: True
    EOF

  network_config = <<EOF
    version: 2
    ethernets:
      ens3:
        addresses:
          - "${format("172.24.20.20%d/24", count.index + 1)}"
        gateway4: 172.24.20.254
        nameservers:
          addresses: [172.24.2.51]
    EOF
}



resource "libvirt_domain" "terraform-vm" {
  count = 5
  name = "${format("ubuntu-terraform%02d", count.index + 1)}"
  memory = 2048
  vcpu = 2

  disk { volume_id = libvirt_volume.os-image[count.index].id }

  cloudinit = libvirt_cloudinit_disk.cloudinit_terraform-vm[count.index].id

  cpu {
    mode = "host-passthrough"
  }

  network_interface {
    bridge = "bridge1"
  }

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

  graphics {
    type = "vnc"
    listen_type = "address"
    autoport = "true"
  }
}
```



## VNC接続をする
```main.tf
  graphics {
    type = "vnc"
    listen_type = "address"
    autoport = "true"
  }
}
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2738475/21a7ae1f-9bf1-a6a6-79c6-d5b2b779a8b1.png)

## Cloud-initの使用
```tf
resource "libvirt_cloudinit_disk" "cloudinit_terraform-vm" {
  count = 5
  name = "${format("terraform-vm%02d.iso", count.index + 1)}"
  pool = "default"

  user_data = <<EOF
    #cloud-config
    hostname: "${format("terraform-vm%02d.qcow2", count.index + 1)}"
    user: tmcit
    password: tmcit
    chpasswd: { expire: False }
    ssh_pwauth: True
    EOF

  network_config = <<EOF
    version: 2
    ethernets:
      ens3:
        addresses:
          - "${format("172.24.20.20%d/24", count.index + 1)}"
        gateway4: 172.24.20.254
        nameservers:
          addresses: [172.24.2.51]
    EOF
}
```
## VMのスペックの指定
```tf
resource "libvirt_domain" "terraform-vm" {
  count = 5
  name = "${format("ubuntu-terraform%02d", count.index + 1)}"
  memory = 2048
  vcpu = 2

  disk { volume_id = libvirt_volume.os-image[count.index].id }

  cloudinit = libvirt_cloudinit_disk.cloudinit_terraform-vm[count.index].id

  cpu {
    mode = "host-passthrough"
  }

  network_interface {
    bridge = "bridge1"
  }

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

  graphics {
    type = "vnc"
    listen_type = "address"
    autoport = "true"
  }
}
```

## 実行すると
以下のIPアドレスを持ったVMが5個払いだされる。
memory: 2048
vcpu: 2

- 172.24.20.201
- 172.24.20.202
- 172.24.20.203
- 172.24.20.204
- 172.24.20.205