最近在做一个自动化测试项目,深入研究了vagrant, 遇到了不少坑,网上的资料很多都无法解决。
本人通过亲自实验,得到了比较好的解决方案,分享给大家。
virtualbox 5.2.6 r120293(Qt5.6.1)
vagrant 版本Vagrant 2.0.2
安装在centos1611 上,centos1611 直接安装在一台物理服务器上.
之间遇到一个很深的坑. 我在Esxi 6.0 的VMware 云平台上安装 virtualbox,vagrant 都正常,但是vagrant up 启动虚拟机总是不正常,报加载不到驱动{有可能是虚拟机里安装虚拟机自动启动驱动出问题},故无法实现自动化操作。无奈之下,采用物理机上。
[centos@localhost t]$ 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"
#config.ssh.insert_key = true
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.username="root"
config.ssh.password="secret"
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
###########
#online-install
config.vm.define "online-install" do |web|
web.vm.hostname = "online-install"
web.vm.network :public_network,
auto_config: true, ip: "192.168.66.121", bridge:
"ens1f0", bootproto: "static", gateway: "192.168.66.1"
#下面红色部分演示provision用法
#web.vm.provision "shell",
# run: "always",
# inline: "ip addr add 192.168.66.121/24 dev eth1"
web.vm.provider :virtualbox do |vb|
vb.linked_clone = true
vb.name = "online-install"
vb.cpus = 2
vb.memory = 1024
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
end
#offline-install
config.vm.define "offline-install" do |web|
web.vm.hostname = "offline-install"
web.vm.network :public_network, auto_config: true, ip: "192.168.66.131", bridge:
"ens1f0", bootproto: "static", gateway: "192.168.66.1"
web.vm.provider :virtualbox do |vb|
vb.linked_clone = true
vb.name = "offline-install"
vb.cpus = 2
vb.memory = 1024
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
end
end
启动后效果如下:
[root@offline-install network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@offline-install network-scripts]# more ifcfg-eth1
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.66.131
NETMASK=255.255.255.0
DEVICE=eth1
GATEWAY=192.168.66.1
PEERDNS=no
#VAGRANT-END
[root@offline-install network-scripts]#
坑: 网关参数ansible 模版参数设置错误,导致虚拟机启动后没有变量替换,导致无法正常设置ip