vagrantfile 示例

Vagrantfile

  • 修改根目录大小
  • 多个主机
  • 多个磁盘
  • 网络配置
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |node|
  (1..1).each do |index|
    node.vm.define "node#{index}" do |config|
      # box config
      config.vm.box = "centos7"
      config.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"
      config.vm.box_check_update = false
      config.vm.post_up_message = "Hello, welcome to server powered by vagrant."

      # change / size
      config.disksize.size = "50GB"

      # network config
      config.vm.network "forwarded_port", guest: 80, host: "4708#{index}", host_ip: "0.0.0.0"
      config.vm.network "private_network", ip: "10.10.10.5#{index}"

      config.vm.hostname = "node#{index}"
      # /etc/hosts
      config.vm.provision :hosts do | provisioner |
        provisioner.add_host '10.10.10.50', ["node1"]
      end

      # file sync
      config.vm.synced_folder "/root/yang-dev", "/root/deploy"


      config.vm.provider "virtualbox" do |vb|
        # Display the VirtualBox GUI when booting the machine
        vb.gui = false

        # cpu
        vb.cpus = 4

        # Customize the amount of memory on the VM:
        vb.memory = "8192"

        vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata", "--controller", "IntelAHCI"]
        (1..2).each do |count|
          file_to_disk = "/disk#{index}/xbox/vd2_#{count}.vdi"
          unless File.exist?(file_to_disk)
            # 20G disk
            vb.customize ["createhd", "--filename", file_to_disk, "--size", 20 * 1024, "--format", "VDI"]
          end
          vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "#{count}", "--device", 0, "--type", "hdd", "--medium", file_to_disk]
        end
      end

      config.vm.provision "shell", inline: <<-SHELL
        # change root password
        echo abc1234 | passwd --stdin root
        # permit root login by ssh-key
        sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
        sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
        systemctl restart sshd
        echo "hello vagrant."
      SHELL
    end
  end
end
posted @ 2020-07-06 16:00  hiyang  阅读(324)  评论(0编辑  收藏  举报