dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.59k stars 458 forks source link

Terraform | libvirt | Guidance requested #835

Closed bitvijays closed 3 years ago

bitvijays commented 3 years ago

Dear terraform-provider-libvirt Team,

Hope you are doing well and had a good weekend. Thank you for creating libvirt provider for terraform much appreciated. Makes the job easier 👍

Need guidance implementing a simple scenario. We are trying to install few virtual machines using terraform on KVM virtualization using libvirt. We want to implement a simple solution where the end-user provides details such as domain_name, domain_memory, domain_cpu, domain_hard_disk_size, pool_name (where the VM Harddisk should be kept) and domain_os (Debian, CentOS, Ubuntu etc).

We have three modules

Pool-Module

To create a pool apart from default defined by the user which contains

File: pool.tf
variable "pools" {
    default = {
        "pool_UMA" = { name = "UMA4", path = "/tmp/UMA4"},
    }
}

# create pool
resource "libvirt_pool" "libvirt_pool_x" {
  for_each = var.pools
  name = each.value.name
  type = "dir"
  path = each.value.path
}

and accepts input in the below format

Download-Cloud-Image Module

To download cloud images defined by the user and store them locally, if not already downloaded

Created a module for

File: download_image.tf
#     images = {"image_debian" = { name = "base_debian_10"   , pool = "OS_Images", source = "https://cdimage.debian.org/cdimage/openstack/current/debian-10-openstack-amd64.qcow2"},}
variable "images"{
  description = "Map for the images"
  type = map
}

variable "pool_images" {
  type = string
}

# Download the OS images from the internet
resource "libvirt_volume" "download_image" {
  for_each = var.images
  name = each.value.name
  pool = each.value.pool
  source = each.value.source
}

VM-Create Module

To create the VM based on the parameters defined by the user and use the base-image downloaded.

Still working on this

Current State

Problem 1:

If we download a newer OS image, terraform kind of deletes and recreates the pool which should not happen.

image

Problem 2:

Currently, in the init.tf, we put

module "libvirt_Images" {
    source    = "./modules/libvirt_images/"
    pool_images = "OS_Images"
    images = {
        # Debian Base OS image
        "base_debian_10"    = { name = "base_debian_10"   , pool = "OS_Images", source = "https://cdimage.debian.org/cdimage/openstack/current/debian-10-openstack-amd64.qcow2"},
        # Ubuntu Base OS image
        "base_ubuntu_20_04" = { name = "base_ubuntu_20_04", pool = "OS_Images", source = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img"},        
        # CentOS Base OS image
        "base_centos_8"     = { name = "base_centos_8"    , pool = "OS_Images", source = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.3.2011-20201204.2.x86_64.qcow2"},        
    }
}

to download the images from the internet. is there a way to create a variable pool_images and store "OS_Imagesin that? as currently, we are repeatedly writingpool = "OS_Imagesin theimages` variable.

We would really appreciate the community help and guidance for creating this. Thank You :)