Для улучшения работы сайта мы используем файлы 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"
  }
}
Создание ВМ с SDN-конфигурацией сетевых интерфейсов
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"

    sdn_port {
      security_groups_enabled = true
      security_group_ids = [
        "600f8bba-9479-4d3f-b1de-52c43950bc00",
        "fdba5d33-4b2b-4243-94bf-300105fae8ce"
      ]

      fixed_ip {
        subnet_id = "005fb3cf-666d-4a27-ba4e-31a329fa13ea"
        ip_address = "192.168.0.1"
      }

      mac_learning = true
    }
  }
}
Создание ВМ с инициализацией
resource "zvirt_vm" "linux" {
  name        = "linux"
  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    = "linux.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" "windows" {
  name        = "windows"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  initialization {
    hostname = "windows.example.com"

    active_directory_ou = "OU=example,OU=ru"
    domain              = "example.ru"
    org_name            = "OrganizationA"

    custom_script = <<EOF
<SynchronousCommand wcm:action="add">
    <Order>1</Order>
    <CommandLine>powershell.exe -ExecutionPolicy Bypass -File C:\path\to\setup.ps1</CommandLine>
    <Description>Run setup script</Description>
</SynchronousCommand>
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"
  }
}
Создание ВМ с параметрами НА
resource "zvirt_vm" "vm" {
  name        = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id

  high_availability       = true
  resume_behaviour        = "kill"
  lease_storage_domain_id = "3cab9169-4f9f-4e6d-965e-a6a6a32ba298"
}
Создание ВМ с защитой от удаления
resource "zvirt_vm" "vm" {
  name             = "vm"
  cluster_id  = data.zvirt_cluster.cluster.id
  template_id = data.zvirt_template.template.id
  delete_protected = true
}