Hero Image
Vagrantfile and Provider

Day 8 - Vagrantfile and Provider Day 9 - Advanced Vagrantfile(編輯中) 用以下這個範例 你只要使用 Vagrant up 加上 provider 參數,就可開啟不同來源的機器 例如我要開啟 Vsphere 的機器,我只要下 vagrant up --provider=vsphere 要開 AWS 的機器,我只要下 vagrant up --provider=aws VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # 我們定義一個 Ubuntu 的機器 # box 的需求是對於 local 的 VM 才需要的 # 所以在 provider 是 vmware_fusion 或 Virtualbox 時,才會用到這個設定 config.vm.box = "hashicorp/precise64" # vmware_fusion config.vm.provider "vmware_fusion" do |v, override| override.vm.box = "precise64_vmware" v.gui = false end # vsphere config.vm.provider :vsphere do |vsphere| # 對於私有雲及公有雲,要clone 的vm 是儲存在雲端上的 # 所以 box 使用 dummy box 來達到這個目的 vsphere.vm.box = "nkhasanov/vsphere-simple" vsphere.host = 'YOURIP' vsphere.compute_resource_name = 'YOUR DATACENTER' vsphere.resource_pool_name = 'YOUR RESOURCE POOL' vsphere.insecure = true vsphere.template_name = 'VM TEMPLATE' vsphere.name = "#{YOUR_NAME}-test-machine" vsphere.user = 'administrator' vsphere.password = '$p1unK_Lab' vsphere.vm_base_path = 'vmware_template' vsphere.linked_clone = true end # virtual box config.vm.provider "virtualbox" do |vb| vb.gui = false end # aws config.vm.provider :aws do |aws, override| # aws configurations aws.access_key_id = "#{YOUR_AWS_ACCESS_KEY_ID}" aws.secret_access_key = "#{YOUR_AWS_ACCESS_KEY}" aws.keypair_name = "#{YOUR_NAME}" aws.security_groups = "#{YOUR_NAME}" aws.instance_type = "t2.small" aws.region = "us-east-1" # ubuntu 14.04 x64 aws.ami = "ami-864d84ee" # override info override.ssh.username = "ubuntu" override.ssh.private_key_path = "#{YOUR_AWS_PRIVATE_KEY_PATH}" override.vm.synced_folder "#{YOUR_SYNC_FOLDER}", "/vagrant", type: "rsync" override.vm.box = "dimroc/awsdummy" end end LOCAL_BOXS = { "ubuntu1404x64" => "ubuntu/trusty64", "ubuntu1210x64" => "chef/ubuntu-12.10" } AWS_AMIS = { "ubuntu1404x64" => "ami-864d84ee", "ubuntu1210x64" => "ami-02df496b", "windows2012r2x64" => "ami-9ade1df2", "windows2012x64" => "ami-5ce32034", "windows2008r2x64" => "ami-2ae02342", "windows2008x64" => "ami-5e24e936", "windows2003r2x64" => "ami-b0e320d8" } VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "precise64_vmware" # vmware_fusion config.vm.provider "vmware_fusion" do |v, override| override.vm.box = "precise64_vmware" v.gui = false v.vmx["memsize"] = "1024" v.vmx["numvcpus"] = "2" end # vsphere config.vm.provider :vsphere do |vsphere| vsphere.vm.box = "nkhasanov/vsphere-simple" vsphere.host = '#{}' vsphere.compute_resource_name = '#{}' # vsphere.resource_pool_name = 'YOUR RESOURCE POOL' vsphere.insecure = true vsphere.template_name = 'qasus-tw-centos7x64-01' vsphere.name = "#{YOUR_NAME}-test-machine" vsphere.user = 'administrator' vsphere.password = '#{}' vsphere.vm_base_path = 'vmware_template' vsphere.linked_clone = true end # virtual box config.vm.provider "virtualbox" do |vb| vb.gui = false vb.memory = 1024 vb.cpus = 2 end # aws config.vm.provider :aws do |aws, override| # aws configurations aws.access_key_id = "#{YOUR_AWS_ACCESS_KEY_ID}" aws.secret_access_key = "#{YOUR_AWS_ACCESS_KEY}" aws.keypair_name = "#{YOUR_NAME}" aws.security_groups = "#{YOUR_NAME}" aws.instance_type = "t2.small" aws.region = "us-east-1" # ubuntu 14.04 x64 aws.ami = "ami-864d84ee" # override info override.ssh.username = "ubuntu" override.ssh.private_key_path = "#{YOUR_AWS_PRIVATE_KEY_PATH}" override.vm.synced_folder "#{YOUR_SYNC_FOLDER}", "/vagrant", type: "rsync" override.vm.box = "dimroc/awsdummy" end # VMs (1..MAX_VM_NUMBER).each do |i| # define linux config.vm.define "l#{i}" do |node| # aws node.vm.provider :aws do |aws| aws.tags = { "Name" => "#{YOUR_NAME}-linux-#{i}" } end # local # node.vm.network "private_network", ip: "192.168.33.%d" % (i+2) node.vm.hostname = "ftan-linux-#{i}" end # define windows config.vm.define "w#{i}" do |node| node.vm.provider :aws do |aws| aws.ami = "ami-2ae02342" aws.tags = { "Name" => "#{YOUR_NAME}-windows-#{i}" } end end end # define customer YOUR_CUSTOMIZED_VM.each do |vm| config.vm.define "%s" % vm["name"] do |node| # aws node.vm.provider :aws do |aws| aws.ami = AWS_AMIS[vm["platform"]] aws.tags = { "Name" => "#{YOUR_NAME}-%s" % vm["name"] } end # vmware fusion # vmware workstation node.vm.provider :vmware_fusion do |fusion| fusion.vm.box = LOCAL_BOXS[vm["platform"]] fusion.vm.hostname = "#{YOUR_NAME}-%s" % vm["name"] end # vsphere # azure end end end

Hero Image
How to automatically resize virtual box disk with vagrant

How to automatically resize virtual box disk with vagrant Increasing Disk Space of a Linux-based Vagrant Box on Provisioning Vagrant.configure(2) do |config| config.vm.box = "centos/7" config.disksize.size = '20GB' end $ sudo parted /dev/sda resizepart 2 100% $ sudo lvextend -l +100%FREE /dev/centos/root $ sudo xfs_growfs /dev/centos/root Automate Part Vagrant.configure(2) do |config| common = <<-SCRIPT sudo parted /dev/sda resizepart 2 100% sudo pvresize /dev/sda2 sudo lvextend -l +100%FREE /dev/centos/root sudo xfs_growfs /dev/centos/root SCRIPT config.vm.define "node01" do |node1| node1.vm.hostname = "node01" node1.vm.network "private_network", ip: "192.168.56.121" config.vm.provision :shell, :inline => common end end vagrant plugin install vagrant-disksize Vagrantfile # Fail if the vagrant-disksize plugin is not installed unless Vagrant.has_plugin?("vagrant-disksize") raise 'vagrant-disksize is not installed!' end Vagrant.configure("2") do |config| config.vm.provider "virtualbox" do |vb| vb.name = "DISKEXTEND" vb.memory = 2048 vb.cpus = 2 end config.vm.define :"DISKEXTEND" do |t| end config.vm.hostname = "DISKEXTEND" config.vm.box = "bento/ubuntu-18.04" # Increase the default disk size of the bento image (64GB) to 96GB config.disksize.size = "96GB" # Run a script on provisioning the box to format the file system config.vm.provision "shell", path: "disk-extend.sh" end Provisioning Script: disk-extend.sh #!/bin/bash echo "> Installing required tools for file system management" if [ -n "$(command -v yum)" ]; then echo ">> Detected yum-based Linux" sudo yum makecache sudo yum install -y util-linux sudo yum install -y lvm2 sudo yum install -y e2fsprogs fi if [ -n "$(command -v apt-get)" ]; then echo ">> Detected apt-based Linux" sudo apt-get update -y sudo apt-get install -y fdisk sudo apt-get install -y lvm2 sudo apt-get install -y e2fsprogs fi ROOT_DISK_DEVICE="/dev/sda" ROOT_DISK_DEVICE_PART="/dev/sda1" LV_PATH=`sudo lvdisplay -c | sed -n 1p | awk -F ":" '{print $1;}'` FS_PATH=`df / | sed -n 2p | awk '{print $1;}'` ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'` echo "The root file system (/) has a size of $ROOT_FS_SIZE" echo "> Increasing disk size of $ROOT_DISK_DEVICE to available maximum" sudo fdisk $ROOT_DISK_DEVICE <<EOF d n p 1 2048 no w EOF sudo pvresize $ROOT_DISK_DEVICE_PART sudo lvextend -l +100%FREE $LV_PATH sudo resize2fs -p $FS_PATH ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'` echo "The root file system (/) has a size of $ROOT_FS_SIZE" exit 0