Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pakosaan opened this issue Nov 12, 2024 · 0 comments
Open

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

pakosaan opened this issue Nov 12, 2024 · 0 comments

Comments

@pakosaan
Copy link

pakosaan commented Nov 12, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant