vagrant 创建虚拟机脚本

VAGRANTFILE_API_VERSION = "2"

disk_ceph01 = ‘.ceph01_secondDisk.vdi‘
disk_ceph02 = ‘.ceph02_secondDisk.vdi‘
disk_ceph03 = ‘.ceph03_secondDisk.vdi‘
disk_ceph04 = ‘.ceph04_secondDisk.vdi‘
disk_cinder = ‘.cinder_secondDisk.vdi‘

$script1 = <<SCRIPT
mkdir -p /root/.ssh
chmod 700 /root/.ssh
cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
SCRIPT

$script2 = <<SCRIPT
echo "auto enp0s8
iface enp0s8 inet manual" >> /etc/network/interfaces
ifconfig enp0s8 down
ifconfig enp0s8 up
SCRIPT

$hosts_sh= <<SCRIPT
echo "10.0.0.11 controller
10.0.0.21 network
10.0.0.41 cinder
10.0.0.31 compute01" >> /etc/hosts
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Use the same key for each machine
  config.ssh.insert_key = false
  #config.vm.provision "file", source: "/root/.ssh/id_rsa.pub", destination: "/tmp/id_rsa.pub"
  #config.vm.provision "shell", inline: $script1
  #config.vm.boot_timeout = 1000
  config.vm.provision "shell", inline: $hosts_sh

config.vm.define "controller" do |controller|
    controller.vm.box = "bento/ubuntu-16.04"
    controller.vm.hostname = "controller"
    controller.vm.network "forwarded_port", guest: 22, host: 2201
    controller.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true      
    controller.vm.network "private_network", ip: "10.0.0.11"
    controller.vm.network "private_network", ip: "192.168.100.11"
    controller.vm.provider "virtualbox" do |v|
      v.memory = 8192
      v.cpus = 4
    end
  end

config.vm.define "network" do |network|
    network.vm.box = "bento/ubuntu-16.04"
    network.vm.hostname = "network"
    network.vm.network "forwarded_port", guest: 80, host: 8082
    network.vm.network "forwarded_port", guest: 22, host: 2202
    network.vm.boot_timeout = 600
    network.vm.provision "shell", inline: $script2
    network.vm.network "private_network", type: "dhcp", auto_config: false    
    network.vm.network "private_network", ip: "10.0.0.21"
    network.vm.network "private_network", ip: "192.168.100.21"    
    network.vm.provider "virtualbox" do |v|
      v.memory = 2192
      v.cpus = 2
    end
  end

config.vm.define "compute01" do |compute|
    compute.vm.box = "bento/ubuntu-16.04"
    compute.vm.hostname = "compute01"    
    compute.vm.network "forwarded_port", guest: 80, host: 8083
    compute.vm.network "forwarded_port", guest: 22, host: 2203
    compute.vm.boot_timeout = 600
    compute.vm.provision "shell", inline: $script2
    compute.vm.network "private_network", type: "dhcp", auto_config: false    
    compute.vm.network "private_network", ip: "10.0.0.31"
    compute.vm.network "private_network", ip: "192.168.100.31"    
    compute.vm.provider "virtualbox" do |v|
      v.memory = 8192
      v.cpus = 4
    end
  end

config.vm.define "cinder" do |cinder|
    cinder.vm.box = "bento/ubuntu-16.04"
    cinder.vm.hostname = "cinder"
    cinder.vm.network "forwarded_port", guest: 80, host: 8086
    cinder.vm.network "forwarded_port", guest: 22, host: 2206
    cinder.vm.boot_timeout = 600
    cinder.vm.provision "shell", inline: $script2
    cinder.vm.network "private_network", type: "dhcp", auto_config: false
    cinder.vm.network "private_network", ip: "10.0.0.41"
    cinder.vm.network "private_network", ip: "192.168.100.41"
    cinder.vm.provider "virtualbox" do |v|
      unless File.exist?(disk_cinder)         
        v.customize [‘createhd‘, ‘--filename‘, disk_cinder, ‘--variant‘, ‘Standard‘, ‘--size‘, 20 * 1024]
      end
      v.memory = 2592
      v.cpus = 1
      v.customize [‘storageattach‘, :id, ‘--storagectl‘, ‘SATA Controller‘, ‘--port‘, 1, ‘--device‘, 0, ‘--type‘, ‘hdd‘, ‘--medium‘, disk_cinder]
    end    
  end

config.vm.define "ceph01" do |ceph01|
    ceph01.vm.box = "bento/ubuntu-16.04"
    ceph01.vm.hostname = "ceph01"
    ceph01.vm.boot_timeout = 600    
    ceph01.vm.network "private_network", ip: "192.168.100.110"
    ceph01.vm.network "private_network", ip: "192.168.200.110"        
    ceph01.vm.provider "virtualbox" do |v|
      unless File.exist?(disk_ceph01)
        v.customize [‘createhd‘, ‘--filename‘, disk_ceph01, ‘--variant‘, ‘Standard‘, ‘--size‘, 20 * 1024]
      end
      v.memory = 8192
      v.cpus = 4
      v.customize [‘storageattach‘, :id, ‘--storagectl‘, ‘SATA controller‘, ‘--port‘, 1, ‘--device‘, 0, ‘--type‘, ‘hdd‘, ‘--medium‘, disk_ceph01]
    end
  end

config.vm.define "ceph02" do |ceph02|
    ceph02.vm.box = "bento/ubuntu-16.04"
    ceph02.vm.hostname = "ceph02"
    ceph02.vm.boot_timeout = 600    
    ceph02.vm.network "private_network", ip: "192.168.100.111"
    ceph02.vm.network "private_network", ip: "192.168.200.111"    
    ceph02.vm.provider "virtualbox" do |v|
      unless File.exist?(disk_ceph02)
        v.customize [‘createhd‘, ‘--filename‘, disk_ceph02, ‘--variant‘, ‘Standard‘, ‘--size‘, 20 * 1024]
      end
      v.memory = 8192
      v.cpus = 4
      v.customize [‘storageattach‘, :id, ‘--storagectl‘, ‘SATA controller‘, ‘--port‘, 1, ‘--device‘, 0, ‘--type‘, ‘hdd‘, ‘--medium‘, disk_ceph02]
    end
  end

config.vm.define "ceph03" do |ceph03|
    ceph03.vm.box = "bento/ubuntu-16.04"
    ceph03.vm.hostname = "ceph03"
    ceph03.vm.boot_timeout = 600    
    ceph03.vm.network "private_network", ip: "192.168.100.121"
    ceph03.vm.network "private_network", ip: "192.168.200.121"    
    ceph03.vm.provider "virtualbox" do |v|
      unless File.exist?(disk_ceph03)
        v.customize [‘createhd‘, ‘--filename‘, disk_ceph03, ‘--variant‘, ‘Standard‘, ‘--size‘, 20 * 1024]
      end
      v.memory = 8192
      v.cpus = 4
      v.customize [‘storageattach‘, :id, ‘--storagectl‘, ‘SATA controller‘, ‘--port‘, 1, ‘--device‘, 0, ‘--type‘, ‘hdd‘, ‘--medium‘, disk_ceph03]
    end
  end

config.vm.define "ceph04" do |ceph04|
    ceph04.vm.box = "bento/ubuntu-16.04"
    ceph04.vm.hostname = "ceph04"
    ceph04.vm.boot_timeout = 600    
    ceph04.vm.network "private_network", ip: "192.168.100.131"
    ceph04.vm.network "private_network", ip: "192.168.200.131"    
    ceph04.vm.provider "virtualbox" do |v|
      unless File.exist?(disk_ceph04)
        v.customize [‘createhd‘, ‘--filename‘, disk_ceph04, ‘--variant‘, ‘Standard‘, ‘--size‘, 20 * 1024]
      end
      v.memory = 8192
      v.cpus = 4
      v.customize [‘storageattach‘, :id, ‘--storagectl‘, ‘SATA controller‘, ‘--port‘, 1, ‘--device‘, 0, ‘--type‘, ‘hdd‘, ‘--medium‘, disk_ceph04]
    end
  end
end

时间: 2024-12-16 15:13:13

vagrant 创建虚拟机脚本的相关文章

使用vagrant创建虚拟机

关于vagrant,维基百科给出了定义:"Vagrant is an open-source software product for building and maintaining portable virtual development environments.",看到了吧,是一个搭建虚拟开发环境的开源软件.下面就来看看vagrant如何搭建的吧. 1.在vagrant官网https://www.vagrantup.com/下载vagrant,安装过后如下图所示. 如果安装过后

kvm 创建虚拟机脚本(centos7)

#!/bin/bash #创建单个虚拟机(需要电脑中存在模板虚拟机) #获取新虚拟机名称(单个创建模式) get_newname(){ while true do read -p "请输入新虚拟机名称:" newname if [ $newname ];then break else echo "************" echo "请输入姓名!" echo "************" fi done } get_newm

kvm批量创建虚拟机并自动设定ip和服务器名

1. 说明 kvm比起vmware来说最没有的功能就是用模板部署虚拟机的时候可以自定义ip地址,这对于大量创建vm的场景来说你需要一台台vnc的登录改动ip,显然是很累的,最近我就遇到这种场景(大型的cloudstack和openstack架构不说) 想到了一个方法,就是用一台关了机的虚拟机作为模板,复制它的xml文件可以改mac地址,这样就可以通过mac地址的唯一性而在模板虚拟机中加入一个脚本来根据一个写好在虚拟机中的列表文件来设定相应的ip地址和服务器名.这种方法要求以下步骤 1.模板虚拟机

基于VirtualBox和Vagrant搭建虚拟机

一.VirtualBox和vagrant的安装 VirtualBox 是一款开源的虚拟机软件,和VMWare是同类型的软件,用于在当前的电脑上构建一台虚拟机,在这台虚拟机上可以安装系统和软件,与真实的电脑一般无二. Vagrant用于创建和部署虚拟化开发环境.它使用Oracle的开源VirtualBox虚拟化系统.. 注意的是这两个的版本,如果匹配不上在运行vagrant命令时就会出现版本问题,笔者使用的是windows7系统,这里提供上面两个安装包的百度云的地址: 1.VirtualBox 链

使用Vagrant和Virtualbox创建虚拟机

简单地说,Vagrant让我们可以通过代码的方式快速地.可重复地创建针对不同虚拟环境的虚拟机,包括Virtualbox.AWS.Docker等.它使得我们可以一次性地.自动创建多个环境相同的虚拟机,对于软件开发和测试尤其有用.本文我们将以Virtualbox为例,看看Vagrant的基本使用. (一)快速入门 首先需要创建一个目录用于存放Vagrantfile文件以及Vagrant在工作中的数据: mkdir my-vagrant-project cd my-vagrant-project 然后

Windows上用VAGRANT创建运行管理VirtualBox虚拟机

百度百科:Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境. 体会: 1.用Vagrant,我可以快速的自动化的从模板box虚拟机创建出新的虚拟机,启停虚拟机,开发测试,不用了destroy虚拟机. 2.Vagrant支持Windows, MacOS, Linux,所以Windows, MacOS用户有福了. 3.box吗,从 http://www.vagrantbox.es/ 下载好

shell 脚本创建虚拟机 并配置网卡ip地址(两个脚本)

#######################脚本1:创建虚拟机kvm##########################!/bin/bashqcow2_dir=/var/lib/libvirt/imagesread -p "请输入虚拟机号码:" num #主机号if [ $num -lt 9 ];thennum=0$num ##主机号fiif [ -z "$num" ];thenecho "错误输出"exit 1fikvm_name=host$

从现有的虚拟机创建vagrant box ,再利用新创建的box创建虚拟机

本人使用的虚拟机provider是virtualbox,OS是centos7 创建自己的virtualbox前,需要在virtualbox上已经搭建了一个虚拟机.如下为已经搭建好的虚拟机,虚拟机名称为cusbox_vb1 利用已经搭建好的虚拟机就可以创建自己的vagrant box.创建box之前需要先关闭虚拟机. 创建vagrant box vagrant package --base 虚拟机的名字 --output 要创建的box的名字 如:vagrant package --base cu

Linux下利用vagrant创建单台虚拟机

1.创建虚拟机 VBoxManage createvm --name hadoop71 --ostype RedHat_64 --register 2.查看一下当前可用的虚拟机 VBoxManage list vms 3.查看一下当前虚拟机hadoop71的状态及一些默认配置 VBoxManage showvminfo hadoop71 4.VBoxManage modifyvm "hadoop71" --memory 2048  ---修改内存大小 5.VBoxManage modi