The vRealize Automation platform allows you not only to organize the work of the cloud, but also to work with it, describing the infrastructure as code (Incfastructure as Code – IaC ). One of the most common tools that implements this model and is already integrated with vRA is Terraform. Getting started with how Terraform and vRA 8 work together.
If we have a quick start , then immediately a working example of a Terraform configuration file for creating a server on vRA 8 (based on the template published in the directory):
# main.tf
provider "vra" {
url = var.url
refresh_token = var.refresh_token
insecure = var.insecure
}
data "vra_project" "this" {
name = var.project_name
}
data "vra_catalog_item" "this" {
name = var.catalog_item_name
expand_versions = true
}
resource "vra_deployment" "this" {
name = var.deployment_name
description = var.description
catalog_item_id = data.vra_catalog_item.this.id
catalog_item_version = var.catalog_item_version
project_id = data.vra_project.this.id
inputs = {
vmSize = var.flavor
imageName = var.image
}
timeouts {
create = "60m"
delete = "20m"
}
}
To correctly fill in all the fields in inputs, request information about the names of the fields and their values from the administrator. You can get the full description of the fields yourself – via the REST API request to the vRA : https: // <vRA-HOSTNAME> / catalog / api / items / {catalog_item_id}.
The vra provider is required to work, it will be installed when terraform init is executed (you can manually download it from the link https://releases.hashicorp.com/terraform-provider-vra/ )
# variables.tf
variable "url" {}
variable "refresh_token" {}
variable "insecure" {}
variable "project_name" {}
variable "catalog_item_name" {}
variable "catalog_item_version" {}
variable "deployment_name" {}
variable "flavor" {}
variable "image" {}
variable "description" {}
# terraform.tfvars
refresh_token = "ys34567...LnfxhN"
url = "https://vra8.zabedu.ru"
insecure = "true"
project_name = "Project_AS"
catalog_item_name = "Get new VM"
catalog_item_version = "2.4"
deployment_name = "Terraform test"
flavor = "medium"
image = "CentOS_7"
description = "Test deployment (terraform)"
Before you start working with vRA, you need to get a refresh-token, it is valid for 90 days. The token is returned by a POST request: https: // <vRA-HOSTNAME> / csp / gateway / am / api / login? Access_token. You can find request parameters and examples of obtaining a token in the documentation: https://code.vmware.com/docs/12597/GUID-AC1E4407-6139-412A-B4AA-1F102942EA94.html .
A ready-made script for obtaining a refresh token (for the laziest, python3):
# get_refresh_token.py
import requests
import json
from getpass import getpass
username = input("vRA user: ")
password = getpass()
body = {
"username": username,
"password": password
}
json_data = json.dumps(body)
vra_url = "https://vra8.zabedu.ru/csp/gateway/am/api/login?access_token"
headers = {'Content-type': 'application/json'}
response = requests.post(vra_url, verify = False, data = json_data, headers=headers)
response_json = response.json()
file_name = "refresh_token.txt"
f = open(file_name, "w")
f.write(response_json["refresh_token"])
f.close
Request forms in the vRA portal often contain additional validation for the input. Using program calls, you independently control the correctness of all transmitted values. Do not forget that:
- VRA 8 deployment names cannot be duplicated;
- Virtual machine names must be unique and comply with your internal standards and format agreed with administrators;
- The set of template fields can be changed by vRealize Automation administrators (especially during the early stages of vRA deployment);
- The transfer of incorrect data can lead to various errors both during the deployment of the deployment and during its operation.
All configuration files and code from this article are available for download at https://github.com/isas2.
Translated by Google Translate