Deploy Nutanix resources with Terraform

Deploy Nutanix resources with Terraform

Introduction

Infrastructure as Code (IaC) is an IT infrastructure management approach that emphasizes the use of code to automate the provisioning, deployment, and configuration of infrastructure resources. The goal is to treat infrastructure as software, using the same tools, processes, and methodologies to manage it. This approach offers many benefits, such as improved consistency, reliability and scalability of infrastructure, faster delivery of new features and services, and reduced risk of human error. Nutanix resources such as images, virtual servers, and groups can be deployed using IaC. There are various techniques available for this purpose. This blog will focus on using Terraform.

What is Terraform?

Terraform is an open-source IaC tool that enables users to define and manage infrastructure resources across multiple cloud providers and on-premises data centers, including Nutanix. It provides a declarative configuration language and supports a wide range of infrastructure services, including compute, storage, networking, and more. Terraform uses a state file to keep track of the current infrastructure configuration, and it can apply changes to infrastructure resources safely and predictably. Terraform is designed to enable infrastructure automation and reduce the time and effort required to provision and manage infrastructure.

Terraform depends on a provider. A provider is a plugin that allows Terraform to interact with a specific cloud provider, service, or platform API. Providers are responsible for managing the lifecycle of resources and handling API requests and responses for their respective services. The public registry of HashiCorp, the software company that provides Terraform, includes the Nutanix provider. For more information, see: The Nutanix provider.

If you want to install Terraform, you can find the instructions for various operating systems on: Install Terraform.

The basics of Terraform

A Terraform configuration is written in HashiCorp Configuration Language (HCL) and describes the desired state of the infrastructure resources. Terraform automatically determines how to configure the resources to reach the desired state. You only need to specify the desired state of the resources, and Terraform will take care of the rest.

In addition, with Terraform data from a resource can be retrieved that can later be used in the configuration. In the Terraform configuration file, the format for creating a resource is always: resource <resource defined in the provider> <unique name> { parameters }, and for retrieving data: data <data source defined in the provider> <unique name> { parameters }.

All configuration files have the .tf extension. As a best practice, the main configuration file should be named main.tf, while the file variables.tf declares all the variables. The file terraform.tfvars assigns values to the variables. Terraform uses the internal file terraform.tfstate to store the current state of all resources. These files are used when applying Terraform. See the next figure.

Terraform - Concept of Terraform

There are four basic commands in Terraform to manage infrastructure resources:

init: This command initializes a new or existing Terraform working directory. It downloads the required provider plugins, initializes the backend, and prepares the environment for Terraform to run.

plan: This command generates an execution plan based on the current state of the Terraform configuration and the target infrastructure. The plan outlines the changes that Terraform will make to the infrastructure to reach the desired state.

apply: This command applies the changes outlined in the execution plan generated by the plan command. It creates, modifies, or deletes infrastructure resources as needed to reach the desired state.

destroy: This command destroys all the resources managed by the Terraform configuration. It is a safety mechanism to ensure that all resources are destroyed when they are no longer needed.

What can you deploy on Nutanix?

With Terraform, you can deploy the following resources on Nutanix:

  • Prism Central: virtual machines, images, projects, VPCs, categories, users, etc.
  • Foundation: api key, image nodes, ipmi configuration
  • Nutanix Database Services: databases, restores, profiles, tags, etc.
  • Karbon: k8s cluster, private registry

Deploy Nutanix resources

This section describes some examples of deploying Nutanix resources. You can find all examples on the Metis IT public git repository: Metis IT Public Repository

The basics

Everything starts with a configuration file that defines the provides that will be used for managing resources. You can find an example of a template on the Metis IT public git repository: example template with Nutanix provider.

This is the content of the template.

terraform {
  required_providers {
    nutanix = {
      source = "nutanix/nutanix"
      version = "1.8.1"
    }
  }
}
provider "nutanix" {
  username     = var.nutanix_username
  password     = var.nutanix_password
  endpoint     = var.nutanix_endpoint
  port         = var.nutanix_port
  insecure     = true
  wait_timeout = 10
}

The code snippet defines the Nutanix provider and specifies its version, and it also configures the provider with the necessary information. The variables used in this code snippet are defined in the file variables.tf, which can be found in the Metis public Git repository.

In the following sections, I’ll describe some use cases. All files can be downloaded from the Metis IT public Git repository (Metis IT Public Repository). You will also find more examples in this repository. In my experience, the best way to learn Terraform is by doing.

Create an image

The following code snippet creates an image on Nutanix from a qcow2 image located on a web server. You can find all the necessary files at create an image.

resource "nutanix_image" "image" {
  name        = "Arch Linux"
  description = "Arch-Linux-x86_64-basic"
  source_uri  = "https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-basic.qcow2"
}

At the moment, it is not possible to define the storage container. The image will always be created in the SelfService storage container.

Create a virtual server

Also the deployment of a virtual server can be done with Terraform. Since a virtual server consists of various components, the Terraform configuration can be quite large. Therefore, instead of providing the code snippet here, I will refer you to the example on the public Metis IT Git repository: create a virtual machine

In this example I’m also using the count meta-argument. By using this meta-argument you can deploy more resources from the same block.

Get a list of users

With Terraform it is not only possible to create resources, but you can also get information from a Nutanix resource. With this code snippet you get a list of all users on Prism Central that are logged in at least once.

data "nutanix_users" "users" {}
output "users" {
  value = data.nutanix_users.users
}

After you applied a Terraform configuration file with the data source Nutanix_users, you can save the users list to a file in json format with the following command: terraform output -json users > users.json

You can find all the necessary files of this example at: get a list of users

Conclusion

Terraform makes it possible to deploy resources on a Nutanix cluster with infrastructure as code. Additionally, Terraform can support a CI/CD pipeline involving a Nutanix cluster. However, it is important to note that Terraform currently does not provide a complete set of resources that can be deployed on a Nutanix cluster. Furthermore, Terraform has limited support for configuring resources once they have been deployed. This is where Ansible can play a role. With Terraform, resources can be deployed, and with Ansible, they can be further configured. In my next blog, I’ll explain more about Ansible in combination with Nutanix. Stay tuned!

2 juni 2023
Ronald van Vugt