安装vagrant-aws插件

1
$ vagrant plugin install vagrant-aws

配置vagrant

1
2
3
4
$ mkdir aws
$ cd aws
$ vagrant init
$ vi Vagrantfile

修改配置文件Vagrantfile, 添加下列内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
config.vm.define :osapp01d do |osapp01d|

  osapp01d.vm.provider :aws do |aws, override|

    aws.access_key_id = 您的aws access key id
    aws.secret_access_key = 您的aws secret access key

    aws.instance_type = 您的EC2 instance类型
    aws.ami = "ami-9ffa709e" #cent-os 6.4 64位
    aws.region = "ap-northeast-1" #东京主机
    aws.security_groups = 您的security group id
    aws.subnet_id = 您的subnet id
    aws.tags = {
      'Name' => "osapp01d"
    }
    aws.elastic_ip = true #为EC2 instance绑定静态IP

    override.ssh.username = "root"
    aws.keypair_name = 您的key pair name
    override.ssh.private_key_path = .pem私钥的位置

    override.vm.box = "dummy"
    override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"

    override.vm.synced_folder "./", "/vagrant", disabled: true
  end
end

参数的含义和赋值请参考这里
请把相关参数替换成您的实际值

PS:关于各linux发行版的默认user

For Amazon Linux, the default user name is ec2-user. For RHEL5, the user name is often root but might be ec2-user. For Ubuntu, the user name is ubuntu. For SUSE Linux, the user name is root. Otherwise, check with your AMI provider.

创建 EC2 instance 并启动

1
vagrant up --provider=aws osapp01d

如果您使用的是gnome-terminal, 请注意要设置为以登录shell方式运行命令(Run command as login shell)
Edit > Profile Preferences > Title and Command > Run command as a login shell

登录EC2 instance, 添加vagrant用户

1
vagrant ssh osapp01d

以下代码在EC2 inscatnce里执行

1
2
3
$ groupadd admin
$ useradd -g admin vagrant
$ passwd vagrant

设置vagrant用户的密码

1
$ visudo

在最末添加一行:

%admin ALL=(ALL:ALL) ALL

如需使用同步文件夹,需将Defaults requiretty这行注释掉
保存退出

向EC2 instance安装cookbooks

1
2
3
4
5

$ mkdir Cookbooks
$ cd Cookbooks
$ knife solo prepare vagrant@xx.xx.xx.xx(您的EC2 instance的public ip)
$ knife solo cook vagrant@xx.xx.xx.xx(您的EC2 instance的public ip) nodes/osapp01d,json

关于文件夹同步

如果您希望使用同步文件夹,
请将Vagrantfile里的override.vm.synced_folder "./", "/vagrant", disabled: true注释掉
并且重启EC2 instance

1
vagrant reload osapp01d

请注意这一步要在EC2 instance创建完成后,
并且您已经将Defaults requiretty注释掉后才可执行.
否则会发生问题vagrant无法正常启动, 详情见这里

下载安装 Virtualbox

这里可以直接用ubuntu软件中心提供的版本
其他系统可以再下面找
下载Virtualbox

下载安装 vagrant

点击进入下下载列表
注意低版本的vagrant提供gem安装方式, 目前已不支持, 请不要采取此种方式, 详见此处

下载安装 vagrant 的 centos box

点击下载box
安装box
$ vagrant box add centos-6.4 /home/hanbing/Downloads/CentOS-6.4-x86_64-v20130731.box
可以在下面找到其他操作系统的box
vagrantbox

配置vagrant

1
2
3
4
5
$ cd ~/
$ mkdir vagrant
$ cd vagrant
$ vagrant init
$ vi Vagrantfile

把下面内容添加到 Vagrantfile

1
2
3
config.vm.box = "centos-6.4"
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.host_name = "oshost"

设置共享文件夹(若不配置,默认把host的Vagrantfile所在目录共享到guest的/vagrant),
第一个参数是host machine的待共享文件夹, 可以用相对路径.
第二个参数是gues tmachine的共享文件夹, 必须为绝对路径, 若不存在会自动创建一个.
其他支持的options可以参看这里
config.vm.synced_folder "/home/hanbing", "/vagrant"

有时默认的共享文件夹会有性能问题, 需要加参数nfs: true
若要使用NFS文件夹, host machine需要安装了nfsd
另外注意在Windows下不支持NFS, 所以Windows会自动忽略nfs参数
config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", nfs: true

配置好后可以通过vagrant up启动vm
Vagrant常用命令

1
2
3
4
5
$ vagrant up 开机
$ vagrant ssh 登⼊
$ vagrant suspend 暂停
$ vagrant halt 关机
$ vagrant destroy 刪除

插曲 启动时报错

报错信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
hanbing@hanbing-System-Product-Name:~/vagrant$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...
    default: Error: Connection timout. Retrying...

原本以为ssh的问题, 后来发现是centOS无法启动,直接用VirtualBox启动时有如下提示:

VT-x/AMD-V 硬件加速已被启用, 但当前处于无效状态. 您虚拟电脑内的操作系统将无法检测到64位的处理器,因此也将无法启动.

请确认在您电脑的BIOS中已启用 VT-x/AMD-V 支持.

解决办法:
(1)进入BIOS设置,把Virtualization设为为Enabled(一般会在高级设置里)
(2)虚拟机设置 > 系统 > 硬件加速 > 启用VT-x/AMD-V
(3)重新执行 $ vargant up
正常了^_^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /home/hanbing/vagrant

PS:2013版Mac air下不存在这个问题

开始安装各个cookbook

执行$ vagrant ssh-config --host oshost
会出现类似下面内容:

1
2
3
4
5
6
7
8
9
10
Host oshost
  HostName 127.0.0.1
  User vagrant
  Port
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/hanbing/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

将其加入~/.ssh/config内, 以后使用$ ssh oshost可以直接连入guest machine
$ vagrant ssh-config –host oshost >> ~/.ssh/config

除此以外,使用vagrant ssh [主机名]也可连入guest machine
$ vagrant ssh oshost

创建kitchen, 下载cookbooks $ mkdir Cookbooks $ cd Cookbooks`

安装gem $ gem install librarian-chef $ gem install knife-solo

在当前目录生成一个kitchen   $ knife solo init .

在kitchen里生成Cheffile $ librarian-chef init

编辑Cheffile添加需要的cookbooks
下载Cheffile里要求的cookbooks到./cookbooks目录 $ librarian-chef install

之后执行以下操作开始装机过程

1
2
$ knife solo prepare oshost
$ knife solo cook oshost

knifo-solo命令

1
2
3
4
$ knife solo init 目录                               新建一个符合chef结构的目录(kitchen)用以创建和储存recipes
$ knife solo prepare user@host                      在给定的host安装chef
$ knife solo cook user@host [nodes/<hostname>.json] 将kitchen上传到指定的host并执行chef-solo
$ knife solo bootstrap user@host                    perpare+cook

配置多台 vm

修改Vagrantfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  config.vm.define :oshost do |oshost|
    oshost.vm.box = "centos-6.4"
    oshost.vm.network :private_network, ip: "192.168.33.10"
    oshost.vm.host_name = "oshost"
    oshost.vm.provider "virtualbox" do |v|
      v.name = "oshost"
    end
  end

  config.vm.define :osdep01d do |osdep01d|
    osdep01d.vm.box = "centos-6.4"
    osdep01d.vm.network :private_network, ip: "192.168.33.11"
    osdep01d.vm.host_name = "osdep01d"
    osdep01d.vm.provider "virtualbox" do |v|
      v.name = "osdep01d"
    end
  end

启动vm
$ vagrant up osdep01d

添加ssh host
$ vagrant ssh-config –host osdep01d >> ~/.ssh/config

osdep01d安装cookbooks
$ knife solo bootstrap osdep01d

参考文章

HOWTO get started with chef, librarian-chef and vagrant
[教學]使用Vagrant練習環境佈署
[Rails佈署實戰教學]使用Chef-Solo一鍵安裝機器
Vagrant Provisioning, Chef solo and Librarian-chef

相关资源

VirtualBox
vagrantbox
cookbooks

原文地址

%Q

用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\“)

>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""

()也可用其他非数字字母的符号或成对的符号代替, 诸如[], !!, ++,{}, <>等.
以下写法全部与上面等效:

>> %Q!Joe said: "Frank said: "#{what_frank_said}""!
>> %Q[Joe said: "Frank said: "#{what_frank_said}""]
>> %Q+Joe said: "Frank said: "#{what_frank_said}""+     

除此之外还可省略Q写作:

>> %/Joe said: "Frank said: "#{what_frank_said}""/
=> "Joe said: "Frank said: "Hello!""" 

%q

%Q类似, 但是表示的是单引号字符串

>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
=> "Joe said: 'Frank said: '\#{what_frank_said} ' '"    

%W

语法近似于%Q, 用于表示其中元素被双引号括起的数组.

>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"] 

%w

用于表示其中元素被单引号括起的数组. 比较奇怪的是\(斜杠空格)会被转化成(空格), 但是其他的内容不会.

>> %w(a b c\ d \#e #{1}f)
=> ["a", "b", "c d", "\\#e", "\#{1}f"]

%x

使用“`方法执行一段shell脚本并返回标准输出内容.

>> %x(echo foo:#{foo})
=> "foo:Foo\n"    

%r

语法近似于%Q, 用于正则表达式.

>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"     

%s

用于表示symbol, 但是不会对其中表达式等内容进行转化

>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"

%i

Ruby 2.0 之后引入的语法, 用于生成一个symbol数组

2.0.0p247 :014 > %i(a b c)
=> [:a, :b, :c]