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

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

Примеры описания структурных файлов
datasources.tf
data "ovirt_cluster" "cluster" {
  name = var.cluster
}
data "ovirt_templates" "template" {
  name = var.template
}
data "ovirt_vnic_profile" "vnic" {
  name = var.vnic_profile
}
data "ovirt_storage_domain" "storage" {
  name = var.data_storage_domain
}
terraform.tfvars
# variables
zvirt_url = "https://ost-basic-suite-master-engine.lago.local/ovirt-engine/"
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_url" {
  description = "URL to engine: https://domain.name/ovirt-engine"
  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_id" {
  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 {
    ovirt = {
      source  = "ovirt/ovirt"
      version = ">= 2.1.5"
    }
  }
}

provider "ovirt" {
  url          = "${var.zvirt_url}/api"
  username     = var.zvirt_username
  password     = var.zvirt_password
  tls_insecure = true
}

# создание ВМ
resource "ovirt_zvm" "vm" {
  cluster_id      = data.ovirt_cluster.cluster.id
  name            = "terraform_vm"
  template_id     = tolist(data.ovirt_templates.template.templates)[0].id
  state           = "run_on_create"
  os_type         = "znode"
  boot_disk_size  = 1 * 1024 * 1024 * 1024
  maximum_memory  = 2 * 1024 * 1024 * 1024
  memory          = 1 * 1024 * 1024 * 1024
  cpu_cores       = 1
  description     = "desc"
  comment         = "comment"
  nic_configuration{
      boot_protocol = "static"
      name = "enp1s0"

      ip {
        address = "10.250.66.31"
        gateway = "10.250.66.254"
        mask = "255.255.255.0"
        version = "v4"
      }

    }
}

# vnic attachment
resource "ovirt_znic_attachment" "nic"{
   vnic_profile_id = data.ovirt_vnic_profile.vnic.id
   vm_id           = ovirt_zvm.vm.id
   name            = "nic10"
}

# disk
resource "ovirt_zdisk" "disk" {
  storage_domain_id = data.ovirt_storage_domain.storage.id
  format            = "cow"
  size              = 3 * 1024 * 1024
  alias             = "terraform_disk"
  sparse            = true
}

# disk attach
resource "ovirt_zdisk_attachment" "disk_attach" {
  vm_id          = ovirt_zvm.vm.id
  disk_id        = ovirt_zdisk.disk.id
  disk_interface = "virtio_scsi"
}
Создание необходимого количиства ВМ с 1 сетевым интерфейсом и 1 диском
config.tf
terraform {
  required_providers {
    ovirt = {
      source  = "ovirt/ovirt"
      version = ">= 2.1.5"
    }
  }
}

provider "ovirt" {
  url          = "https://${var.zvirt_hostname}/ovirt-engine/api"
  username     = var.zvirt_username
  password     = var.zvirt_password
  tls_insecure = true
}
vars.tf
variable "zvirt_hostname" {}
variable "zvirt_username" {}
variable "zvirt_password" {}

variable "cluster" {}
variable "template" {}
variable "vnic" {}
variable "storage" {}

variable "vm_count" {}
vars.auto.tfvars
zvirt_hostname = "engine.example.ru"
zvirt_username = "admin@zvirt@internalsso"
zvirt_password = "P@ssw0rd"

cluster  = "Default"
template = "alma-linux-9-template"
vnic     = "ovirtmgmt"
storage  = "vm-data"

vm_count = 3
datasources.tf
data "ovirt_cluster" "cluster" {
  name = var.cluster
}
data "ovirt_templates" "template" {
  name = var.template
}
data "ovirt_vnic_profile" "vnic" {
  name = var.vnic
}
data "ovirt_storage_domain" "storage" {
  name = var.storage
}
main.tf
resource "ovirt_zdisk" "my_disk" {
  count = var.vm_count

  alias             = var.vm_count > 1 ? "my-vm-${count.index}-disk" : "my-vm-disk"
  size              = 50 * pow(1024, 3)
  format            = "raw"
  sparse            = false
  storage_domain_id = data.ovirt_storage_domain.storage.id
}

resource "ovirt_zvm" "my_vms" {
  count = var.vm_count

  name                      = var.vm_count > 1 ? "my-vm-${count.index}" : "my-vm" // Если количество ВМ 1, имя будет my-vm, если количество больше единицы, ВМ будут называться my-vm-0, my-vm-1 и т.д.
  clone                     = false
  cluster_id                = data.ovirt_cluster.cluster.id
  template_id               = tolist(data.ovirt_templates.template.templates)[0].id
  state                     = "up"
  cpu_sockets               = 1
  cpu_cores                 = 2
  cpu_threads               = 1
  memory                    = 4096 * pow(1024, 2) // 4096 мегабайт памяти
  maximum_memory            = 4096 * pow(1024, 2) // 4096 мегабайт памяти
  boot_disk_size            = 20 * pow(1024, 3)   // 20 гигабайт диск памяти
  placement_policy_host_ids = []
  os_type                   = "rhel_9x64"
  initialization {
    hostname    = var.vm_count > 1 ? "my-vm-${count.index}" : "my-vm"
    dns_servers = "8.8.8.8 8.8.4.4"
  }
}

resource "ovirt_zdisk_attachment" "my_disk_attach" {
  count = var.vm_count

  disk_id        = ovirt_zdisk.my_disk[count.index].id
  vm_id          = ovirt_zvm.my_vms[count.index].id
  disk_interface = "virtio_scsi"
}

resource "ovirt_znic_attachment" "name" {
  count = var.vm_count

  name            = "nic1"
  vnic_profile_id = data.ovirt_vnic_profile.vnic.id
  vm_id           = ovirt_zvm.my_vms[count.index].id
}
Создание дисков и добавление их к виртуальной машине
config.tf
terraform {
  required_providers {
    ovirt = {
      source  = "ovirt/ovirt"
      version = ">= 2.1.5"
    }
  }
}

provider "ovirt" {
  url          = "https://${var.zvirt_hostname}/ovirt-engine/api"
  username     = var.zvirt_username
  password     = var.zvirt_password
  tls_insecure = true
}
vars.tf
variable "zvirt_hostname" {}
variable "zvirt_username" {}
variable "zvirt_password" {}

variable "cluster" {}
variable "template" {}
variable "vnic" {}
variable "storage" {}

variable "disks" {}
vars.auto.tfvars
zvirt_hostname = "engine.example.ru"
zvirt_username = "admin@zvirt@internalsso"
zvirt_password = "P@ssw0rd"

cluster  = "DudeCluster"
template = "alma-9-template"

disks = [
  {
    alias   = "data_disk"
    size    = "50"
    format  = "raw"
    sparse  = "false"
    storage = "Data"
  },
  {
    alias   = "var"
    size    = "15"
    format  = "cow"
    sparse  = "true"
    storage = "Data"
  },
  {
    alias   = "home"
    size    = "10"
    format  = "cow"
    sparse  = "true"
    storage = "Data"
  }
]
datasources.tf
data "ovirt_cluster" "cluster" {
  name = var.cluster
}
data "ovirt_templates" "template" {
  name = var.template
}
data "ovirt_storage_domain" "storages" {
  for_each = { for index, disk in var.disks : disk.alias => disk }
  name     = each.value.storage
}
main.tf
resource "ovirt_zdisk" "example2_disks" {
  for_each          = { for index, disk in var.disks : disk.alias => disk }
  alias             = each.value.alias
  size              = each.value.size * pow(1024, 3)
  format            = each.value.format
  sparse            = each.value.sparse
  storage_domain_id = data.ovirt_storage_domain.storages[each.key].id
}

resource "ovirt_zvm" "example2_vm" {

  name                      = "example2-vm"
  clone                     = false
  cluster_id                = data.ovirt_cluster.cluster.id
  template_id               = tolist(data.ovirt_templates.template.templates)[0].id
  state                     = "up"
  cpu_sockets               = 1
  cpu_cores                 = 2
  cpu_threads               = 1
  memory                    = 4096 * pow(1024, 2)
  maximum_memory            = 4096 * pow(1024, 2)
  boot_disk_size            = 20 * pow(1024, 3)
  placement_policy_host_ids = []
  os_type                   = "rhel_9x64"
}

resource "ovirt_zdisk_attachment" "example2_disks_attach" {
  for_each       = { for index, disk in var.disks : disk.alias => disk }
  vm_id          = ovirt_zvm.example2_vm.id
  disk_interface = "virtio_scsi"
  disk_id        = ovirt_zdisk.example2_disks[each.key].id
}
output.tf
output "storages" {
  value = data.ovirt_storage_domain.storages
}

результат
storages = {
  "data_disk" = {
    "id" = "ae8de7e5-e2b7-4027-9bd0-09e0bda0d168"
    "name" = "Data"
  }
  "home" = {
    "id" = "ae8de7e5-e2b7-4027-9bd0-09e0bda0d168"
    "name" = "Data"
  }
  "var" = {
    "id" = "ae8de7e5-e2b7-4027-9bd0-09e0bda0d168"
    "name" = "Data"
  }
}
Использование модулей

Создается модуль в директории /root/example3/modules/vm/config.tf со следующей структурой:

config.tf
terraform {
  required_providers {
    ovirt = {
      source  = "ovirt/ovirt"
      version = ">= 2.1.5"
    }
  }
}
vars.tf
variable "cluster" {}
variable "template" {}
variable "vnic" {}

variable "vm_name" {}
variable "vm_clone" {
  default = false
}
variable "vm_power_state" {
  default = "up"
}
variable "vm_cpu_sockets" {
  default = 1
}
variable "vm_cpu_cores" {
  default = 1
}
variable "vm_cpu_threads" {
  default = 1
}
variable "vm_memory" {
  default = 2048
}
variable "vm_maximum_memory" {
  default = 2048
}
variable "vm_boot_disk_size" {
  default = 20
}
variable "vm_os_type" {
  default = "other_linux"
}
variable "vm_disks" {}
variable "vm_nic_name" {
  default = "1"
}
datasources.tf
data "ovirt_cluster" "cluster" {
  name = var.cluster
}
data "ovirt_templates" "template" {
  name = var.template
}
data "ovirt_vnic_profile" "vnic" {
  name = var.vnic
}
data "ovirt_storage_domain" "storages" {
  for_each = { for index, disk in var.vm_disks : disk.alias => disk }
  name     = each.value.storage
}
data "ovirt_wait_for_ip" "wait_ip" {
  vm_id = ovirt_zvm.example3_vm.id

  depends_on = [ovirt_znic_attachment.example3_vnic]
}
main.tf
resource "ovirt_zdisk" "example3_disks" {
  for_each          = { for index, disk in var.vm_disks : disk.alias => disk }
  alias             = each.value.alias
  size              = each.value.size * pow(1024, 3)
  format            = each.value.format
  sparse            = each.value.sparse
  storage_domain_id = data.ovirt_storage_domain.storages[each.key].id
}

resource "ovirt_zvm" "example3_vm" {
  name                      = var.vm_name
  clone                     = var.vm_clone
  cluster_id                = data.ovirt_cluster.cluster.id
  template_id               = tolist(data.ovirt_templates.template.templates)[0].id
  state                     = var.vm_power_state
  cpu_sockets               = var.vm_cpu_sockets
  cpu_cores                 = var.vm_cpu_cores
  cpu_threads               = var.vm_cpu_threads
  memory                    = var.vm_memory * pow(1024, 2)
  maximum_memory            = var.vm_maximum_memory * pow(1024, 2)
  boot_disk_size            = var.vm_boot_disk_size * pow(1024, 3)
  placement_policy_host_ids = []
  os_type                   = var.vm_os_type
}

resource "ovirt_zdisk_attachment" "example3_disks_attach" {
  for_each       = { for index, disk in var.vm_disks : disk.alias => disk }
  vm_id          = ovirt_zvm.example3_vm.id
  disk_interface = each.value.interface
  disk_id        = ovirt_zdisk.example3_disks[each.key].id
}

resource "ovirt_znic_attachment" "example3_vnic" {
  name            = var.vm_nic_name
  vm_id           = ovirt_zvm.example3_vm.id
  vnic_profile_id = data.ovirt_vnic_profile.vnic.id
}
output.tf
output "vm_id" {
  value = ovirt_zvm.example3_vm.id
}
output "vm_ipv4" {
  value = element(tolist(tolist(data.ovirt_wait_for_ip.wait_ip.interfaces)[0].ipv4_addresses), 0)
}
output "vm_disks" {
  value = ovirt_zdisk.example3_disks
}

После создания модуля его можно использовать в других проектах. Например создадим проект в директории /root/example3/project/ со следующей структурой:

config.tf
terraform {
  required_providers {
    ovirt = {
      source  = "ovirt/ovirt"
      version = ">= 2.1.5"
    }
  }
}

provider "ovirt" {
  url          = "https://${var.zvirt_hostname}/ovirt-engine/api"
  username     = var.zvirt_username
  password     = var.zvirt_password
  tls_insecure = true
}
vars.tf
variable "zvirt_hostname" {}
variable "zvirt_username" {}
variable "zvirt_password" {}
vars.tf
variable "zvirt_hostname" {}
variable "zvirt_username" {}
variable "zvirt_password" {}
vars.auto.tfvars
zvirt_hostname = "engine.example.ru"
zvirt_username = "admin@zvirt@internalsso"
zvirt_password = "P@ssw0rd"
main.tf
module "example3_vm1" {
  source = "/root/example3/modules/vm"

  cluster  = "Default"
  vnic     = "ovirtmgmt"
  template = "alma-linux-9-template"

  vm_name           = "example3-vm1"
  vm_cpu_cores      = 4
  vm_memory         = 8192
  vm_maximum_memory = 8192
  vm_os_type        = "rhel_9x64"

  vm_disks = []
}

module "example3_vm2" {
  source = "/root/example3/modules/vm"

  cluster  = "Default"
  vnic     = "ovirtmgmt"
  template = "alma-linux-9-template"

  vm_name           = "example3-vm2"
  vm_cpu_cores      = 2
  vm_memory         = 4096
  vm_maximum_memory = 4096
  vm_os_type        = "rhel_9x64"

  vm_disks = [
    {
      alias     = "example3_vm2_data"
      size      = 30
      format    = "cow"
      sparse    = true
      storage   = "Data"
      interface = "virtio_scsi"
    },
    {
      alias     = "example3_vm2_home"
      size      = 5
      format    = "cow"
      sparse    = true
      storage   = "Data"
      interface = "virtio_scsi"
    }
  ]
}
output.tf
output "vm1_id" {
  value = module.example3_vm1.vm_id
}
output "vm1_ip" {
  value = module.example3_vm1.vm_ipv4
}

output "vm2_id" {
  value = module.example3_vm2.vm_id
}
output "vm2_ip" {
  value = module.example3_vm2.vm_ipv4
}
output "vm2_disks" {
  value = module.example3_vm2.vm_disks
}

Пример вывода результата

vm1_id = "857b1823-61c1-46b1-a8dc-a4c31529bc6a"
vm1_ip = "10.0.0.12"
vm2_id = "92e57204-6b9b-4752-b3a7-d052d8d41858"
vm2_ip = "10.0.0.13"
vm2_disks = {
  "exampl3_vm2_home" = {
    "alias" = "example3_vm2_home"
    "format" = "cow"
    "id" = "4ff860fe-fc84-4f2e-a3cb-ccd082894ea6"
    "size" = 5368709120
    "sparse" = true
    "status" = "ok"
    "storage_domain_id" = "ae8de7e5-e2b7-4027-9bd0-09e0bda0d168"
    "timeouts" = null /* object */
    "total_size" = 197120
  }
  "example3_vm2_data" = {
    "alias" = "example3_vm2_data"
    "format" = "cow"
    "id" = "8bfd09a2-e09e-41c3-af66-26e20cdcab02"
    "size" = 32212254720
    "sparse" = true
    "status" = "ok"
    "storage_domain_id" = "ae8de7e5-e2b7-4027-9bd0-09e0bda0d168"
    "timeouts" = null /* object */
    "total_size" = 197120
  }
}