先日、TerraformをAlmaLinuxにインストールして使ってみた。
Terraformは主にAWSやAzureといったクラウドの設定をする際に利用されるツールという印象が強いが、オンプレミスのvSphere環境などでも各種設定を行うことができる。
本記事では、TerraformでオンプレESXiに仮想マシンを作成・削除する手順を作る方法を記載する。
環境
環境は以下の通り。
- ESXi : 7.0 Update 3
- OS : AlmaLinux 9.2
- Terraform : v1.5.2
以下URLの手順にてTerraformがインストール済みであることを前提とする。
Terraformによる仮想マシン作成手順
通常のTerraformと同じく、tfファイルを作ってterraform init
→terraform apply
で仮想マシンを作成し、terraform destory
で仮想マシンが削除されることを確認する。
1. tfファイルを作成
TerraformでESXiを操作する場合は、「Terraform Provider for VMware vSphere」を用いる。
マニュアルに使用例は書いてあるが、vCenter Serverが存在する環境の実行例がとなるので、ESXiに対して実行する際の注意点を以下に記載する。
vsphere_datacenter
はha-datacenter
を指定vsphere_compute_cluster
は不要。ただし、vsphere_virtual_machine
のリソース指定において、resource_pool_id
は必須項目なので、vsphere_resource_pool
を空で作って作成しておくguest_id
は以下URLから[Enumerated Types]→[VirtualMachineGuestOsIdentifier]にアクセスし作成するOSの種類に合わせて正しく指定する。- 仮想マシン作成後にTerraformが応答待ちになってしまうことから、
wait_for_guest_net_timeout = -1
を設定する
上記をもとに作成したtfファイルは以下の通り。
main.tf
terraform {
required_version = ">= 1.2.0"
}
locals {
esxi_user = "[ESXiのユーザ名]"
esxi_password = "[ESXiのユーザのパスワード]"
esxi_server = "[ESXiのIPアドレス]"
}
provider "vsphere" {
user = local.esxi_user
password = local.esxi_password
vsphere_server = local.esxi_server
allow_unverified_ssl = true
}
data "vsphere_datacenter" "datacenter" {
name = "ha-datacenter"
}
data "vsphere_datastore" "datastore" {
name = "ssd_01"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "media" {
name = "nfs_01"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_resource_pool" "pool" {
}
data "vsphere_network" "network" {
name = "Network_01"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
resource "vsphere_virtual_machine" "vm" {
name = "terraform-testvm"
resource_pool_id = data.vsphere_resource_pool.pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 1
memory = 1024
guest_id = "other5xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
disk {
label = "disk0"
size = 16
thin_provisioned = true
}
cdrom {
datastore_id = data.vsphere_datastore.media.id
path = "/04_ISO/Linux/AlmaLinux/AlmaLinux-9.2-x86_64-dvd.iso"
}
wait_for_guest_net_timeout = -1
}
2. Terraform実行
上記tfファイルをesxi_vm_linux
ディレクトリに作成し、Terraformを実行する。
# cd esxi_vm_linux
# terraform init
~(省略)~
# terraform apply
data.vsphere_datacenter.datacenter: Reading...
data.vsphere_resource_pool.pool: Reading...
data.vsphere_datacenter.datacenter: Read complete after 0s [id=ha-datacenter]
data.vsphere_datastore.media: Reading...
data.vsphere_datastore.datastore: Reading...
data.vsphere_network.network: Reading...
data.vsphere_datastore.media: Read complete after 0s [id=192.168.1.1:/nfs]
data.vsphere_resource_pool.pool: Read complete after 0s [id=ha-root-pool]
data.vsphere_datastore.datastore: Read complete after 0s [id=63942c98-19e3f477-d004-90e2ba3d67d0]
data.vsphere_network.network: Read complete after 0s [id=HaNetwork-Network_01]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# vsphere_virtual_machine.vm will be created
+ resource "vsphere_virtual_machine" "vm" {
+ annotation = (known after apply)
+ boot_retry_delay = 10000
+ change_version = (known after apply)
+ cpu_limit = -1
+ cpu_share_count = (known after apply)
+ cpu_share_level = "normal"
+ datastore_id = "63942c98-19e3f477-d004-90e2ba3d67d0"
+ default_ip_address = (known after apply)
+ ept_rvi_mode = "automatic"
+ extra_config_reboot_required = true
+ firmware = "bios"
+ force_power_off = true
+ guest_id = "other5xLinux64Guest"
+ guest_ip_addresses = (known after apply)
+ hardware_version = (known after apply)
+ host_system_id = (known after apply)
+ hv_mode = "hvAuto"
+ id = (known after apply)
+ ide_controller_count = 2
+ imported = (known after apply)
+ latency_sensitivity = "normal"
+ memory = 1024
+ memory_limit = -1
+ memory_share_count = (known after apply)
+ memory_share_level = "normal"
+ migrate_wait_timeout = 30
+ moid = (known after apply)
+ name = "terraform-testvm"
+ num_cores_per_socket = 1
+ num_cpus = 1
+ power_state = (known after apply)
+ poweron_timeout = 300
+ reboot_required = (known after apply)
+ resource_pool_id = "ha-root-pool"
+ run_tools_scripts_after_power_on = true
+ run_tools_scripts_after_resume = true
+ run_tools_scripts_before_guest_shutdown = true
+ run_tools_scripts_before_guest_standby = true
+ sata_controller_count = 0
+ scsi_bus_sharing = "noSharing"
+ scsi_controller_count = 1
+ scsi_type = "pvscsi"
+ shutdown_wait_timeout = 3
+ storage_policy_id = (known after apply)
+ swap_placement_policy = "inherit"
+ tools_upgrade_policy = "manual"
+ uuid = (known after apply)
+ vapp_transport = (known after apply)
+ vmware_tools_status = (known after apply)
+ vmx_path = (known after apply)
+ wait_for_guest_ip_timeout = 0
+ wait_for_guest_net_routable = true
+ wait_for_guest_net_timeout = -1
+ cdrom {
+ datastore_id = "192.168.1.1:/nfs"
+ device_address = (known after apply)
+ key = (known after apply)
+ path = "/04_ISO/Linux/AlmaLinux/AlmaLinux-9.2-x86_64-dvd.iso"
}
+ disk {
+ attach = false
+ controller_type = "scsi"
+ datastore_id = "<computed>"
+ device_address = (known after apply)
+ disk_mode = "persistent"
+ disk_sharing = "sharingNone"
+ eagerly_scrub = false
+ io_limit = -1
+ io_reservation = 0
+ io_share_count = 0
+ io_share_level = "normal"
+ keep_on_remove = false
+ key = 0
+ label = "disk0"
+ path = (known after apply)
+ size = 16
+ storage_policy_id = (known after apply)
+ thin_provisioned = true
+ unit_number = 0
+ uuid = (known after apply)
+ write_through = false
}
+ network_interface {
+ adapter_type = "vmxnet3"
+ bandwidth_limit = -1
+ bandwidth_reservation = 0
+ bandwidth_share_count = (known after apply)
+ bandwidth_share_level = "normal"
+ device_address = (known after apply)
+ key = (known after apply)
+ mac_address = (known after apply)
+ network_id = "HaNetwork-Network_01"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
vsphere_virtual_machine.vm: Creating...
vsphere_virtual_machine.vm: Creation complete after 1s [id=564de646-7f10-7d49-33d3-7c55f22ef581]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
作成した結果をVMware Host Clientにて確認してみると、問題なくterraform-testvm
の仮想マシンが作成され、起動まで実行されていることがわかる。
「設定の編集」を確認すると、USBコントローラなどがない、必要最低限の構成で仮想マシンが作成されていた。
Terraformによる仮想マシン削除手順
最後に作成した仮想マシンを削除してみよう。仮想マシンの削除はterraform destroy
をすればよい。
# terraform destroy
data.vsphere_resource_pool.pool: Reading...
data.vsphere_datacenter.datacenter: Reading...
data.vsphere_datacenter.datacenter: Read complete after 0s [id=ha-datacenter]
data.vsphere_network.network: Reading...
data.vsphere_datastore.media: Reading...
data.vsphere_datastore.datastore: Reading...
data.vsphere_datastore.datastore: Read complete after 0s [id=63942c98-19e3f477-d004-90e2ba3d67d0]
data.vsphere_datastore.media: Read complete after 0s [id=192.168.1.1:/nfs]
data.vsphere_resource_pool.pool: Read complete after 0s [id=ha-root-pool]
data.vsphere_network.network: Read complete after 0s [id=HaNetwork-Network_01]
vsphere_virtual_machine.vm: Refreshing state... [id=564de646-7f10-7d49-33d3-7c55f22ef581]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# vsphere_virtual_machine.vm will be destroyed
- resource "vsphere_virtual_machine" "vm" {
- boot_delay = 0 -> null
- boot_retry_delay = 10000 -> null
- boot_retry_enabled = false -> null
- change_version = "2023-08-11T09:27:30.121451Z" -> null
- cpu_hot_add_enabled = false -> null
- cpu_hot_remove_enabled = false -> null
- cpu_limit = -1 -> null
- cpu_performance_counters_enabled = false -> null
- cpu_reservation = 0 -> null
- cpu_share_count = 1000 -> null
- cpu_share_level = "normal" -> null
- datastore_id = "63942c98-19e3f477-d004-90e2ba3d67d0" -> null
- efi_secure_boot_enabled = false -> null
- enable_disk_uuid = false -> null
- enable_logging = false -> null
- ept_rvi_mode = "automatic" -> null
- extra_config = {} -> null
- extra_config_reboot_required = true -> null
- firmware = "bios" -> null
- force_power_off = true -> null
- guest_id = "other5xLinux64Guest" -> null
- guest_ip_addresses = [] -> null
- hardware_version = 19 -> null
- host_system_id = "ha-host" -> null
- hv_mode = "hvAuto" -> null
- id = "564de646-7f10-7d49-33d3-7c55f22ef581" -> null
- ide_controller_count = 2 -> null
- latency_sensitivity = "normal" -> null
- memory = 1024 -> null
- memory_hot_add_enabled = false -> null
- memory_limit = -1 -> null
- memory_reservation = 0 -> null
- memory_share_count = 10240 -> null
- memory_share_level = "normal" -> null
- migrate_wait_timeout = 30 -> null
- moid = "59" -> null
- name = "terraform-testvm" -> null
- nested_hv_enabled = false -> null
- num_cores_per_socket = 1 -> null
- num_cpus = 1 -> null
- pci_device_id = [] -> null
- power_state = "on" -> null
- poweron_timeout = 300 -> null
- reboot_required = false -> null
- resource_pool_id = "ha-root-pool" -> null
- run_tools_scripts_after_power_on = true -> null
- run_tools_scripts_after_resume = true -> null
- run_tools_scripts_before_guest_reboot = false -> null
- run_tools_scripts_before_guest_shutdown = true -> null
- run_tools_scripts_before_guest_standby = true -> null
- sata_controller_count = 0 -> null
- scsi_bus_sharing = "noSharing" -> null
- scsi_controller_count = 1 -> null
- scsi_type = "pvscsi" -> null
- shutdown_wait_timeout = 3 -> null
- swap_placement_policy = "inherit" -> null
- sync_time_with_host = false -> null
- sync_time_with_host_periodically = false -> null
- tools_upgrade_policy = "manual" -> null
- uuid = "564de646-7f10-7d49-33d3-7c55f22ef581" -> null
- vapp_transport = [] -> null
- vbs_enabled = false -> null
- vmware_tools_status = "guestToolsNotRunning" -> null
- vmx_path = "terraform-testvm/terraform-testvm.vmx" -> null
- vvtd_enabled = false -> null
- wait_for_guest_ip_timeout = 0 -> null
- wait_for_guest_net_routable = true -> null
- wait_for_guest_net_timeout = -1 -> null
- cdrom {
- client_device = false -> null
- datastore_id = "192.168.1.1:/nfs" -> null
- device_address = "ide:0:0" -> null
- key = 3000 -> null
- path = "04_ISO/Linux/AlmaLinux/AlmaLinux-9.2-x86_64-dvd.iso" -> null
}
- disk {
- attach = false -> null
- controller_type = "scsi" -> null
- datastore_id = "63942c98-19e3f477-d004-90e2ba3d67d0" -> null
- device_address = "scsi:0:0" -> null
- disk_mode = "persistent" -> null
- disk_sharing = "sharingNone" -> null
- eagerly_scrub = false -> null
- io_limit = -1 -> null
- io_reservation = 0 -> null
- io_share_count = 1000 -> null
- io_share_level = "normal" -> null
- keep_on_remove = false -> null
- key = 2000 -> null
- label = "disk0" -> null
- path = "terraform-testvm/terraform-testvm.vmdk" -> null
- size = 16 -> null
- thin_provisioned = true -> null
- unit_number = 0 -> null
- uuid = "6000C29e-8a40-b94b-d566-a61c53225832" -> null
- write_through = false -> null
}
- network_interface {
- adapter_type = "vmxnet3" -> null
- bandwidth_limit = -1 -> null
- bandwidth_reservation = 0 -> null
- bandwidth_share_count = 50 -> null
- bandwidth_share_level = "normal" -> null
- device_address = "pci:0:7" -> null
- key = 4000 -> null
- mac_address = "00:0c:29:2e:f5:81" -> null
- network_id = "HaNetwork-Network_01" -> null
- use_static_mac = false -> null
}
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
vsphere_virtual_machine.vm: Destroying... [id=564de646-7f10-7d49-33d3-7c55f22ef581]
vsphere_virtual_machine.vm: Destruction complete after 1s
Destroy complete! Resources: 1 destroyed.
ESXiの「最近のタスク」においても、仮想マシンの電源をOFFしてから削除処理が実行されていることがわかる。
以上で、TerraformでオンプレESXiに仮想マシンを作成・削除する手順は完了となる。
0 件のコメント:
コメントを投稿