hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
612 stars 449 forks source link

Terraform does not create two servers, just rename the new configuration #1273

Closed ghost closed 3 years ago

ghost commented 3 years ago
data "vsphere_datacenter" "dc" {
  name          = "esxi"
}

data "vsphere_datastore" "datastore" {
  name          = "datastore1"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_resource_pool" "pool" {
  name          = "Resources"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_network" "network" {
  name          = "VM Network"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_virtual_machine" "template" {
  name          = "plantilla-debian-10"
  datacenter_id = data.vsphere_datacenter.dc.id

}

resource "vsphere_virtual_machine" "vm" {
  name         = "Apuntes"
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id
  num_cpus         = 1
  memory           = 1024
  firmware         = "efi"
  guest_id         = data.vsphere_virtual_machine.template.guest_id
  scsi_type        = data.vsphere_virtual_machine.template.scsi_type
  network_interface {
    network_id     = data.vsphere_network.network.id
    adapter_type   = data.vsphere_virtual_machine.template.network_interface_types[0]
  }
  disk {
    label = "disk0"
    size = data.vsphere_virtual_machine.template.disks.0.size
    eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned

  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template.id
    linked_clone = false
    timeout = 15
    customize {
     linux_options {
         host_name = "Aapuntes"
         domain = ""
    }
         network_interface {
           ipv4_address = "192.168.1.20"
           ipv4_netmask = "24"

         }
        ipv4_gateway = "192.168.1.1"
        dns_server_list = ["192.168.1.5"]
    }
  }
}

I create the server but when I go to create another I modify the file the ip and name 192.168.1.20--->192.168.1.30 and name Aapuntes---->Aproxy but it is not creating a new vm for me, it only modifies the existing one as it would have to be added in the config to create new virtual machines ???

jweigand commented 3 years ago

This is a core of how Terraform itself functions, not something that can be changed by the vsphere provider. Terraform effectively stores information about your VM in state by referencing the "vm" portion of the resource name resource "vsphere_virtual_machine" "vm". When you change the name and ip attributes of that reference, you're telling Terraform to continue to only have a single VM it references as "vm", but change it to have this name and IP instead.

If you want to add a second VM, you'd either need to add a count or for_each attribute to your existing block, or do a new resource block to define the second VM resource "vsphere_virtual_machine" "second_vm".

ghost commented 3 years ago

thanks

ghost commented 3 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!