我想知道在使用Vagrant创建多机环境时是否有办法为配置程序指定默认值?
我试图做以下事情:
Vagrant.configure("2") do |config|
config.vm.box = "andyshinn/ubuntu-precise64"
config.vm.provision :chef_client do |chef|
chef.chef_server_url = "https://api.opscode.com/organizations/myorg"
chef.validation_key_path = "~/.chef/myorg-validator.pem"
chef.delete_node = true
chef.delete_client = true
chef.validation_client_name = "myorg-validator"
end
config.vm.define :app do |app|
app.vm.network "private_network", ip: "172.16.64.61"
app.vm.host_name = "vagrant-app-#{ENV['USER']}"
app.vm.provision :chef_client do |chef|
chef.add_recipe "myrecipe1"
chef.add_recipe "myrecipe2"
chef.add_recipe "sudo"
end
end
config.vm.define :web do |web|
web.vm.network "private_network", ip: "172.16.64.62"
web.vm.host_name = "vagrant-web-#{ENV['USER']}"
web.vm.provision :chef_client do |chef|
chef.add_recipe "myrecipe3"
chef.add_recipe "myrecipe4"
chef.add_recipe "sudo"
end
end
end
但是每个VM块似乎都没有获取任何主配置块设置。我收到这个错误:
There are errors in the configuration of this machine. Please fix
the following errors and try again:
chef client provisioner:
* Chef server URL must be populated.
* Validation key path must be valid path to your chef server validation key.
可以通过其他方法吗?
答案 0 :(得分:1)
您的问题属于范围之一。变量chef
在config.vm.provision
do块中具有块范围。
一旦此块退出,您的更改就会消失。您应该遵循流浪网站上的multi-machine
文档中的示例。
config.vm.provision "shell", inline: "echo Hello"
Vagrant.configure("2") do |config|
config.vm.box = "andyshinn/ubuntu-precise64"
config.vm.provision "chef_client", chef_server_url: "https://api.opscode.com/organizations/myorg"
config.vm.provision "chef_client", validation_key_path: "~/.chef/myorg-validator.pem
config.vm.provision "chef_client", delete_node: true
config.vm.provision "chef_client", delete_client: true
config.vm.provision "chef_client", validation_client_name: "myorg-validator"
config.vm.define :app do |app|
app.vm.network "private_network", ip: "172.16.64.61"
app.vm.host_name = "vagrant-app-#{ENV['USER']}"
app.vm.provision :chef_client do |chef|
chef.add_recipe "myrecipe1"
chef.add_recipe "myrecipe2"
chef.add_recipe "sudo"
end
end
应该做的伎俩
答案 1 :(得分:0)
答案 2 :(得分:0)
我认为内部块“覆盖”外部块。但是,你可以做一些事情,比如我最终在我的答案(我自己的问题)中假装inside out ordering of provisioners而不是像Vagrant提供的那样从外到。在你的情况下,你不会像我一样拥有一个shell和puppet供应商,但是你应用类似template method模式的东西来扩展一个厨师供应商的食谱。也许是这样的事情:
module Vagrant
module Config
module V2
class Root
def provision(extra_recipes)
config.vm.provision :chef_client do |chef|
chef.chef_server_url = "https://api.opscode.com/organizations/myorg"
chef.validation_key_path = "~/.chef/myorg-validator.pem"
chef.delete_node = true
chef.delete_client = true
chef.validation_client_name = "myorg-validator"
extra_recipes.each { |recipe| chef.add_recipe(recipe) }
end
end
end
end
end
end
Vagrant.configure("2") do |config|
config.vm.box = "andyshinn/ubuntu-precise64"
config.vm.define :app do |app|
app.vm.network "private_network", ip: "172.16.64.61"
app.vm.host_name = "vagrant-app-#{ENV['USER']}"
app.provision("myrecipe1", "myrecipe2", "sudo")
end
config.vm.define :web do |web|
web.vm.network "private_network", ip: "172.16.64.62"
web.vm.host_name = "vagrant-web-#{ENV['USER']}"
app.provision("myrecipe3", "myrecipe4", "sudo")
end
end