在实际创建共享之前,我需要在我的 Vagrantfile 中设置一个用户。我想启动 Vagrant 实例,配置用户,然后设置共享。设置共享后,我想运行更多配置。
我该如何做到这一点?
谢谢!
示例 YAML 配置文件:
centos64:
ip: "192.168.33.110"
properties:
memory: 8192
cpus: 2
forwards:
- host: 8080
guest: 8080
- host: 9115
guest: 9115
shares:
repos:
owner: zenoss
group: zenoss
host: "~/repos/zenoss/"
guest: "/home/zenoss/repos"
providers:
virtualbox: "http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box"
vmware_fusion: "https://dl.dropbox.com/u/5721940/vagrant-boxes/vagrant-centos-6.4-x86_64-vmware_fusion.box"
provisions:
# sets up users and passwords
- provision: shell
path: setup_users.sh
privileged: true
# installs packages for OS
- provision: shell
path: install_packages.sh
privileged: true
# update rc files
- provision: shell
path: update_rcs.sh
privileged: true
示例 Vagrantfile:
require 'yaml'
settings = YAML.load_file "vagrant_config.yml"
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
settings.each do |name, cfg|
config.vm.define name do |agent|
agent.vm.box = name
cfg[:forwards].each do |port|
agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
end
if cfg.has_key?(:shares)
cfg[:shares].each do |sname, share|
share[:host] = File.expand_path(share[:host])
group = "vagrant"
owner = "vagrant"
host = share[:host]
guest = share[:guest]
if share.has_key?(:group)
group = share[:group]
end
if share.has_key?(:owner)
owner = share[:owner]
end
if
box.vm.synced_folder host, guest, owner: owner, group: group
end
end
agent.vm.provider :vmware_fusion do |v, override|
v.box = name + "_fusion"
v.box_url = cfg[:providers][:vmware_fusion]
cfg[:properties].each do |prop, val|
if prop == "cpus"
prop = "numvcpus"
elsif prop == "memory"
prop = "memsize"
end
v.vmx[prop] = val
end
v.vmx['fixed-address'] = cfg['ip']
end
agent.vm.provider :virtualbox do |v, override|
override.vm.box = name + "_vb"
override.vm.box_url = cfg['providers']['virtualbox']
override.vm.network :private_network, ip: cfg['ip']
cfg[:properties].each do |prop, val|
if prop == "numvcpus"
prop = "cpus"
elsif prop == "memsize"
prop = "memory"
end
v.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
end
end
cfg[:provisions].each do |prov|
agent.vm.provision prov[:provision] do |p|
# setup keys that do not directly map
if prov.has_key?('recipes')
prov[:recipes].each do |r|
p.add_recipe r
end
prov.delete(:recipes)
end
# setup 1:1 mapping keys
prov.each do |key, val|
if key != 'provision'
p.instance_variable_set("@#{key}", val)
end
end
end
end
end
end
end