virtualbox 自动创建虚拟机时,IP无法自动设置的3个坑

时间:2024-04-10 16:09:09

之前用的好好的vagrant+virtualbox 自动化部署,突然在mac pro 测试时发生了ip 无法自动设置的问题。

后来终于找到了解决办法。

 

1. 这个Vagrantfile 文件必须在物理机器磁盘上,不能放在外挂的USB移动硬盘上,否则可能不能正常运行.(可能是磁盘格式的问题,具体原因尚不清楚。)

2. 模版的网卡名称必须和物理网卡的一致。我之前配置错误,ifcfg-enp0s3网卡,配置文件里 device=eth0 name=eth0 导致vagrant up 时,无法分配IP地址。

3. 桥接的网卡名称必须写对,我使用笔记本电脑是 bridge: "en0: Wi-Fi (Airport)",不能写成en0,

虽然我的ifconfig 列表显示如下:virtualbox 自动创建虚拟机时,IP无法自动设置的3个坑

 

 

我的ansible 自动创建生成的配置文件如下:

happy:flink-cluster happy$ more Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos1708-kernel4.4.116-docker-17.12.0-jre9-ce-go1.9" #centos1708-jdk1.9-docker-17.12.0-ce-go1.9

  #config.ssh.insert_key = true
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.ssh.username="root"
  config.ssh.password="kaixin.com"
  config.vm.provider :virtualbox do |vb|
    vb.linked_clone = true
    vb.memory = "1024"
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end

###########


  #flink1
  config.vm.define "flink1" do |web|
  web.vm.hostname = "flink1"
  web.vm.network :public_network, ip: "192.168.66.11", bridge: "en0: Wi-Fi (Airport)", bootproto: "static", gateway: "192.168.66.1"
  web.vm.provider :virtualbox do |vb|
  vb.linked_clone = true
  vb.name = "flink1"
  vb.cpus = 2
  vb.memory = 2048
  vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
  vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end
  end


  #flink2
  config.vm.define "flink2" do |web|
  web.vm.hostname = "flink2"
  web.vm.network :public_network, ip: "192.168.66.12", bridge: "en0: Wi-Fi (Airport)", bootproto: "static", gateway: "192.168.66.1"
  web.vm.provider :virtualbox do |vb|
  vb.linked_clone = true
  vb.name = "flink2"
  vb.cpus = 2
  vb.memory = 2048
  vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
  vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end
  end


  #flink3
  config.vm.define "flink3" do |web|
  web.vm.hostname = "flink3"
  web.vm.network :public_network, ip: "192.168.66.13", bridge: "en0: Wi-Fi (Airport)", bootproto: "static", gateway: "192.168.66.1"
  web.vm.provider :virtualbox do |vb|
  vb.linked_clone = true
  vb.name = "flink3"
  vb.cpus = 2
  vb.memory = 2048
  vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
  vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end
  end

end
happy:flink-cluster happy$