dmacvicar / terraform-provider-libvirt

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

Spin up VMs on multiple libvirt providers #829

Open arunnalpet opened 3 years ago

arunnalpet commented 3 years ago

Hi, On localhost, I am able to deploy and manage multiple VMs this way and its working fine. I have object list of VMs in 'myvms' variable. Using for_each I am planning to deploy VMs listed in 'myvms'. I have a module 'singlevm' which accepts VM attributes, and spins up a single VM. Here is the code, and this is working perfect for me!

provider "libvirt" {
  uri = "qemu:///system"
}

# For each VM declared in terraform.tfvars, deploy a VM
module "kvm_instances" {
  source = "./modules/singlevm"

  for_each = var.myvms
  vmname   = each.value.hostname
  disksize = each.value.osdisk_gb
  datadisksize = each.value.datadisk_gb
  osimage  = each.value.Image
  memory   = each.value.RAM
  vcpu     = each.value.CPU_Count
}

Now, I am trying to extend above code to deploy and manage VMs on multiple libvirt providers. I attempting to have separate modules for deploying VMs on specific host. For each host I have separate variable which holds the list of VMs. What is the best way to set 'provider for each of these modules? (The following code was my attempt, and it doesnt work. It throws up a syntax errror!!)

provider "libvirt" {
  uri = "qemu:///system"
}

provider "libvirt" {
  alias = "remotehost1"
  uri   = "qemu+ssh://root@10.3.20.11/system"
}

provider "libvirt" {
  alias = "remotehost2"
  uri   = "qemu+ssh://root@10.3.20.12/system"
}

# For each VM declared in terraform.tfvars, deploy a VM
module "kvm_instances_host1" {
  source = "./modules/singlevm"

  for_each = var.vm_list_on_host1
  vmname   = each.value.hostname
  disksize = each.value.osdisk_gb
  datadisksize = each.value.datadisk_gb
  osimage  = each.value.Image
  memory   = each.value.RAM
  vcpu     = each.value.CPU_Count
  providers = {
    libvirt = libvirt.remotehost1
  }
}

module "kvm_instances_host2" {
  source = "./modules/singlevm"

  for_each = var.vm_list_on_host2
  vmname   = each.value.hostname
  disksize = each.value.osdisk_gb
  datadisksize = each.value.datadisk_gb
  osimage  = each.value.Image
  memory   = each.value.RAM
  vcpu     = each.value.CPU_Count
  providers = {
    libvirt = libvirt.remotehost2
  }
}

This is the error i get! Not very much helpful. But I see, it doesnt like the 'providers' json within each module!

Error: virError(Code=38, Domain=7, Message='End of file while reading data: Ncat: No such file or directory.: Input/output error')

  on main.tf line 5, in provider "libvirt":
   5: provider "libvirt" {

Please recommend the best approach

nmasse-itix commented 3 years ago

Hello,

Ncat: No such file or directory

Have you installed netcat on the target hypervisors ?

On CentOS/RHEL/Fedora:

sudo dnf install nmap-ncat

Hope it helps.

Nicolas

arunnalpet commented 3 years ago

@nmasse-itix I see I have those packages already installed on all hypervisors.

MalloZup commented 3 years ago

@arunnalpet hi thx for issue. here is an example of the feature, as I can tell the syntax is bit different there https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/examples/v0.13/multiple/main.tf#L28

can you give it try?

If it is failing, you could add

TF_LOG=DEBUG terraform apply and attach somewhere the logs

Thank you :sun_with_face:

scabala commented 2 months ago

Hi @arunnalpet is this issue solved? If so, could you close it?