Jump to content
Welcome to our new Citrix community!
  • Importing existing NetScaler configuration into Terraform


    Rohit Myali
    • Validation Status: Work In Progress
      Summary: Learn how to import existing NetScaler configuration into terraform using terraform import command
      Has Video?: No

    Importing Existing NetScaler Configuration into Terraform

    Usecase

    If you want to manage your NetScaler from terraform you need to have the existing infrastructure in the Terraform state file. In that case, terraform import will be helpful in importing the existing NetScaler configuration into terraform. Refer here to read about terraform import command.

    Usage

    Step 1: Identify the existing infrastructure you will import

    • You will have all the config saved in your NetScaler as equivalent CLI command. Identify the configuration that you wish to import into terraform.
    • Below is the image representing the configuration that is already present in NetScaler. We will import these configurations into terraform using the terraform import command.
    image.png.d440a0e253ff8532cc00bec4e1f0428b.png

    Step 2 : Create provider and terraform block

    • Write the terraform block that specifies the terraform to install the required provider plugin onto the local folder and the provider block will be used to connect to the target NetScaler.
    provider.tf

    terraform {

      required_providers {

        citrixadc = {

          source = "citrix/citrixadc"

        }

      }

    }

    provider "citrixadc" {

      endpoint = "http://localhost:8080"

      username = "<username>"

      password = "<password>"

    }

    Step 3: Create the resource block for the resource that you wish to import

    • You want to import above mentioned existing instance into the terraform from NetScaler. we have one instance of lbvserver, two instances of service, and binding of lbvserver to both the services.
    • write the empty resource block or resource block with the same existing configuration present in the target NetScaler. As shown below
    resource "citrixadc_lbvserver" "demo_lbvserver" {

     

    }

    resource "citrixadc_service" "demo_service1" {

     

    }

    resource "citrixadc_service" "demo_service2" {

     

    }

    resource "citrixadc_lbvserver_service_binding" "demo_binding1" {

     

    }

    resource "citrixadc_lbvserver_service_binding" "demo_binding2" {

     

    }

    Step 4: Import the Resource Instance

    • First initialize the terraform with terraform init command.
    • The resource should be imported using the primary ID of the particular resource(ID can be found in the documentation).
    • Identify the existing infrastructure you will import. Import use the command terraform import <resource_type>.<resource_name> <resourceID>
    • For the above-given configuration, we have lbvserver, service and lbvserver_service_binding resources.

      The terraform import command for each resource would be:

    terraform import citrixadc_lbvserver.demo_lbvserver tf_lbvserver

    terraform import citrixadc_service.demo_service1 tf_service1

    terraform import citrixadc_service.demo_service2 tf_service2

    terraform import citrixadc_lbvserver_service_binding.demo_binding1 tf_lbvserver,tf_service1

    terraform import citrixadc_lbvserver_service_binding.demo_binding2 tf_lbvserver,tf_service2

    Step 5: Write Terraform configuration that matches that infrastructure.

    • You can view the configuration in terraform state by command terraform show
    • Now fill the resource block that matches your existing infrastructure configurations by copying it from terraform state file. You can choose to copy only certain attributes for resources which you want to maintain.
    • Below is an example configuration that matches the above existing NetScaler infrastructure.
    resource "citrixadc_lbvserver" "demo_lbvserver" {

      name        = "tf_lbvserver"

      ipv46       = "10.10.10.10"

      port        = 80

      servicetype = "ANY"

    }

    resource "citrixadc_service" "demo_service1" {

      name        = "tf_service1"

      ip          = "10.10.10.1"

      servicetype = "ANY"

      port        = 90

    }

    resource "citrixadc_service" "demo_service2" {

      name        = "tf_service2"

      ip          = "10.10.10.2"

      servicetype = "ANY"

      port        = 80

    }

    resource "citrixadc_lbvserver_service_binding" "demo_binding1" {

      name        = "tf_lbvserver"

      servicename = "tf_service1"

    }

    resource "citrixadc_lbvserver_service_binding" "demo_binding2" {

      name        = "tf_lbvserver"

      servicename = "tf_service2"

    }

     

    In order to create dependencies between resources , user needs to manually edit certain values of the attributes for certain resources such as binding entities. For example demo_binding1 resource needs to edited as follows:

    resource "citrixadc_lbvserver_service_binding" "demo_binding1" {

      name        =  citrixadc_lbvserver.demo_lbvserver.name

      servicename =  citrixadc_service.demo_service1.name

    }

    Step 6: Validate the import is successful

    • After you import the resource and write the corresponding terraform configuration running terraform plan shouldn’t introduce any changes i.e., when you execute the command terraform plan you should get the below message.

    image.png.5c7b3e2ffbe7b96d2f67cb22c9c65a8d.png

    Further References


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...