Для улучшения работы сайта мы используем файлы cookies. Оставаясь на сайте, вы соглашаетесь с политикой обработки персональных данных.

Примеры использования

Примеры описания структурных файлов
datasources.tf
data "zvirt_cluster" "cluster" {
  name = "Default"
}
data "zvirt_template" "template" {
  name = var.template
  version = "1"
}
data "zvirt_vnic_profile" "vnic_profile" {
  name = var.vnic_profile
}
data "zvirt_storage_domain" "storage_domain" {
  name = var.data_storage_domain
}
terraform.tfvars
# variables
zvirt_hostname = "ost-basic-suite-master-engine.lago.local"
zvirt_username = "admin@zvirt@internalsso"
zvirt_password = "123456"

cluster = "Default"
data_storage_domain = "fc-data-1"
template = "AstraLinux175"
vnic_profile = "vlan100"
variables.tf
# variables
variable "zvirt_hostname"{
    description = "engine.example.ru"
    type = string
}

variable "zvirt_username" {
  description = "User name to get token (append @internal for aaa-jdbc or @internalsso for Keycloak)"
  type = string
}

variable "zvirt_password" {
  description = "User password"
  type = string
}

variable "cluster" {
  description = "Cluster name"
  type        = string
}

variable "data_storage_domain" {
  description = "Data storage domain name"
  type        = string
}

variable "template" {
    description = "VM template name"
    type = string
    default = "Blank"
}

variable "vnic_profile" {
  description = "VNic profile name"
  type        = string
}
zvirt.tf
terraform {
  required_version = ">= 1.5.0"
  required_providers {
    zvirt = {
      source  = "terraform-registry.orionsoft.ru/orionsoft/zvirt"
      version = "1.0.0"
    }
  }
}

provider "zvirt" {
  hostname     = var.zvirt_hostname
  username     = var.zvirt_username
  password     = var.zvirt_password
  tls_insecure = true
}

# создание ВМ
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  disk_attachment {
    alias     = "boot"
    interface = "virtio_scsi"
  }

  disk_attachment {
    alias     = "data"
    interface = "virtio_scsi"
    size      = 25 * pow(1024, 3)
  }

  disk_attachment {
    id        = "f6e2202f-6ebc-4634-92ca-b970f0577a6f"
    interface = "virtio_scsi"
  }
}
Создание ВМ с определеными значениями CPU и памяти
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  cpu_sockets = 2
  cpu_cores   = 2
  cpu_threads = 2

  memory            = 4 * pow(1024, 3)
  max_memory        = 8 * pow(1024, 3)
  guaranteed_memory = 1 * pow(1024, 3)
}
Создание ВМ с сетевыми интерфейсами
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  nic {
    vnic_profile_id = data.zvirt_vnic_profile.vnic_profile.id
    name            = "nic1"
    interface       = "virtio"
  }
}
Создание ВМ с инициализацией cloud-init
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  initialization {
    nic_configuration {
      name               = "enp1s0"
      on_boot            = true
      boot_protocol_dhcp = false
      ip_address         = "192.168.0.123"
      netmask            = "255.255.255.0"
      gateway            = "192.168.0.1"
      version            = "v4"
    }
    nic_configuration {
      name               = "enp3s0"
      on_boot            = true
      boot_protocol_dhcp = true
    }

    hostname    = "vm.example.com"
    dns_servers = ["8.8.8.8"]
    dns_search  = "example.com"
    username    = "admin"
    authorized_ssh_keys = [
      "ssh-rsa ...",
      "ssh-rsa ..."
    ]
    custom_script = <<EOF
    runcmd:
    - echo "Hello, World!" > /home/admin/test.txt
    EOF
  }
}
Создание ВМ с подключенными дисками
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  disk_attachment {
    alias     = "boot"
    interface = "virtio_scsi"
  }

  disk_attachment {
    alias     = "data"
    interface = "virtio_scsi"
    size      = 25 * pow(1024, 3) // 25 GiB
  }

  disk_attachment {
    id        = "f6e2202f-6ebc-4634-92ca-b970f0577a6f"
    interface = "virtio_scsi"
  }
}