josenk / terraform-provider-esxi

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

Unable add vswitch uplink: Unable to Set: Busy #162

Closed mmanjos closed 2 years ago

mmanjos commented 2 years ago

Description I'm trying to create a basic vswitch with one uplink:

resource "esxi_vswitch" "vSwitch1" {
  name = "vSwitch1"
  uplink {
    name = "vmnic2"
  }
}

The ESXi server is running 7.0 Update 2

After the failed apply, vSwitch1 does get created and remains as a network resource in my ESXi server, but it has no uplinks assigned to it. Re-running the terraform apply afterwards gets the same results (no change)

To Reproduce If I clear any "vSwitch1" configs on the actual esxi server and then terraform apply, I get the following error:

esxi_vswitch.vSwitch1: Creating...
╷
│ Error: Failed to update vswitch: Failed to add vswitch uplink: Unable to Set: Busy
│ Process exited with status 1
│ 
│ 
│ 
│   with esxi_vswitch.vSwitch1,
│   on lab.tf line 22, in resource "esxi_vswitch" "vSwitch1":
│   22: resource "esxi_vswitch" "vSwitch1" {
│ 
╵

Desktop (please complete the following information):

Terraform v1.1.0
on linux_amd64
+ provider registry.terraform.io/josenk/esxi v1.9.1
VMware ovftool 4.4.1 (build-16812187)

Apologies in advance if I'm using the esxi_vswitch resource incorrectly - I'm just getting started with terraform. I can create esxi_guests just fine with my setup, but this vswitch config doesn't work.

mmanjos commented 2 years ago

Here is terraform.state after the failed apply (note the uplink map is empty):

{
  "version": 4,
  "terraform_version": "1.1.0",
  "serial": 1,
  "lineage": "e3363f99-0ca4-70a7-b933-e5531ee85c07",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "esxi_vswitch",
      "name": "vSwitch1",
      "provider": "provider[\"registry.terraform.io/josenk/esxi\"]",
      "instances": [
        {
          "status": "tainted",
          "schema_version": 0,
          "attributes": {
            "forged_transmits": false,
            "id": "vSwitch1",
            "link_discovery_mode": "listen",
            "mac_changes": false,
            "mtu": 1500,
            "name": "vSwitch1",
            "ports": 128,
            "promiscuous_mode": false,
            "uplink": []
          },
          "sensitive_attributes": [],
          "private": "bnVsbA=="
        }
      ]
    }
  ]
}
mmanjos commented 2 years ago

And even outside of terraform, if I run this command directly on the esxi host itself, I get the same error:

[root@lab:~] esxcli network vswitch standard uplink add -u "vmnic2" -v "vSwitch1"
Unable to Set: Busy
mmanjos commented 2 years ago

Ah - darn; found my problem. I really thought vmnic2 was the one I wanted to target, but I was using the wrong interface that was already tied up.

Fixed my typo and now the apply is working great - thanks!

josenk commented 2 years ago

Thanks for the update.... Glad it was an easy fix.

mmanjos commented 2 years ago

Actually, just ran into another issue after it was done; my setup has a vswitch and 5 port groups created by terraform from a completely clean state, followed by a VM that's connected to one of the port groups.

The vswitch, uplink and first port group provisioned fine but for some reason, the following 4 port groups failed to create. The VM that was created at the end was supposed to be attached to one of the 4 port groups that didn't get created, so it was created in a disconnected state.

Re-running terraform apply noticed the missing portgroups needed to be created, and once they were it seemed like they were working but the VM that was created earlier was still disconnected and wasn't automatically re-joined to the right network.

Am I using terraform the right way (and is this a bug?) or should I have two tf files, one for the network environment setup and then one for VMs only after the network is ready?

mmanjos commented 2 years ago

Ah, that works much better :) I just added the appropriate depends_on statements and now everything's nice and happy. Thanks again for your work on developing this module!

josenk commented 2 years ago

If you are properly referring the resources, you should not need to use depends_on...

https://github.com/josenk/terraform-provider-esxi/blob/v1.9.1/examples-0.13/07%20Networking/main.tf#L52

You can use Terraform however you like. If you layer in two separate Terraform files, you will want to load the first layer (network in your case) using the data resource.

https://www.terraform.io/docs/language/data-sources/index.html

mmanjos commented 2 years ago

Ah - thanks. That makes more sense, and I think also explains some issues I had before as well. I've been referencing the resources by name/string not specific terraform reference. Fixing that will probably help a lot.