Contents
TerraformでNSX-Tのプロバイダーがあったので使ってみました。
NSX-T Terraform Provider
terraform-provider-nsxt
環境
項目 | バージョン |
---|---|
CentOS | 8.1.1911 |
NSX-T | 3.0 |
Terraform | 0.12.24 |
go | 1.14.2 |
準備
goのインストール
Providerをビルドするためにgoのバイナリをダウンロードします。
1 2 3 4 5 6 7 |
[root@terraform ~]# curl -L https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz -O [root@terraform ~]# tar zxvf go1.14.2.linux-amd64.tar.gz -C /usr/local/ [root@terraform ~]# echo 'export PATH=$PATH:/usr/local/go/bin/' >> .bashrc [root@terraform ~]# . .bashrc [root@terraform ~]# which go /usr/local/go/bin/go |
Terraformのインストール
Terraformのバイナリをダウンロードします。
1 2 3 4 5 6 |
[root@terraform ~]# curl -L https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip -O [root@terraform ~]# unzip terraform_0.12.24_linux_amd64.zip [root@terraform ~]# mv terraform /usr/local/bin/ [root@terraform ~]# which terraform /usr/local/bin/terraform |
NSX-T Providerのインストール
NSX-T Providerをビルドします。
1 2 3 4 5 6 7 8 |
[root@terraform ~]# dnf -y install git [root@terraform ~]# mkdir -p $GOPATH/src/github.com/terraform-providers [root@terraform ~]# cd $GOPATH/src/github.com/terraform-providers [root@terraform terraform-providers]# git clone https://github.com/terraform-providers/terraform-provider-nsxt.git [root@terraform terraform-providers]# cd $GOPATH/src/github.com/terraform-providers/terraform-provider-nsxt [root@terraform terraform-provider-nsxt]# make [root@terraform terraform-provider-nsxt]# cd |
実行
ここでは、あまり複雑ではないセグメントの追加を例にしてみます。
main.tfファイル作成
main.tf
ファイルを作成します。
環境に合わせてパラメーター値を変更してください。
1 2 |
[root@terraform ~]# vi main.tf |
1 2 3 4 5 6 7 |
provider "nsxt" { host = "nsxt-manager" username = "admin" password = "secret" allow_unverified_ssl = true } |
initの実行
Providerをフェッチするために init
を実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@terraform ~]# terraform init Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "nsxt" (terraform-providers/nsxt) 2.0.0... 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.nsxt: version = "~> 2.0" 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. |
セグメント情報の追加
以下のように main.tf
へセグメント情報を追加します。
ここでは vlan
のセグメントを vlansegment1
という名前で追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
provider "nsxt" { host = "nsxt-manager" username = "admin" password = "secret" allow_unverified_ssl = true } data "nsxt_policy_transport_zone" "vlantz" { transport_type = "VLAN_BACKED" is_default = true } resource "nsxt_policy_vlan_segment" "vlansegment1" { display_name = "vlansegment1" description = "Terraform provisioned VLAN Segment" transport_zone_path = data.nsxt_policy_transport_zone.vlantz.path domain_name = "tftest2.org" vlan_ids = ["101", "102"] subnet { cidr = "12.12.2.1/24" } advanced_config { connectivity = "OFF" local_egress = true } } |
planの実行
planを実行してみて、どういう変更が加わるか確認してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
[root@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.nsxt_policy_transport_zone.vlantz: Refreshing state... ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # nsxt_policy_vlan_segment.vlansegment1 will be created + resource "nsxt_policy_vlan_segment" "vlansegment1" { + description = "Terraform provisioned VLAN Segment" + display_name = "vlansegment1" + domain_name = "tftest2.org" + id = (known after apply) + nsx_id = (known after apply) + path = (known after apply) + revision = (known after apply) + transport_zone_path = "/infra/sites/default/enforcement-points/default/transport-zones/a95c914d-748d-497c-94ab-10d4647daeba" + vlan_ids = [ + "101", + "102", ] + advanced_config { + connectivity = "OFF" + hybrid = false + local_egress = true } + subnet { + cidr = "12.12.2.1/24" + network = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. |
セグメントが追加される変更が加わることが確認できました。
applyの実行
実機に設定を反映してセグメントを作成します。
1 2 3 4 5 6 7 |
[root@terraform ~]# terraform apply -auto-approve data.nsxt_policy_transport_zone.vlantz: Refreshing state... nsxt_policy_vlan_segment.vlansegment1: Creating... nsxt_policy_vlan_segment.vlansegment1: Creation complete after 1s [id=363c7c58-1cdd-4d9a-84fe-6cb11fe17f82] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. |
こんな感じで作成できました 🙂
設定を変更したり削除する場合は、セグメントのresourceの内容を変更したり(resource自体を)削除すればいいです。