josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
540 stars 154 forks source link

Gathering data from hypervisor? #165

Closed ELLEation closed 2 years ago

ELLEation commented 2 years ago

Hello,

I am utilizing this project to put my ESXI server to work. However I cannot generate a list of all the hosts utilizing a templatefile and iteration.

For instance in the following I would expect the wildcard to pull all existing guest vm ip addresses and output them to the file below.

I am not entirely sure where I've gone wrong here as I've done similar with AWS and it has worked fine. But I understand this project is quite different.

### main.tf ###
resource "local_file" "ansible_inventory" {
    content = templatefile("inventory.tpl",
        {
            guest_vms = esxi_guest.*.ip_address
        }
   )
    filename = "inventory"
}
### inventory.tpl ###
[guests]
%{ for vm in guest_vms ~}
${vm} 
%{ endfor ~}
jremerich commented 2 years ago

Hi!

I'm not on development team, but may I help you. This project is a resource provider, it manipulates the resource on ESXi. So, what you're trying to do will not work.

Maybe you need to use terraform import to do what you want. I never did this, but I think that you'll need to create something like this:

resource "local_file" "ansible_inventory" {
    content = templatefile("inventory.tpl",
        {
            guest_vms = esxi_guest.vm1.ip_address
        }
   )
    filename = "inventory"
}

resource "esxi_guest" "vm1" {
   guest_name = "NAME OF YOUR VM ON ESXI"
}

So execute terraform import esxi_guest.vm1 and after your plan/apply.

Unfortunelly I don't know how to do this as an array/loop, but may be a start.

In another hand, there is an another project that can help you on your task, take a look: https://github.com/vmware/govmomi/tree/master/govc

With that one you'll be abble to list all vm's running govc find . -type m and get ip with govc vm.ip $vm

I hope that this helps you.

josenk commented 2 years ago

I am utilizing this project to put my ESXI server to work. It's not entirely clear what you mean, but I'll assume you mean that you use terraform-provider-esxi to build all your vms on esxi????

In the README.md. https://github.com/josenk/terraform-provider-esxi#known-issues-with-vmware_esxi

terraform import cannot import the guest disk type (thick, thin, etc) if the VM is powered on and cannot import the guest ip_address if it's powered off.

Something to keep in mind is that the IP address is only available if the VM is up and it's running vmware tools.

BTW: The ip_address can be stored in a file when you build the system. (if you save it using outputs... See example 06. Then you would simply run these commands to get the current ip address from the build.

Terraform can do it... But, as jremerich pointed out there are other tools that might be better. I don't believe there's any issue with terraform-provider-esxi.