terra-farm / terraform-provider-virtualbox

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

Static IP Address #23

Open tylerauerbeck opened 6 years ago

tylerauerbeck commented 6 years ago

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.

pyToshka commented 6 years ago

@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

VoyTechnology commented 5 years ago

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.

ringods commented 5 years ago

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.

VoyTechnology commented 5 years ago

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

CyraxNova commented 5 months ago

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)
}