/var/log/study

つまり雑記

既存のvSphere環境をTerraform import して管理する

yaaamaaaguuu.hatenablog.com

ほぼ1年越しの再入門。既存のvSphere環境を管理するために色々と試してみる

今回は, フォルダとvDS, データストアをインポートして ~planで差分がなくなるところ~ だるかったのでインポートまで。

terraform v0.11.3 で検証

色々試す前の確認事項

ワークフロー

  • 初回のみ
    • terraform init
  • 以降
    1. tf ファイルに既存の構成を書く
    2. terraform import で既存の構成をインポート
    3. terraform plan で確認
  • 変更があれば
    1. tf ファイルを編集
    2. terraform plan で確認
    3. terraform apply で実行

importなりapplyなりをすると、terraform.tfstateが作成される。tfstateに無いものは新規作成としてみなされるっぽい。

でもって、terraform.tfstateをgitで管理する?とかは以下に書いてあるので、適当なタイミングで調査しようと思う。

www.terraform.io

tfファイルの構成

terraformはカレントディレクトリにある tf ファイルを全て読み込んで実行するとの事。

なので一旦はansibleのようにディレクトリ構成は考えずに取り組んでいく

tf内の用語

  • providor
    • 接続情報とか
  • data
    • たぶん参照系
  • resource
    • たぶん自分で作りたいやつ
  • variable
    • 変数

まず接続確認

providor.tf を作り接続確認

variable "vsphere_user" { 
  default = "administrator@vsphere.local" # デフォルトを入れておくと、コマンド実行時に引数を与えなくて済む
}

variable "vsphere_password" { 
  # なにも定義せずに実行すると該当の変数に何を入れるか実行時に聴いてくれる
  # もちろんコマンドラインの引数で与えてもOK
  # ただしterraform import のときはコマンドライン引数に与えてあげる必要がある
}

variable "vsphere_server" { }

provider "vsphere" {
  user           = "${var.vsphere_user}"
  password       = "${var.vsphere_password}"
  vsphere_server = "${var.vsphere_server}"

  # if you have a self-signed cert
  allow_unverified_ssl = true
}

こんな感じのファイルを作った後に、vSphere providoerをインストールするために以下のコマンドを叩く

$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "vsphere" (1.3.3)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.vsphere: version = "~> 1.3"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

するとversionの出力にvsphereのprovidorが追加される

$ terraform --version
Terraform v0.11.3
+ provider.vsphere v1.3.3

そしたら接続確認の為に terraform plan を実行する

$ terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

データセンターを確認する

vsphere_datacenter がデータとリソースで使える

確認するつもりだったが、datacenterはimportに未対応なのでdataで参照系だけ定義しておく

datacenter.tf で。

data "vsphere_datacenter" "my_dc" {
  name = "mycloud"
}
$ tree
.
├── datacenter.tf
├── providor.tf

VMのフォルダ

現状は以下の感じ。

f:id:yaaamaaaguuu:20180311155302p:plain

administration を管理するようにする

インポートする前に既存の定義を書く

administration.tf とする

resource "vsphere_folder" "administration" {
  path          = "administration"
  type          = "vm"
  datacenter_id = "${data.vsphere_datacenter.my_dc.id}"
}
$ tree
.
├── administration.tf
├── datacenter.tf
├── providor.tf

インポートする

https://www.terraform.io/docs/providers/vsphere/r/folder.html#importing

上記を参考に

$ terraform import -var="vsphere_password=####" vsphere_folder.administration /mycloud/vm/administration
vsphere_folder.administration: Importing from ID "/mycloud/vm/administration"...
vsphere_folder.administration: Import complete!
  Imported vsphere_folder (ID: group-v687)
vsphere_folder.administration: Refreshing state... (ID: group-v687)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

確認する

$ terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.vsphere_datacenter.my_dc: Refreshing state...
vsphere_folder.administration: Refreshing state... (ID: group-v687)

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

ネットワーク

既存のネットワークは以下

f:id:yaaamaaaguuu:20180311173226p:plain

myprivate を管理出来るようにする

インポートする前に定義を書く

まずESXiのデータ定義を書く必要があるので、 hosts.tf で以下を書く

variable "esxi_hosts" {
  default = [
    "esxi1.mycloud.local",
    "esxi2.mycloud.local",
    "esxi3.mycloud.local",
  ]
}

variable "network_interfaces" {
  default = [
    "vmnic0",
    "vusb0"
  ]
}

data "vsphere_host" "host" {
  count         = "${length(var.esxi_hosts)}"
  name          = "${var.esxi_hosts[count.index]}"
  datacenter_id = "${data.vsphere_datacenter.my_dc.id}"
}

2こめのnicvusb0 になっているはIntelのNUCにusb nicをくっつけているから。

yaaamaaaguuu.hatenablog.com

$ tree
.
├── administration.tf
├── datacenter.tf
├── hosts.tf
├── providor.tf

次にネットワークのリソースを書く

network.tf

resource "vsphere_distributed_virtual_switch" "myprivate_dvs" {
  name          = "myprivate"
  datacenter_id = "${data.vsphere_datacenter.my_dc.id}"

  uplinks         = ["uplink1"]
  active_uplinks  = ["uplink1"]

  host {
    host_system_id = "${data.vsphere_host.host.0.id}"
    devices        = ["${var.network_interfaces}"]
  }

  host {
    host_system_id = "${data.vsphere_host.host.1.id}"
    devices        = ["${var.network_interfaces}"]
  }

  host {
    host_system_id = "${data.vsphere_host.host.2.id}"
    devices        = ["${var.network_interfaces}"]
  }
}

インポートする

$ terraform import --var="vsphere_password=####" vsphere_distributed_virtual_switch.myprivate_dvs /mycloud/network/myprivate
vsphere_distributed_virtual_switch.myprivate_dvs: Importing from ID "/mycloud/network/myprivate"...
vsphere_distributed_virtual_switch.myprivate_dvs: Import complete!
  Imported vsphere_distributed_virtual_switch (ID: 50 2d 17 35 c3 ea f4 f3-32 64 07 3c 8e 3a d8 8a)
vsphere_distributed_virtual_switch.myprivate_dvs: Refreshing state... (ID: 50 2d 17 35 c3 ea f4 f3-32 64 07 3c 8e 3a d8 8a)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

plan

terraform plan

すると結構差分が出るので、後はドキュメントを読んで差分を埋める

ストレージ

f:id:yaaamaaaguuu:20180311175223p:plain

現状は上記

import前の定義

storage.tfで以下のように書く

resource "vsphere_nas_datastore" "storage1" {
  name            = "storage1.mycloud.local"
  host_system_ids = ["${data.vsphere_host.host.*.id}"]

  type         = "NFS"
  remote_hosts = ["storage1.mycloud.local"]
  remote_path  = "/data/virtual-machines"
}

import

データストアのインポートにはmoidが必要らしいのでweb clientのurlから適当に拾ってくる

$ terraform import -var="vsphere_password=###" vsphere_nas_datastore.storage1 datastore-11
vsphere_nas_datastore.storage1: Importing from ID "datastore-11"...
vsphere_nas_datastore.storage1: Import complete!
  Imported vsphere_nas_datastore (ID: datastore-11)
vsphere_nas_datastore.storage1: Refreshing state... (ID: datastore-11)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

plan

terraform plan

ストレージのpalnは特に差分が出ない

一旦まとめ

$ tree
.
├── administration.tf
├── datacenter.tf
├── hosts.tf
├── network.tf
├── providor.tf
├── storage.tf
├── terraform.tfstate
└── terraform.tfstate.backup

こんな感じになった。