2018-03-29 10:42:14 +00:00
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
|
# 定义虚拟机数量
|
|
|
|
|
vms = Array(1..5)
|
|
|
|
|
vms.each do |i|
|
2018-11-04 13:53:37 +00:00
|
|
|
|
config.vm.define "vm#{i}" do |cfg|
|
2018-03-29 10:42:14 +00:00
|
|
|
|
# 设置虚拟机的 Box
|
2018-11-04 13:53:37 +00:00
|
|
|
|
cfg.vm.box = "centos-7.5-ehlxr"
|
2018-03-29 10:42:14 +00:00
|
|
|
|
|
|
|
|
|
# 不检查 box 更新
|
|
|
|
|
cfg.vm.box_check_update = false
|
|
|
|
|
# 设置虚拟机的主机名
|
2018-11-04 13:53:37 +00:00
|
|
|
|
cfg.vm.hostname="vm.node#{i}"
|
2018-03-29 10:42:14 +00:00
|
|
|
|
# cfg.vm.network "forwarded_port", guest: 80, host: 8080
|
|
|
|
|
|
|
|
|
|
# hostonly
|
|
|
|
|
cfg.vm.network "private_network", ip: "192.168.3.10#{i}",:auto_network => true
|
|
|
|
|
# bridged
|
|
|
|
|
# cfg.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
|
|
|
|
|
|
|
|
|
|
cfg.vm.provider "virtualbox" do |vb|
|
|
|
|
|
# Display the VirtualBox GUI when booting the machine
|
|
|
|
|
vb.gui = false
|
|
|
|
|
|
|
|
|
|
# Customize the amount of memory on the VM:
|
|
|
|
|
vb.memory = "1024"
|
|
|
|
|
|
|
|
|
|
vb.cpus = 1
|
|
|
|
|
|
|
|
|
|
# 名称指的是在 VirtualBox 中显示的名称
|
2018-11-04 13:53:37 +00:00
|
|
|
|
vb.name = "vmhost#{i}"
|
2018-03-29 10:42:14 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 增加各节点 host 配置(插件安装:vagrant plugin install vagrant-hosts)
|
|
|
|
|
cfg.vm.provision :hosts do |provisioner|
|
|
|
|
|
vms.each do |x|
|
2018-11-04 13:53:37 +00:00
|
|
|
|
provisioner.add_host "192.168.3.10#{x}", ["vm.node#{x}"]
|
2018-03-29 10:42:14 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 默认 root 登陆
|
|
|
|
|
cfg.ssh.username = "root"
|
|
|
|
|
cfg.ssh.password = "vagrant"
|
|
|
|
|
cfg.ssh.insert_key = "true"
|
|
|
|
|
|
|
|
|
|
# do NOT check the correct additions version when booting this machine(插件安装:vagrant plugin install vagrant-vbguest)
|
2018-11-04 13:53:37 +00:00
|
|
|
|
# cfg.vbguest.auto_update = false
|
2018-03-29 10:42:14 +00:00
|
|
|
|
|
2018-11-04 13:53:37 +00:00
|
|
|
|
cfg.vm.synced_folder "/Users/ehlxr/works/Vagrant/vm_share", "/root/", type: "virtualbox"
|
2018-11-02 04:42:30 +00:00
|
|
|
|
cfg.vm.synced_folder "/Users/ehlxr/works/Vagrant", "/vagrant", disabled: true
|
2018-03-29 10:42:14 +00:00
|
|
|
|
|
|
|
|
|
# 开机运行命令
|
2018-11-02 02:42:27 +00:00
|
|
|
|
cfg.vm.provision "shell", run: "always", inline: <<-SHELL
|
|
|
|
|
echo -e "\033[1;33mConfig ssh...\033[0m"
|
2018-11-02 04:42:30 +00:00
|
|
|
|
mkdir -p ~/.ssh && cat /root/config/authorized.key >> ~/.ssh/authorized_keys
|
2018-11-02 02:42:27 +00:00
|
|
|
|
sed -i 's/^#RSAAuthentication.*/RSAAuthentication\ yes/g' /etc/ssh/sshd_config
|
|
|
|
|
sed -i 's/^#PubkeyAuthentication.*/PubkeyAuthentication\ yes/g' /etc/ssh/sshd_config
|
|
|
|
|
sed -i 's/^PasswordAuthentication.*/PasswordAuthentication\ yes/g' /etc/ssh/sshd_config
|
|
|
|
|
echo -e "\033[1;33mConfig dns...\033[0m"
|
2018-11-02 04:42:30 +00:00
|
|
|
|
cp /root/config/resolv.conf /etc/
|
2018-11-02 02:42:27 +00:00
|
|
|
|
SHELL
|
2018-03-29 10:42:14 +00:00
|
|
|
|
|
|
|
|
|
# 自定义初始化执行脚本
|
|
|
|
|
# cfg.vm.provision "shell", path: "config/init.sh"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|