terra-farm / terraform-provider-virtualbox

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

NAT interface have equal IP address on all virtualbox machine #75

Open patsevanton opened 5 years ago

patsevanton commented 5 years ago

Box https://vagrantcloud.com/geerlingguy/boxes/centos7/versions/1.2.16/providers/virtualbox.box

Terraform config

resource "virtualbox_vm" "node" {
  count     = 3
  name      = format("node%02d", count.index + 1)
  image     = "virtualbox.box"
  cpus      = 1
  memory    = "2048 mib"

  network_adapter {
    type = "nat"
  }

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

}

terraform show

# virtualbox_vm.node[0]:
resource "virtualbox_vm" "node" {
    cpus   = 1
    id     = "8bf98ea2-8492-45d5-979e-8ad8938bf2c2"
    image  = "virtualbox.box"
    memory = "2.0 gib"
    name   = "node01"
    status = "running"

    network_adapter {
        device                 = "IntelPro1000MTServer"
        ipv4_address           = "10.0.2.15"
        ipv4_address_available = "yes"
        mac_address            = "08002785549C"
        status                 = "up"
        type                   = "nat"
    }
    network_adapter {
        device                 = "IntelPro1000MTServer"
        host_interface         = "vboxnet1"
        ipv4_address           = "192.168.57.3"
        ipv4_address_available = "yes"
        mac_address            = "080027265D28"
        status                 = "up"
        type                   = "hostonly"
    }
}

# virtualbox_vm.node[1]:
resource "virtualbox_vm" "node" {
    cpus   = 1
    id     = "620a7195-4e1b-49be-8295-13739352f4d4"
    image  = "virtualbox.box"
    memory = "2.0 gib"
    name   = "node02"
    status = "running"

    network_adapter {
        device                 = "IntelPro1000MTServer"
        ipv4_address           = "10.0.2.15"
        ipv4_address_available = "yes"
        mac_address            = "08002795B91B"
        status                 = "up"
        type                   = "nat"
    }
    network_adapter {
        device                 = "IntelPro1000MTServer"
        host_interface         = "vboxnet1"
        ipv4_address           = "192.168.57.6"
        ipv4_address_available = "yes"
        mac_address            = "080027F6A8B9"
        status                 = "up"
        type                   = "hostonly"
    }
}

# virtualbox_vm.node[2]:
resource "virtualbox_vm" "node" {
    cpus   = 1
    id     = "ab2e09b6-8993-4445-afd5-467beecca59d"
    image  = "virtualbox.box"
    memory = "2.0 gib"
    name   = "node03"
    status = "running"

    network_adapter {
        device                 = "IntelPro1000MTServer"
        ipv4_address           = "10.0.2.15"
        ipv4_address_available = "yes"
        mac_address            = "08002724A15B"
        status                 = "up"
        type                   = "nat"
    }
    network_adapter {
        device                 = "IntelPro1000MTServer"
        host_interface         = "vboxnet1"
        ipv4_address           = "192.168.57.5"
        ipv4_address_available = "yes"
        mac_address            = "080027F9123D"
        status                 = "up"
        type                   = "hostonly"
    }
}
patsevanton commented 5 years ago

node3

[root@node3 ~]# ip a
3: enp0s17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:24:a1:5b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s17
       valid_lft 81822sec preferred_lft 81822sec
    inet6 fe80::ffc3:fb7e:5052:c8bd/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

node2

[root@node2 ~]# ip a
3: enp0s17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:95:b9:1b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s17
       valid_lft 81806sec preferred_lft 81806sec
    inet6 fe80::9a82:5804:2bcf:571b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
patsevanton commented 5 years ago

How Add NATNetwork interface?

patsevanton commented 5 years ago

https://www.virtualbox.org/manual/ch06.html#network_nat_service https://www.virtualbox.org/sdkref/interface_i_network_adapter.html https://www.virtualbox.org/sdkref/interface_i_network_adapter.html#a7cdb4cf56cb04b1b31f36e57bb15c3fe https://www.virtualbox.org/manual/ch08.html#vboxmanage-natnetwork

ringods commented 5 years ago

@patsevanton we know there is still a lot missing. At the moment we only have a vm resource. We should add quite a bit more datasources and resources to this provider to make everything much more workable and configurable.

patsevanton commented 5 years ago

How printf variable attr - https://github.com/patsevanton/terraform-provider-virtualbox/blob/master/virtualbox/resource_vm.go#L512 ?

patsevanton commented 5 years ago

change netTfToVbox func

func netTfToVbox(d *schema.ResourceData) ([]vbox.NIC, error) {
    tfToVboxNetworkType := func(attr string) (vbox.NICNetwork, error) {
        fmt.Println("WARNING: ------------------------ attr")
        fmt.Println(attr)
        switch attr {
        case "bridged":
            return vbox.NICNetBridged, nil
        case "nat":
            return vbox.NICNetNAT, nil
        case "hostonly":
            return vbox.NICNetHostonly, nil
        case "internal":
            return vbox.NICNetInternal, nil
        case "generic":
            return vbox.NICNetGeneric, nil
        default:
            mydata := []byte(netType)
            err := ioutil.WriteFile("tfToVboxNetworkType.txt", mydata, 0777)
            if err != nil {
                fmt.Println(err)
            }
            return ""
        }
    }

But i dont find tfToVboxNetworkType.txt

patsevanton commented 5 years ago

Different between

        case "hostonly":
            return vbox.NICNetHostonly, nil

and

        case "natnetwork":
            return vbox.NICNetNatNetwork, nil

Output command:

--nic1  --nictype1 82545EM --cableconnected1 on

and

--nic1 hostonly --nictype1 82545EM --cableconnected1 on --hostonlyadapter1 vboxnet0
patsevanton commented 5 years ago

I created pull request https://github.com/terra-farm/go-virtualbox/pull/11

But error is same:

2019-08-27T22:22:52.506+0600 [DEBUG] plugin.terraform-provider-virtualbox: pid-5351-vbm.go:45: executing: VBoxManage modifyvm ansible --firmware bios --bioslogofadein off --bioslogofadeout off --bioslogodisplaytime 0 --biosbootmenu disabled --ostype Linux_64 --cpus 1 --memory 256 --vram 20 --acpi on --ioapic on --rtcuseutc on --cpuhotplug off --pae on --longmode on --hpet off --hwvirtex on --triplefaultreset off --nestedpaging on --largepages on --vtxvpid on --vtxux on --accelerate3d off --boot1 disk --boot2 none --boot3 none --boot4 none --nic1  --nictype1 82545EM --cableconnected1 on
2019-08-27T22:22:52.573+0600 [DEBUG] plugin.terraform-provider-virtualbox: pid-5351-utils.go:12: [ERROR] Setup VM properties: exit status 1
2019/08/27 22:22:52 [DEBUG] virtualbox_vm.ansible[0]: apply errored, but we're indicating that via the Error pointer rather than returning it: [ERROR] Setup VM properties: exit status 1
patsevanton commented 4 years ago

Hello!

@ringods can you check code https://github.com/isage/terraform-provider-virtualbox ? Thanks