terraform-provider-virtualbox icon indicating copy to clipboard operation
terraform-provider-virtualbox copied to clipboard

Static IP Address

Open tylerauerbeck opened this issue 6 years ago • 5 comments

Does the Virtualbox provider support configuring a static IP address? I see that other providers are able to do this, so it would make sense to replicate that within this plugin. Just not sure if I'm just looking over it or not.

tylerauerbeck avatar Mar 31 '18 02:03 tylerauerbeck

@tylerauerbeck , at this moment no, if i remember right, virtualbox tool not support setup static ip. By the way you can use cloudinit for setup ip address

pyToshka avatar Apr 23 '18 04:04 pyToshka

It might be possible to add an static IP to an interface: http://www.virtualbox.org/manual/ch06.html#nichardware

I will have to play around with it and will report back.

VoyTechnology avatar Jul 19 '19 14:07 VoyTechnology

I think this is related to #64, which I created yesterday. Having the network interfaces as separate resources will allow for more finer grained configuration before they are used in a vm resource.

ringods avatar Jul 29 '19 08:07 ringods

I agree. The functionality can be added as part of #64. I will reassign the issue to you @ringods.

VoyTechnology avatar Jul 29 '19 14:07 VoyTechnology

For Ubuntu we can use cloud-init. After generate iso we can mount from terraform.

  1. Make iso
sudo apt-get install genisoimage

cat meta-data

local-hostname: tpl-ubuntu
instance-id: vmname

cat user-data

#cloud-config
users:
  - name: techuser
    groups: sudo
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh_authorized_keys:
      - ssh-ed25519 AAAAC3Nz....
    shell: /bin/bash
write_files:
  - path: /etc/ssh/sshd_config.d/60-cloudimg-settings.conf
    content: |
       PasswordAuthentication yes
packages:
  - mc
runcmd:
  - [ systemctl, status, ssh ]
final_message: Machine configured

cat network-config

network:
  version: 2
  ethernets:
    enp0s17:
      dhcp4: no
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.100
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
    
genisoimage -input-charset utf-8 -output seed-01.iso -volid cidata -joliet -rock user-data meta-data network-config

#chage ip to 192.168.1.11 in network-config file and make next iso
genisoimage -input-charset utf-8 -output seed-02.iso -volid cidata -joliet -rock user-data meta-data network-config
  1. Edit main.tf
resource "virtualbox_vm" "vmname" {
  count     = 2
  name      = format("vmname-%02d", count.index + 1)
  image     = "https://app.vagrantup.com/ubuntu/boxes/jammy64/versions/20240315.1.0/providers/virtualbox.box"
  cpus      = 2
  memory    = "2.0gib"

  network_adapter {
    type = "bridged"
    host_interface = "Realtek Gaming 2.5GbE Family Controller"
  }

  optical_disks = [
    format("d:\\tmp\\seed-%02d.iso", count.index + 1)
  ]

}

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

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

CyraxNova avatar Mar 23 '24 14:03 CyraxNova