Skip to content

VM Receives Random IP Address Instead of Specified Static IP #13541

Closed
@pakosaan

Description

@pakosaan

Expected behavior

I want static ip mentioned in vagrantfile

Actual behavior

I get random ip address

Reproduction information

Vagrant version

2.4.0 and 2.4.1

Host operating system

Fedora 41 Workstation

Guest operating system

fedora-41-cloud-base, ubuntu-24.04, fedora-40-cloud-base

Steps to reproduce

1.vagrant up
2.vagrant ssh vm-1
3.ip a

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrant configuration for setting up multiple VMs with flexible activation

Vagrant.configure("2") do |config|
  config.vagrant.plugins = "vagrant-libvirt"

  # List of VM names to activate
  ACTIVE_VMS = ["vm-1", "vm-2", "vm-3"]

  # Define settings for each VM in a hash, now including box images
  vm_settings = {
    "vm-1" => { ip: "10.200.0.2", memory: 512, cpus: 1, box: "pakosaan/fedora-cloud-base-41" },
    "vm-2" => { ip: "10.200.0.3", memory: 512, cpus: 1, box: "cloud-image/ubuntu-24.04" },
    "vm-3" => { ip: "10.200.0.4", memory: 512, cpus: 1, box: "fedora/40-cloud-base" },
  }

  # Function to configure each VM with settings from the vm_settings hash
  def configure_vm(config, name, settings)
    config.vm.define name do |vm_config|
      # Use the box image specified in the settings for each VM
      vm_config.vm.box = settings[:box]
      
      # Set the VM hostname based on its name
      vm_config.vm.hostname = name

      # Configure a private network IP for the VM from the settings hash
      vm_config.vm.network :private_network, ip: settings[:ip]

      # Set up a shared folder between the host and VM
      vm_config.vm.synced_folder "data", "/vm/data"

      # Configure resources like memory and CPUs for the VM's provider (libvirt)
      vm_config.vm.provider "libvirt" do |libvirt|
        libvirt.memory = settings[:memory]
        libvirt.cpus = settings[:cpus]
      end

      # Shell provisioner: Run a shell script named "provision.sh" on VM startup
      vm_config.vm.provision "shell", path: "script/provision.sh"

      # Ansible provisioner for automated configuration management
      # vm_config.vm.provision "ansible" do |ansible|
      #   ansible.playbook = "playbook.yml"
      #   ansible.compatibility_mode = "2.0"
      #   ansible.extra_vars = {
      #     ansible_python_interpreter: "/opt/ansible_venv/bin/python3"
      #   }
      # end
    end
  end

  # Loop through each VM setting in vm_settings and configure only the VMs in ACTIVE_VMS
  vm_settings.each do |name, settings|
    configure_vm(config, name, settings) if ACTIVE_VMS.include?(name)
  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions