3forges / packman

A Recipe to Terraform a Virtualbox VM, with Docker, Docker Compose on Debian
2 stars 2 forks source link

Static IP for bridge adapter #8

Open CyraxNova opened 3 months ago

CyraxNova commented 3 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

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

P.s. Add to https://github.com/3forges/packman/blob/master/terraform/README.md. If you want :)

Jean-Baptiste-Lasselle commented 3 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)
}

P.s. Add to https://github.com/3forges/packman/blob/master/terraform/README.md. If you want :)

Oh, thank you Very much for suggesing a quick way to add the VBox Guest Addintions with a cloud init feature in my recipe:

CyraxNova commented 3 months ago

in you PWD, when you run cloud init, you don't have any other file than the 3 you cat above ?

Only 3 files. About cloud-init https://cloudinit.readthedocs.io/en/22.3/topics/datasources/nocloud.html#datasource-nocloud

the boot sequence of your terraformed VMis not set to boot on the CD/DVD, right, and its not possible to set boot order afair, with the terraframr provider, so you do the boot manually ?

Used optical_disks = and didn't configure anything else.

and last but not least: where's your VBox Guest Additions added in your VM ? Does your cloud init setup help at all with that?

The VBox guest add-ons I added to the gold image earlier. I added them and repackage the golden image (manually once).