iT邦幫忙

DAY 4
0

用Packer, Vagrant和Salt秒殺你的環境建置系列 第 4

Day 4 - vagrantfile (1)

Vagrantfile 是vagrant 用來對VM做設定的 config 檔
因為 Vagrant 是用ruby 寫的,Vagrantfile 也是一個ruby file
若你已經會寫ruby,相信看懂這個檔案不是難事

為了講解 Vagrantfile 我拿以下 github 已經有人寫好的 example來當作範例

github source

Example 1

Single box with default options.

Vagrant::Config.run do |config|
config.vm.box = 'precise32'
config.vm.box_url = 'http://files.vagrantup.com/precise32.box'
end

上面這個範例是一個很簡單的 Vagrantfile,
當你的資料夾中有 vagrantfile 包含上述內容時
你下 vagrant up 會有如下的動作

  1. 檢查你的電腦中是否已下載 precise32 這個box

  2. http://files.vagrantup.com/precise32.box 下載 presices32 這個box

  3. 自 precise32 clone 一個新的vm

  4. 開啟這個vm

Example 2

Single box with some custom options defined in variables.

box = 'precise32'
url = 'http://files.vagrantup.com/precise32.box'
hostname = 'myprecisebox'
domain = 'example.com'
ip = '192.168.0.42'
ram = '256'

Vagrant::Config.run do |config|
config.vm.box = box
config.vm.box_url = url
config.vm.host_name = hostname + '.' + domain
config.vm.network :hostonly, ip

config.vm.customize [
'modifyvm', :id,
'--name', hostname,
'--memory', ram
]
end

第二個範例是為你的 vm 增加一些額外的設定

你下 vagrant up 會有如下的動作

  1. 檢查你的電腦中是否已下載 precise32 這個box
  2. http://files.vagrantup.com/precise32.box 下載 presices32 這個box
  3. 自 precise32 clone 一個新的vm
  4. 設定新 vm 的 ram 為 256mb, 網路模式為 hostonly
  5. 開啟這個vm
  6. 設定新 vm 的 ip 為 192.168.0.42
  7. 設定新 vm 的 domain name 為 myprecisebox.example.com

在 vagrant 中,有些設定是屬於對 virtualbox 或vmware 操作的 (如 ram size, cpu number)
有些設定是對 vm 本身操作的(如ip , hostname),這些設定就是在開啟時對vm跑script 修改


上一篇
Day 3 - Vagrant BOX and Vagrant Cloud
下一篇
Day 5 - Vagrantfile(2)
系列文
用Packer, Vagrant和Salt秒殺你的環境建置16
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言