Skip to content

How to make a request with the Rancher2 Terraform Provider

This document (000020792) is provided subject to the disclaimer at the end of this document.

Environment

Situation

Sometimes it is necessary to create a basic skeleton for beginning a task, like using the Rancher2 Terraform Provider to speak with the Rancher API.

This represents a starting point to achieve a simple read-only task: "query the cluster information for the local Rancher cluster".

Resolution

Terraform commands are very easy, below are the main options one may typically use.

  • terraform init -- download needed files like the rancher2 terraform provider
  • terraform plan -- compare the environment to the state file, plan is like a diff of any changes to be made
  • terraform apply -- apply the planned changes
  • terraform refresh -- update the state to match remote systems
  • terraform output -- show output values from the main.tf plan
  • terraform destroy -- clean up anything created by terraform
  • terraform fmt -- spacing is important in HCL, terraform's language, use this command to format all spacing in the current working directory

To get started, create a directory to hold all of the files. Terraform will examine the local file or files, and then populate a local terraform.tfstate data file which represents the most recent refresh of the information from the Rancher API.  The files below can be separate or all together in a main.tf file.  Separating plan files into individual pieces can make managing a larger project easier.

Terraform will take actions required using variables supplied by the user or admin, or computed during the "apply" operation.  As a typical rule of thumb for any provider, "data" sources are read operations while "resource" operations are write/create/change.

Upon running "terraform apply" with the main.tf file below, terraform will contact the Rancher API, authenticate, request the cluster_info for the local Rancher cluster with ID "local" and store it into the terraform statefile, as well as output the cluster labels to the screen.  The comments explain a potential name for each file, the only requirement that it ends with the file suffix ".tf".

### tfvars.tf or environment.tf

#  these outline the url speaking to, and the authorization token

variable "api_url" {
  description = "rancher api url"
  default     = "https://urlto.rancher-fqdn.com"
}

variable "token_key" {
  description = "api key to use for tf"
  default     = "token-nameid:jwt-long-hash-string"
}

### providers.tf

# use the variables from the earlier section to define the provider

provider "rancher2" {
  api_url   = var.api_url
  token_key = var.token_key
  insecure  = true
}

### versions.tf

# tell terraform what versions of providers and terraform itself, to expect

terraform {
  required_providers {
    rancher2 = {
      source  = "rancher/rancher2"
      version = ">= 6.0.0"
    }
  }
  required_version = ">= 1.5.7"
}

### main.tf

## hard-coded example, read cluster info for local

data "rancher2_cluster" "local" {
  name = "local"
}

output "cluster_labels" {
  value     = data.rancher2_cluster.local.labels
}

Status

Top Issue

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.