マニュアルはこちらです
https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs
vSphere 側の準備
Terrafrom を実行する前に、展開する仮想マシンの元になるテンプレートを作成する必要があります。
今回は、仮想マシンを作成してゲストOSに Ubuntu をインストールしたものを用意しました。
仮想マシンの展開にかかる時間を短くするためにリンククローンを使用したいので、スナップショットを1つだけ作成しておきます。
vSphere の構成としては以下のような感じで初期状態です。
tfファイルの作成。
1.Terrafrom を導入したマシンにログインします。
ここでは Ubuntu を使用しています。
2.作業用のディレクトリを作成します。
3.Terrafrom が 何を使ってどの環境にログインできるようにするための tfファイルを作成します。
1つの tfファイルに全部書いてもいいんですが、このログインに使う情報は別の仮想マシンを作りたいとき
などに使いまわしができたほうが楽なので私はファイルを分けています。
このファイルに記載する内容は以下のようになります。
vSphere-login.tf
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "~> 2.5"
}
}
}
provider "vsphere" {
user = "administrator@vsphere.local"
password = "Vmware1!Vmware1!"
vsphere_server = "vc01.snt-demo.lab"
allow_unverified_ssl = true
}
ここには2つの内容が書かれています。以下の部分は、Terraformが各製品を扱う際に使用するプロバイダーというプラグインを、利用する製品に合わせて指定しています。
今回使う製品が vSphere なので hashicorp/vsphere になっています。
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "~> 2.5"
}
}
}
以下の部分は vCenter へのログイン情報になります。allow_unverified_ssl = true は自己証明書を使っているので、証明書の検証をスキップするために入れています。
provider "vsphere" {
user = "administrator@vsphere.local"
password = "Vmware1!Vmware1!"
vsphere_server = "vc01.snt-demo.lab"
allow_unverified_ssl = true
}
これで、Terrafrom が vSphere の製品を扱うことを理解して、接続先である vCenter とログインするためのアカウントおよびパスワードを知っている状態となります。3.仮想マシン展開用のtfファイルの作成
次に仮想マシンを展開する部分です。
今回は仮想マシン以外のリソースは作成しません。
記載内容は以下になります。
create-test-vm01.tf
#使用するリソース(データセンター、クラスタ、データストア、ポートグループ、テンプレート用マシン)の指定
data "vsphere_datacenter" "dc" {
name = "DC01"
}
data "vsphere_datastore" "datastore" {
name = "nfs01"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_compute_cluster" "cluster" {
name = "CL01"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = "vm-DPortGroup"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_virtual_machine" "template" {
name = "ubuntu-tmp"
datacenter_id = data.vsphere_datacenter.dc.id
}
#作成するリソースの内容
resource "vsphere_virtual_machine" "vm" {
name = "test-vm01"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 2
memory = 2048
guest_id = data.vsphere_virtual_machine.template.guest_id
firmware = data.vsphere_virtual_machine.template.firmware
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
# DHCPがない環境のためIPアドレス設定の確認を無視
wait_for_guest_ip_timeout = 0
wait_for_guest_net_timeout = 0
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = false
io_reservation = 1
thin_provisioned = true
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
linked_clone = true
}
}
クローン元と同じでいい場合は data.vsphere_virtual_machine.template でテンプレートマシンを指定し、guest_id や firmware など参照させたい内容をくっつけるとテンプレートマシンの情報を使ってくれます。
テンプレートマシンとは異なる設定にしたい仮想マシン名やCPUやメモリといった値は直接書き込んで指定しています。
4.terrafromコマンドの実行 作成したtfファイルから仮想マシンの作成を行っていきます。 4-1.tfファイルを作成したディレクトリで初期化処理
以下のコマンドを実行します。
terraform init4-2.tfファイルの確認
以下のコマンドを実行して apply 可能か確認します。
terraform plan4-3.仮想マシンの展開
以下のコマンドを実行して仮想マシンを展開します。
terraform apply実行していいか確認されるので yes を入力します。
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 9s [id=421723e4-5f72-d210-9a4f-d503258be315] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
4-4.作成物の削除
作成したものを削除するときは以下のコマンドで削除します。
terraform destroyこちらも実行していいか確認されるので yes を入力します。
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=421723e4-5f72-d210-9a4f-d503258be315] vsphere_virtual_machine.vm: Still destroying... [id=421723e4-5f72-d210-9a4f-d503258be315, 10s elapsed] vsphere_virtual_machine.vm: Destruction complete after 14s Destroy complete! Resources: 1 destroyed.
これで仮想マシンの作成と削除ができるようになりました。
次回は、作成する仮想マシンのゲストOSをカスタマイズする方法についてやっていきたいと思います。


