vatesfr / terraform-provider-xenorchestra

Xen Orchestra provider for Terraform
MIT License
150 stars 32 forks source link

extract IP address from created VM's #49

Closed dmcanally closed 4 years ago

dmcanally commented 4 years ago

I can't seem to find a way to extract/output the ip address from created VM's.

I am trying something like this...

resource "xenorchestra_vm" "xoa_vm" {
  count        = var.number_of_vms
  auto_poweron = var.auto_poweron
  memory_max   = var.memory_max
  cpus         = var.cpus
  cloud_config = "${xenorchestra_cloud_config.xoa_cc.template}"
  name_label   = "${var.name}${format("%02d", (count.index))}"
  template     = data.xenorchestra_template.template.id
  network {
      network_id = data.xenorchestra_pif.pif.network
  }

  disk {
    sr_id       = "${element(data.xenorchestra_sr.sr.*.uuid, count.index)}"
    name_label  = "${var.name}${format("%02d", (count.index))}"
    size        = var.disk_size
  }
}

output "ip" {
  description = "Virtual Machine IP's"
  value = ["${xenorchestra_vm.xoa_vm.*.vif0_ip}"]
}

Any advice would be greatly appreciated.

ddelnano commented 4 years ago

@dmcanally how are you assigning an IP to the VM? Is it over DHCP or do you have a static IP? If it's the former I don't know that the provider will be able to know the IP address at creation time.

I'm also not well versed with Xenserver/XCP-ng's networking but it does seem like you can assign IPs to VIFs (https://wiki.xenproject.org/wiki/Xen_Networking) but I'm not sure what implications that means for a particular environment's setup (routing, etc).

dmcanally commented 4 years ago

I am using DHCP, Im trying to build a kube on xcp-ng terraform module with as little input as possible. So far I have terraform rke and xenorchestra plugins working pretty well, but the two things I'm not able to pass from one plugin to the other are ip addresses and hostnames.

something like a data resource for virtual machines would work. With this method, I would be able to dynamically grow a kubernetes cluster by just upping the count on some worker pool.

something like this would be cool...

data "xenorchestra_vm" "vm" {
  count = var.number_of_vms
  name_label = "${var.vm_name}${format("%02d", (count.index))}"
}
output "ip" {
  description = "Virtual Machine IP's"
  value = ["${xenorchestra_vm.xoa_vm.*.ip}"]
}
olivierlambert commented 4 years ago

You can't pass an IP to a VIF directly from the hypervisor API. This is a setting meant to be fixed by the VM OS.

However, you can use Cloudinit if your VM template got Cloudinit installed/configured with noCloud source :)

ddelnano commented 4 years ago

Yea I think Cloudinit, configuration management or some other runtime process is appropriate here. With cloudinit you could assign the kube primary nodes static IPs and then the rest of the nodes could have IPs via DHCP.

@dmcanally I think having a vm data source would be a great addition to the provider but unfortunately extracting the IP address from the VM doesn't seem feasible from the apis that this provider uses (XO and hypervisor api).

Most of my professional career has been working with AWS and so I'm aware accessing an instance's ip address through an attribute is common for resources like aws_instance (which is similar to xenorchestra_vm). However, the aws api has that functionality baked into it (returning the IP address of the ec2 instance at creation time).

I'm going to close this since I don't think there is a way to implement this but I'm happy to continue the discussion on how to develop this k8s cluster module either in this GitHub issue or in the Discord for the project.

ddelnano commented 3 years ago

@dmcanally with v0.10.0 of the provider you should be able to specify cloud-init network configuration to launch an instance with a static IP. The latest docs on the terraform registry have more details.

ddelnano commented 3 years ago

Also wanted to follow up that #112 aims to implement this support by leveraging guest tools. @dmcanally please let me know if that would work for your use case!

ddelnano commented 3 years ago

This will be released in v0.15.0 of the provider.