terraform-provider-libvirt
terraform-provider-libvirt copied to clipboard
Spin up VMs on multiple libvirt providers
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://[email protected]/system"
}
provider "libvirt" {
alias = "remotehost2"
uri = "qemu+ssh://[email protected]/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
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
@nmasse-itix I see I have those packages already installed on all hypervisors.
@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:
Hi @arunnalpet is this issue solved? If so, could you close it?
ℹ️ ℹ️ ℹ️ ℹ️ ℹ️ ℹ️
This issue report is relevant to the legacy version of the provider, which is now in the v0.8 branch of this repository.
Future development is based on a new provider, which is not compatible with this one, nor does share code.
As the new provider solves many issues with the legacy, and to make development in my free time more enjoyable, I have decided to close all bugs for the legacy provider, and to ask to check if the bug would apply to the new one. This also to encourage trying the new version.
- https://registry.terraform.io/providers/dmacvicar/libvirt/latest
and check the documentation:
- https://github.com/dmacvicar/terraform-provider-libvirt/blob/main/README.md
- https://registry.terraform.io/providers/dmacvicar/libvirt/latest/docs
You are free to reopen the bug for v0.8. It may encourage someone to fix it. My efforts will go into the new provider.
I ask you to check the new provider and re-evaluate this bug. 🙏