我有一个 Vagrantfile,其中定义了两个虚拟机。我想使用 puppet 大师来配置那些。当我只有一个虚拟机时,我可以使用
config.vm.provision "puppet_server" do |puppet|
puppet.puppet_server = "puppet.srv"
puppet.puppet_node = config.vm.hostname
puppet.client_cert_path = "../puppet/ssl/ca/signed/#{config.vm.hostname}.pem"
puppet.client_private_key_path = "#{config.vm.hostname}.key"
puppet.options = "--test"
end
我想为两个虚拟机重用此 block ,但不能,因为每个虚拟机的 client_cert_path 和 client_private_key_path 需要不同。
当我尝试使用这些设置执行 Vagrantfile 时,我得到
There are errors in the configuration of this machine. Please fix the following errors and try again:
puppet server provisioner:
- The specified client cert path could not be found
- The specified client private key path could not be found
- "puppet_node" is required when a client cert or key is specified
这让我相信 config.vm.hostname
未定义
如何在不重复代码块的情况下实现此目的?
最佳答案
我在 Vagrant.configure
block 之后添加了一个 Helper
类:
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# regular configuration here
end
class Helper
def self.provision_puppet(vm)
vm.provision "puppet_server" do |puppet|
puppet.puppet_server = "puppet.test"
puppet.puppet_node = vm.hostname
puppet.client_cert_path = "../puppet/ssl/ca/signed/#{vm.hostname}.pem"
puppet.client_private_key_path = "ssl/#{vm.hostname}.key"
puppet.options = "--test"
end
end
end
然后为每个虚拟机调用Helper.provision_vm
config.vm.define "vpn", autostart: false do |vpn|
# various configurations
Helper.provision_puppet vpn.vm
end
关于 Vagrant : reusing a puppet_server block for multiple VMs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32714102/