Telmate / terraform-provider-proxmox

Terraform provider plugin for proxmox
MIT License
2.19k stars 531 forks source link

Incorrect IP address for every vm #477

Closed fabian-born closed 2 years ago

fabian-born commented 2 years ago

Good morning,

I'm still using the proxmox terraform provider to deploy my golden image.

terraform {
  required_providers {
    proxmox = {
      source = "telmate/proxmox"
    }
  }
}

resource "random_pet" "prefix" {}

provider "proxmox" {
    pm_api_url = "https://192.168.10.51:8006/api2/json"
    pm_debug = true
    pm_tls_insecure = true
    pm_user = "terraform@pve"
    pm_password = "my password"
}

resource "proxmox_vm_qemu" "masternodes" {
  count = var.master_node_count
  name = "${random_pet.prefix.id}-m-${count.index + 1}" #count.index starts at 0, so + 1 means this VM will be named test-vm-1 in proxmoxx
  target_node = var.proxmox_host
  clone = var.template_name
  agent = 1
  os_type = "cloud-init"
  cores = 2
  sockets = 1
  cpu = "host"
  memory = var.master_node_ram
  scsihw = "virtio-scsi-pci"
  bootdisk = "scsi0"
  disk {
    slot = 0
    # set disk size here. leave it small for testing because expanding the disk takes time.
    size = "${var.master_node_disk}"
    type = "scsi"
    storage = "local-lvm"
    iothread = 1
  }

  # if you want two NICs, just copy this whole network section and duplicate it
  network {
    model = "virtio"
    bridge = "vmbr0"
    tag = var.node_vlan
  }
  # not sure exactly what this is for. presumably something about MAC addresses and ignore network changes during the life of the VM
#  lifecycle {
#   ignore_changes = [
#      network,
#   ]
#  }

  ipconfig0 = "ip=dhcp"

  # sshkeys set using variables. the variable contains the text of the key.
  sshkeys = <<EOF
  ${var.ssh_key}
  EOF
}

The problem is, that PVE generates a new MAC address for the interfaces, but all VMs gets the same ip from the DHCP Server. For example: I deploy four servers and every has the ip 192.168.69.108. The DHCP server has a lease-time of 600 seconds. Guest Agend is installed

Any idea why I got everytime the same IP?

jhajek commented 2 years ago

Could this be a systemd-networkd issue not a proxmox issue? Ubuntu server for instance switched to using systemd-networkd April of 2020 - what this means is that for the DHCP request of MAC address is not being broadcast but the machine-id is being broadcast. If you have a single template that you deploy from -- you are broadcasting the same machine-id each time -- the DHCP server will think it is the same machine asking for an IP address and give the same one out each time.

Two ways to attack this -- in your /etc/netplan config file you can add the item dhcp-identifier: mac and also delete your the file /etc/machine-id before you finish making your template -- that way the machine-id will be generated uniquely for each image on first boot.

fabian-born commented 2 years ago

I solved the problem with sysctl -w net.ipv4.ip_forward=1 on the pve host.

mleone87 commented 2 years ago

I solved the problem with sysctl -w net.ipv4.ip_forward=1 on the pve host.

where do you guys copy and paste this code? it's at least 2y old and breaks a lot o thing

  # not sure exactly what this is for. presumably something about MAC addresses and ignore network changes during the life of the VM
  lifecycle {
    ignore_changes = [
      network,
    ]
  }

remove it and be sure that you template has a cloud init agent installed and it has a machine-id "clean"

fabian-born commented 2 years ago

I found this in an older howto. I removed it now.

miko866 commented 2 years ago

@fabian-born Hello, I have the same problem with multiple VM per DHCP and I can't find solution. Please can you share with me how can I use that command sysctl -w net.ipv4.ip_forward=1 with Terraform & Proxmox? Thank you.

miko866 commented 2 years ago

I get it.

Create your cloud-init and after install qemu-guest-agent run virt-customize -a $IMAGE_NAME --run-command "echo -n > /etc/machine-id"