Module
NDFC Terraform

Fabric Module

In this section you will be creating a fabric module

Step 1 - Create modules/fabric folder



mkdir -p ~/workspace/ndfclab/terraform/modules/fabric

We will use the module to organize our Terraform code. The module will contain the configuration for the fabric module.


Step 2 - Create main.tf file


touch ~/workspace/ndfclab/terraform/main.tf
code-server -r ~/workspace/ndfclab/terraform/main.tf

The following code does the following:
module is a Terraform keyword that signifies the declaration of a module.
"fabric" is the local name you are giving to this specific instance of the module.
source is a required argument for any module block. It tells Terraform where to find the source code for the module you want to use.
"./modules/fabric" specifies a local path to the module's source code.

  • Terraform will look for .tf files (e.g., variables.tf, outputs.tf, main.tf, resource.tf, etc.) within the ./modules/fabric directory. These files collectively define the resources, variables, and outputs of the fabric module.


module "fabric" {
    source = "./modules/fabric"
}


Step 3 - Create provider.tf file to specify the provider and connection information for NDFC


touch ~/workspace/ndfclab/terraform/provider.tf
code-server -r ~/workspace/ndfclab/terraform/provider.tf

The provider.tf file is crucial for configuring how Terraform interacts with external services or APIs, in this case, your NDFC instance.
terraform is the main configuration block for Terraform settings.
required_providers This nested block declares the external providers that your Terraform configuration depends on. It's a critical part of ensuring that your configuration is explicit about its dependencies and that others can easily set up their environment to run your code.
ndfc is the local name you're assigning to this provider. When you later declare resources from this provider (e.g., resource "ndfc_fabric" ...), Terraform knows to use the settings defined here.

  • source specifies the registry address for the provider.
    • CiscoDevNet is the namespace (often an organization or user name) in the Terraform Registry. ndfc is the name of the provider.
    • When you run terraform init, Terraform will search the official Terraform Registry (or a configured private registry) for a provider published by CiscoDevNet under the name ndfc.
  • version specifies the exact version to use for the ndfc provider.
The bottom provider block configures a specific instance of the ndfc provider that is defined above, providing the authentication and connection details needed to interact with your NDFC instance.


terraform {
  required_providers {
    ndfc = {
      source  = "CiscoDevNet/ndfc"
      version = "0.1.0"
    }
  }
}

provider "ndfc" {
  url      =  "https://10.15.0.26"
  username = "admin"
  password = "cisco.123"
  insecure = true
}


Step 4 - Create modules/fabric/variables.tf file


touch ~/workspace/ndfclab/terraform/modules/fabric/variables.tf
code-server -r ~/workspace/ndfclab/terraform/modules/fabric/variables.tf


The variables.tf file is used to define input variables for this module. These variables allow you to parameterize your module, making it reusable and configurable for different environments or scenarios.
The variables defined in this file will be used to configure the fabric module, allowing you to specify details like the fabric name and IP addresses for your staging devices.


terraform {
  required_providers {
    ndfc = {
      source  = "CiscoDevNet/ndfc"
      version = "0.1.0"
    }
  }
}

variable "fabric_name" {
  description = "Name of the fabric"
  type        = string
  default     = "fabric-terraform"
}

variable "staging-spine1" {
  description = "Staging Spine1 IP address"
  type        = string
  default     = "10.15.6.11"
}

variable "staging-leaf1" {
  description = "Staging Leaf1 IP address"
  type        = string
  default     = "10.15.6.12"
}

variable "staging-leaf1-serial" {
  description = "Staging Leaf1 Serial Number"
  type        = string
  default     = "READ_TIMEOUT"
}

variable "staging-leaf2" {
  description = "Staging Leaf2 IP address"
  type        = string
  default     = "10.15.6.13"
}

variable "staging-leaf2-serial" {
  description = "Staging Leaf2 Serial Number"
  type        = string
  default     = "READ_TIMEOUT"
}


Step 5 - Execute the terraform init command

The terraform init command is used to initialize a Terraform working directory. It downloads the necessary provider plugins and sets up the backend for storing the state file.



terraform init

Initializing the backend...
Initializing modules...
Initializing provider plugins...
- Reusing previous version of ciscodevnet/ndfc from the dependency lock file
- Using previously-installed ciscodevnet/ndfc v0.1.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.