terra-farm / terraform-provider-virtualbox

VirtualBox provider for Terraform
https://terra-farm.github.io/provider-virtualbox/
MIT License
323 stars 136 forks source link

Creating virtualbox machine on linux failed #105

Closed behradeslamifar closed 2 years ago

behradeslamifar commented 2 years ago

Test environment: Terraform: v1.0.3 OS: Ubuntu 20.04 Virtualbox: 6.1.18-142142~Ubuntu~eoan

This is my main.tf file `terraform { required_providers { virtualbox = { source = "terra-farm/virtualbox" version = "0.2.1-alpha.1" } } }

resource "virtualbox_vm" "node" { count = 1 name = "node" image = "/home/user/terraform/virtualbox.box" cpus = 2 memory = "512 mib" status = "running"

network_adapter { type = "bridge" host_interface = "wlp0s20f3" device = "IntelPro1000MTDesktop" } }`

terraform error: ╷ │ Error: [ERROR] Setup VM properties: exit status 1 │ │ with virtualbox_vm.node[0], │ on main.tf line 10, in resource "virtualbox_vm" "node": │ 10: resource "virtualbox_vm" "node" { │ ╵ 2021-07-26T08:19:02.553+0430 [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info 2021-07-26T08:19:02.553+0430 [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock 2021-07-26T08:19:02.553+0430 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2021-07-26T08:19:02.554+0430 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=8063 2021-07-26T08:19:02.554+0430 [DEBUG] provider: plugin exited

Virtualbox errors: 00:00:00.983692 GUI: UIMachineLogicNormal::sltCheckForRequestedVisualStateType: Requested-state=0, Machine-state=5 00:00:31.821611 GUI: UIFrameBufferPrivate::performResize: Size=720x400, Using fallback buffer since no source bitmap is provided 00:00:31.996285 GUI: Request to close Runtime UI because VM is powered off already. 00:00:31.996335 GUI: Passing request to close Runtime UI from machine-logic to UI session. 00:00:32.473771 ERROR [COM]: aRC=NS_ERROR_UNEXPECTED (0x8000ffff) aIID={c0447716-ff5a-4795-b57a-ecd5fffa18a4} aComponent={SessionWrap} aText={The session is not locked (session state: Unlocked)}, preserve=false aResultDetail=0 00:00:32.475982 ERROR [COM]: aRC=NS_ERROR_UNEXPECTED (0x8000ffff) aIID={c0447716-ff5a-4795-b57a-ecd5fffa18a4} aComponent={SessionWrap} aText={The session is not locked (session state: Unlocked)}, preserve=false aResultDetail=0

migmolrod commented 2 years ago

Same here with a slightly different environment: Test environment: Terraform: v1.0.3 OS: Arch Linux (uname -r = 5.13.6-arch1-1) Virtualbox: 6.1.26 r145957

My tf config file is a copy&paste from the provider documentation. And the error is exactly the same: error-tf-virtualbox

VoyTechnology commented 2 years ago

Can you try running terraform again with the TF_LOG=DEBUG environment variable set? I think there was some problem with output not being passed to terraform, but visible in stderr from the go-virtualbox command, and the DEBUG also should log the output from the provider directly.

migmolrod commented 2 years ago

Can you try running terraform again with the TF_LOG=DEBUG environment variable set? I think there was some problem with output not being passed to terraform, but visible in stderr from the go-virtualbox command, and the DEBUG also should log the output from the provider directly.

With TF_LOG=DEBUG, I got this output: https://pastebin.com/N72W1yy7 I can't see anything that hints what the error is.

abathur commented 2 years ago

I'm seeing the same basic thing on macOS. Log attached:

virtualbox_vm_error.log

behradeslamifar commented 2 years ago

`$ TF_LOG=DEBUG terraform apply -auto-approve 2021-08-06T12:43:00.676+0430 [DEBUG] Adding temp file log sink: /tmp/terraform-log801561981 2021-08-06T12:43:00.676+0430 [INFO] Terraform version: 1.0.3 2021-08-06T12:43:00.676+0430 [INFO] Go runtime version: go1.16.4 2021-08-06T12:43:00.677+0430 [INFO] CLI args: []string{"/usr/bin/terraform", "apply", "-auto-approve"} 2021-08-06T12:43:00.677+0430 [DEBUG] Attempting to open CLI config file: /home/behrad/.terraformrc 2021-08-06T12:43:00.677+0430 [DEBUG] File doesn't exist, but doesn't need to. Ignoring. 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /home/behrad/.terraform.d/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /home/behrad/.local/share/terraform/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /usr/share/i3/terraform/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins 2021-08-06T12:43:00.677+0430 [DEBUG] ignoring non-existing provider search directory /var/lib/snapd/desktop/terraform/plugins 2021-08-06T12:43:00.677+0430 [INFO] CLI command args: []string{"apply", "-auto-approve"} 2021-08-06T12:43:00.678+0430 [DEBUG] New state was assigned lineage "5efda27e-eaed-620a-5797-06eb0a03cc17" 2021-08-06T12:43:00.744+0430 [DEBUG] checking for provisioner in "." 2021-08-06T12:43:00.751+0430 [DEBUG] checking for provisioner in "/usr/bin" 2021-08-06T12:43:00.751+0430 [DEBUG] checking for provisioner in ".terraform/plugins/linux_amd64" 2021-08-06T12:43:00.752+0430 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory 2021-08-06T12:43:00.752+0430 [INFO] backend/local: starting Apply operation 2021-08-06T12:43:00.753+0430 [DEBUG] created provider logger: level=debug 2021-08-06T12:43:00.753+0430 [INFO] provider: configuring client automatic mTLS 2021-08-06T12:43:00.797+0430 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 args=[.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1] 2021-08-06T12:43:00.798+0430 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180566 2021-08-06T12:43:00.798+0430 [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 2021-08-06T12:43:00.806+0430 [INFO] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: configuring server automatic mTLS: timestamp=2021-08-06T12:43:00.806+0430 2021-08-06T12:43:00.840+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: plugin address: address=/tmp/plugin026299425 network=unix timestamp=2021-08-06T12:43:00.840+0430 2021-08-06T12:43:00.840+0430 [DEBUG] provider: using plugin: version=5 2021-08-06T12:43:00.899+0430 [DEBUG] No provider meta schema returned 2021-08-06T12:43:00.899+0430 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2021-08-06T12:43:00.900+0430 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180566 2021-08-06T12:43:00.900+0430 [DEBUG] provider: plugin exited 2021-08-06T12:43:00.900+0430 [INFO] terraform: building graph: GraphTypeValidate 2021-08-06T12:43:00.900+0430 [DEBUG] ProviderTransformer: "virtualbox_vm.node" (terraform.NodeValidatableResource) needs provider["registry.terraform.io/terra-farm/virtualbox"] 2021-08-06T12:43:00.901+0430 [DEBUG] ReferenceTransformer: "virtualbox_vm.node" references: [] 2021-08-06T12:43:00.901+0430 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/terra-farm/virtualbox\"]" references: [] 2021-08-06T12:43:00.901+0430 [DEBUG] Starting graph walk: walkValidate 2021-08-06T12:43:00.901+0430 [DEBUG] created provider logger: level=debug 2021-08-06T12:43:00.901+0430 [INFO] provider: configuring client automatic mTLS 2021-08-06T12:43:00.945+0430 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 args=[.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1] 2021-08-06T12:43:00.946+0430 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180575 2021-08-06T12:43:00.946+0430 [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 2021-08-06T12:43:00.953+0430 [INFO] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: configuring server automatic mTLS: timestamp=2021-08-06T12:43:00.953+0430 2021-08-06T12:43:00.985+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: plugin address: address=/tmp/plugin208250846 network=unix timestamp=2021-08-06T12:43:00.985+0430 2021-08-06T12:43:00.985+0430 [DEBUG] provider: using plugin: version=5 2021-08-06T12:43:01.045+0430 [DEBUG] No provider meta schema returned 2021-08-06T12:43:01.046+0430 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2021-08-06T12:43:01.047+0430 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180575 2021-08-06T12:43:01.047+0430 [DEBUG] provider: plugin exited 2021-08-06T12:43:01.047+0430 [INFO] backend/local: apply calling Plan 2021-08-06T12:43:01.047+0430 [INFO] terraform: building graph: GraphTypePlan 2021-08-06T12:43:01.047+0430 [DEBUG] ProviderTransformer: "virtualbox_vm.node (expand)" (terraform.nodeExpandPlannableResource) needs provider["registry.terraform.io/terra-farm/virtualbox"] 2021-08-06T12:43:01.047+0430 [DEBUG] ReferenceTransformer: "virtualbox_vm.node (expand)" references: [] 2021-08-06T12:43:01.047+0430 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/terra-farm/virtualbox\"]" references: [] 2021-08-06T12:43:01.048+0430 [DEBUG] Starting graph walk: walkPlan 2021-08-06T12:43:01.051+0430 [DEBUG] created provider logger: level=debug 2021-08-06T12:43:01.051+0430 [INFO] provider: configuring client automatic mTLS 2021-08-06T12:43:01.093+0430 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 args=[.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1] 2021-08-06T12:43:01.093+0430 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180584 2021-08-06T12:43:01.093+0430 [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 2021-08-06T12:43:01.101+0430 [INFO] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: configuring server automatic mTLS: timestamp=2021-08-06T12:43:01.101+0430 2021-08-06T12:43:01.134+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: plugin address: address=/tmp/plugin283175077 network=unix timestamp=2021-08-06T12:43:01.134+0430 2021-08-06T12:43:01.134+0430 [DEBUG] provider: using plugin: version=5 2021-08-06T12:43:01.196+0430 [DEBUG] No provider meta schema returned 2021-08-06T12:43:01.197+0430 [DEBUG] Resource instance state not found for node "virtualbox_vm.node[0]", instance virtualbox_vm.node[0] 2021-08-06T12:43:01.198+0430 [DEBUG] ReferenceTransformer: "virtualbox_vm.node[0]" references: [] 2021-08-06T12:43:01.198+0430 [DEBUG] refresh: virtualbox_vm.node[0]: no state, so not refreshing 2021-08-06T12:43:01.200+0430 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2021-08-06T12:43:01.201+0430 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180584 2021-08-06T12:43:01.201+0430 [DEBUG] provider: plugin exited

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

virtualbox_vm.node[0] will be created

Plan: 1 to add, 0 to change, 0 to destroy. 2021-08-06T12:43:01.202+0430 [INFO] backend/local: apply calling Apply 2021-08-06T12:43:01.202+0430 [INFO] terraform: building graph: GraphTypeApply 2021-08-06T12:43:01.202+0430 [DEBUG] Resource state not found for node "virtualbox_vm.node[0]", instance virtualbox_vm.node[0] 2021-08-06T12:43:01.202+0430 [DEBUG] ProviderTransformer: "virtualbox_vm.node (expand)" (terraform.nodeExpandApplyableResource) needs provider["registry.terraform.io/terra-farm/virtualbox"] 2021-08-06T12:43:01.202+0430 [DEBUG] ProviderTransformer: "virtualbox_vm.node[0]" (terraform.NodeApplyableResourceInstance) needs provider["registry.terraform.io/terra-farm/virtualbox"] 2021-08-06T12:43:01.202+0430 [DEBUG] ReferenceTransformer: "virtualbox_vm.node (expand)" references: [] 2021-08-06T12:43:01.202+0430 [DEBUG] ReferenceTransformer: "virtualbox_vm.node[0]" references: [] 2021-08-06T12:43:01.202+0430 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/terra-farm/virtualbox\"]" references: [] 2021-08-06T12:43:01.203+0430 [DEBUG] Starting graph walk: walkApply 2021-08-06T12:43:01.203+0430 [DEBUG] created provider logger: level=debug 2021-08-06T12:43:01.203+0430 [INFO] provider: configuring client automatic mTLS 2021-08-06T12:43:01.246+0430 [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 args=[.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1] 2021-08-06T12:43:01.247+0430 [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180593 2021-08-06T12:43:01.247+0430 [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 2021-08-06T12:43:01.254+0430 [INFO] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: configuring server automatic mTLS: timestamp=2021-08-06T12:43:01.254+0430 2021-08-06T12:43:01.287+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: plugin address: address=/tmp/plugin272971827 network=unix timestamp=2021-08-06T12:43:01.287+0430 2021-08-06T12:43:01.287+0430 [DEBUG] provider: using plugin: version=5 2021-08-06T12:43:01.349+0430 [DEBUG] No provider meta schema returned virtualbox_vm.node[0]: Creating... 2021-08-06T12:43:01.352+0430 [INFO] Starting apply for virtualbox_vm.node[0] 2021-08-06T12:43:01.352+0430 [DEBUG] virtualbox_vm.node[0]: applying the planned Create change 2021-08-06T12:43:09.313+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: pid-180593-resource_vm.go:603: [DEBUG] Network adapter: {Network: Hardware:82540EM HostInterface: MacAddr:} 2021-08-06T12:43:09.373+0430 [DEBUG] provider.terraform-provider-virtualbox_v0.2.1-alpha.1: pid-180593-utils.go:12: [ERROR] Setup VM properties: exit status 1 ╷ │ Error: [ERROR] Setup VM properties: exit status 1 │ │ with virtualbox_vm.node[0], │ on main-vbox.tf line 10, in resource "virtualbox_vm" "node": │ 10: resource "virtualbox_vm" "node" { │ ╵ 2021-08-06T12:43:09.379+0430 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2021-08-06T12:43:09.380+0430 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/terra-farm/virtualbox/0.2.1-alpha.1/linux_amd64/terraform-provider-virtualbox_v0.2.1-alpha.1 pid=180593 2021-08-06T12:43:09.380+0430 [DEBUG] provider: plugin exited`

dash82 commented 2 years ago

Same issue here: On ubuntu 20.04 VirtualBox Version 6.1.26 r145957 Terraform v1.0.4 on linux_amd64

not finding anything that points me in the direction of a solution...

debug.txt

mrtimp commented 2 years ago

I'm having the same issue on macOS Big Sur 11.4 (20F71) with Terraform 1.0.5. It seems to be failing at the same place:

https://github.com/terra-farm/terraform-provider-virtualbox/blob/1abdb53f542366907cd01b3344c80b672ae8aef2/virtualbox/resource_vm.go#L325

and the exit status 1 feels like maybe a required command line parameter isn't been supplied or is conflicting or is an unsupported type maybe in here somewhere:

https://github.com/terra-farm/go-virtualbox/blob/6181dcc75a32f9fc3b3021f4ea74e9f9d91e35b3/machine.go#L360-L413

I'm not really sure how to debug further as TF_LOG=DEBUG doesn't seem to yield anything more detailed.

mrtimp commented 2 years ago

I've done some more debugging on this today and it looks like it is the --synthcpu in machine.go. This looks to have been removed in a commit:

https://github.com/terra-farm/go-virtualbox/commit/cba2eea143c179ca0f5b80bfec3d12b1a2db2020

but maybe a new tagged version of the go-virtualbox library is required?

framegrace commented 2 years ago

While this is not fixed, have a workaround for this issue.

Just add this wrapper script on some PATH previous to the one VBoxManage is in (Mine is in /usr/sbin, and my $PATH variable has /usr/local/bin before), so it gets executed by default instead of the real one:

cat /usr/local/sbin/VBoxManage:

#!/bin/bash
declare -a ARGS
for var in "$@"; do
    # Ignore known bad arguments
    if [ "$var" = '--synthcpu' ]; then
    DNEXT=1
        continue
    fi
    if [ ! -z "${DNEXT}" ]; then
    DNEXT=""
    continue
    fi
    ARGS[${#ARGS[@]}]="$var"
done
/usr/bin/VBoxManage "${ARGS[@]}"

It simply removes the --synthcpu off option from the arguments and passes them to the real VBoxManage executable.

mrtimp commented 2 years ago

Great idea @framegrace! Thanks for sharing.

leodotcloud commented 2 years ago

I have built a custom TF provider using the above PR. Now I see the following error:

│ Error: [ERROR] Wait VM until ready: waiting for VM (node-01) to become ready: [ERROR] can't convert vbox network to terraform data: No match with get guestproperty output
│
│   with virtualbox_vm.node[0],
│   on infra.tf line 12, in resource "virtualbox_vm" "node":
│   12: resource "virtualbox_vm" "node" {

After creating a test VM using vagrant, I see that VM created using this TF Virtualbox provider has the SATA disks ordered incorrectly. If I swap the SATA ports, the same VM boots with out any error.

Screen Shot 2021-09-19 at 7 10 21 PM Screen Shot 2021-09-19 at 7 12 40 PM Screen Shot 2021-09-19 at 7 09 47 PM
leodotcloud commented 2 years ago

https://github.com/terra-farm/terraform-provider-virtualbox/blob/v0.2.1-alpha.1/virtualbox/image.go#L66

Glob is picking up the files in random order, causing the configdrive to be placed in SATA port 0. This is causing the boot failure.

VoyTechnology commented 2 years ago

@leodotcloud Thank you for having an in-depth look at it. The change which includes the updated go-virtualbox change is now merged, and hopefully I have resolved your issue in https://github.com/terra-farm/terraform-provider-virtualbox/pull/109.

Since you already have the whole environment ready, would you mind confirming it works and letting me know how you get on? You can either comment here or under https://github.com/terra-farm/terraform-provider-virtualbox/pull/109

leodotcloud commented 2 years ago

@VoyTechnology Thank you for the PR, tested it locally and it works. Now I have a different problem, I will create a different issue instead of spamming this one.

VoyTechnology commented 2 years ago

I am not sure have I solved everybody's issues, but please try and use the provider again using version 0.2.2-alpha.1

leodotcloud commented 2 years ago

Works for me. Thanks a lot, @VoyTechnology for the quick turnaround.

infra.tf

terraform {
  required_providers {
    virtualbox = {
      source = "terra-farm/virtualbox"
      version = "0.2.2-alpha.1"
    }
  }
}

resource "virtualbox_vm" "node" {
  count     = 1
  name      = format("node-%02d", count.index + 1)
  image     = "https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20210916.0.0/providers/virtualbox.box"
  cpus      = 2
  memory    = "4096 mib"
  #user_data = file("user_data")

  network_adapter {
    type           = "hostonly"
    host_interface = "vboxnet0"
  }
}

output "IPAddr" {
  value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 1)
}

output "IPAddr_2" {
  value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 2)
}

Output:

Terraform will perform the following actions:

  # virtualbox_vm.node[0] will be created
  + resource "virtualbox_vm" "node" {
      + cpus   = 2
      + id     = (known after apply)
      + image  = "https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20210916.0.0/providers/virtualbox.box"
      + memory = "4096 mib"
      + name   = "node-01"
      + status = "running"

      + network_adapter {
          + device                 = "IntelPro1000MTServer"
          + host_interface         = "vboxnet0"
          + ipv4_address           = (known after apply)
          + ipv4_address_available = (known after apply)
          + mac_address            = (known after apply)
          + status                 = (known after apply)
          + type                   = "hostonly"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + IPAddr   = (known after apply)
  + IPAddr_2 = (known after apply)
virtualbox_vm.node[0]: Creating...
virtualbox_vm.node[0]: Still creating... [10s elapsed]
virtualbox_vm.node[0]: Still creating... [20s elapsed]
virtualbox_vm.node[0]: Still creating... [30s elapsed]
virtualbox_vm.node[0]: Still creating... [40s elapsed]
virtualbox_vm.node[0]: Still creating... [50s elapsed]
virtualbox_vm.node[0]: Still creating... [1m0s elapsed]
virtualbox_vm.node[0]: Still creating... [1m10s elapsed]
virtualbox_vm.node[0]: Creation complete after 1m17s [id=0fd22827-5bde-40c7-a578-012b08ca87f1]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

IPAddr = "192.168.56.106"
IPAddr_2 = "192.168.56.106"
mrtimp commented 2 years ago

Fantastic @VoyTechnology! I can also confirm that its working now as expected. Thanks for all of your help :-)

migmolrod commented 2 years ago

It works for me as well. Thanks @VoyTechnology!! I also had to rename my nodes from alpine-node-%02d to just node-%02d. Maybe it was a cache thing. The important thing is it works.