此处介绍了如何在 NSX Advanced Load Balancer 中安装 Terraform。

使用提供程序

以下是使用提供程序的步骤:

  1. 在 Terraform 计划中创建提供程序。例如,要在 0.1.x 中限制模板版本,可以使用以下 CLI:

    provider "avi" { avi_username = "admin" avi_tenant = "admin" avi_password = "something" avi_controller= "42.42.42.42" }
  2. 创建 NSX Advanced Load Balancer 池示例。此处,池依赖于只读租户数据源和在 Terraform 计划中定义为资源的另一个运行状况监控器。

    resource "avi_pool" "testpool" {
       name= "pool-42",
       health_monitor_refs= ["${avi_healthmonitor.test_hm_1.id}"]
       tenant_ref= "${data.avi_tenant.default_tenant.id}"
       cloud_ref= "${data.avi_cloud.default_cloud.id}"
       application_persistence_profile_ref= "${avi_applicationpersistenceprofile.test_applicationpersistenceprofile.id}"
       servers {
         ip= {
           type= "V4",
           addr= "10.90.64.66",
         }
         port= 8080
       }
       fail_action= {
         type= "FAIL_ACTION_CLOSE_CONN"
       }
     }
  3. 作为只读或数据源引用现有资源。

    data "avi_applicationprofile" "system_http_profile" {
       name= "System-HTTP"
     }
        
     application_profile_ref= "${data.avi_applicationprofile.system_https_profile.id}"

构建提供程序

以下是构建提供程序的过程:

  1. 您可以将存储库克隆到以下路径:

    $GOPATH/src/github.com/terraform-providers/terraform-provider-avi  
    $ mkdir -p $GOPATH/src/github.com/terraform-providers; cd $GOPATH/src/github.com/terraform-providers
     $ git clone https://github.com/terraform-providers/terraform-provider-avi.git 
  2. 指定提供程序目录并构建提供程序,如下所示:

     $ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-            $ make   

开发提供程序

以下是开发提供程序的步骤:

  1. 在您的计算机上安装 Go。您可以通过添加 $GOPATH/bin to your $PATH 来设置 GOPATH

  2. 要编译提供程序,请运行 make 构建。这将构建提供程序并将提供程序二进制文件放在 $GOPATH/bin 目录中。

    $ make bin 
    ... 
    $ $GOPATH/bin/terraform-provider-avi 
    ...
  3. 运行 make test 以测试提供程序,如下所示:

    $ make test
  4. 运行 make testacc test 以运行全套验收测试,如下所示:

    $ make testacc
注:

如果您希望 Terraform 在计划更新期间禁止敏感字段的差异,则需要将 AVI_SUPPRESS_SENSITIVE_FIELDS_DIFF 环境变量导出为 AVI_SUPPRESS_SENSITIVE_FIELDS_DIFF=true。但是,如果要有意更新计划更新中的敏感字段,则需要取消导出环境变量或将其设置为 false,即导出 AVI_SUPPRESS_SENSITIVE_FIELDS_DIFF=false 或取消设置 AVI_SUPPRESS_SENSITIVE_FIELDS_DIFF

示例 - 使用 Terraform 的虚拟服务

provider "avi" {
  avi_username = "admin"
  avi_tenant = "admin"
  avi_password = "something"
  avi_controller= "10.10.25.42"
}

data "avi_applicationprofile" "system_http_profile" {
  name= "System-HTTP"
}

data "avi_applicationprofile" "system_https_profile" {
  name= "System-Secure-HTTP"
}

data "avi_tenant" "default_tenant" {
  name= "admin"
}

data "avi_cloud" "default_cloud" {
  name= "Default-Cloud"
}

data "avi_serviceenginegroup" "se_group" {
  name = "Default-Group"
}

data "avi_networkprofile" "system_tcp_profile" {
  name= "System-TCP-Proxy"
}

data "avi_analyticsprofile" "system_analytics_profile" {
  name= "System-Analytics-Profile"
}

data "avi_sslkeyandcertificate" "system_default_cert" {
  name= "System-Default-Cert"
}

data "avi_sslprofile" "system_standard_sslprofile" {
  name= "System-Standard"
}

data "avi_vrfcontext" "global_vrf" {
  name= "global"
}

resource "avi_networkprofile" "test_networkprofile" {
  name= "networkprofile-42"
  tenant_ref= "${data.avi_tenant.default_tenant.id}"
  profile{
    type= "PROTOCOL_TYPE_TCP_PROXY"
  }
}

resource "avi_applicationpersistenceprofile" "test_applicationpersistenceprofile" {
  name = "applicationpersistence-42"
  tenant_ref= "${data.avi_tenant.default_tenant.id}"
  persistence_type = "PERSISTENCE_TYPE_CLIENT_IP_ADDRESS"
}

resource "avi_vsvip" "test_vsvip" {
  name= "vip-42"
  vip {
    vip_id= "0"
    ip_address {
      type= "V4",
      addr= "10.90.64.88",
    }
  }
}

resource "avi_virtualservice" "test_vs" {
  name= "vs-42"
  pool_ref= "${avi_pool.testpool.id}"
  tenant_ref= "${data.avi_tenant.default_tenant.id}"
  cloud_ref= "${data.avi_cloud.default_cloud.id}"
  application_profile_ref= "${data.avi_applicationprofile.system_https_profile.id}"
  network_profile_ref = "${data.avi_networkprofile.system_tcp_profile.id}"
  vsvip_ref = "${avi_vsvip.test_vsvip.id}"
  vip {
    vip_id= "0"
    ip_address {
      type= "V4",
      addr= "10.90.64.88",
    }
  }
  services {
    port= 80
    enable_ssl= true
    port_range_end= 80
  }
  cloud_type = "CLOUD_VCENTER"
  se_group_ref= "${data.avi_serviceenginegroup.se_group.id}"
  analytics_profile_ref= "${data.avi_analyticsprofile.system_analytics_profile.id}"
  ssl_key_and_certificate_refs= ["${data.avi_sslkeyandcertificate.system_default_cert.id}"]
  ssl_profile_ref= "${data.avi_sslprofile.system_standard_sslprofile.id}"
  vrf_context_ref= "${data.avi_vrfcontext.global_vrf.id}"
}

resource "avi_healthmonitor" "test_hm_1" {
  name = "healthmonitor-42"
  type = "HEALTH_MONITOR_HTTP"
}

resource "avi_pool" "testpool" {
  name= "pool-42",
  health_monitor_refs= ["${avi_healthmonitor.test_hm_1.id}"]
  tenant_ref= "${data.avi_tenant.default_tenant.id}"
  cloud_ref= "${data.avi_cloud.default_cloud.id}"
  application_persistence_profile_ref= "${avi_applicationpersistenceprofile.test_applicationpersistenceprofile.id}"
  servers {
    ip= {
      type= "V4",
      addr= "10.90.64.66",
    }
    port= 8080
  }
  fail_action= {
    type= "FAIL_ACTION_CLOSE_CONN"
  }
}
注:

有关模型的更多信息,请参阅 OpenAPI (Swagger 2.0) 规范集成