我正在对我的httpd和nginx虚拟主机confs进行puppetizing。 现在我有5个服务器,每个都有自己的端口,并运行在server1上,而nginx运行在server2上。 很明显,我需要保持对httpd和nginx的清单调用,因为它们可以在不同的服务器上运行。
我试图做的是共享两个清单之间的端口,所以我需要做的就是将我想要的虚拟主机的名称传递给httpd和nginx,它将在中心位置查找端口。 我正在尝试实现一个自定义函数,目前有这样的设置:
# /path/to/puppet/modules/global/lib/puppet/parser/functions/app_to_port.rb module Puppet::Parser::Functions newfunction(:app_to_port, :type => :rvalue) do |args| case args[0] when app_1 return 27960 when app_2 return 27961 end end end # /path/to/puppet/modules/httpd/manifests/vhost/conf.pp ... $vhost_port = app_to_port($name) ...
我一直遇到各种问题,$ name我一直得到undefined local variable or method 'app_1'错误,如果我传入一个int, $vhost_portvariables永远不会得到一个值。 我是新来的ruby和傀儡,这导致我相信我缺less一些语言或应用程序构造。 我运行ruby -rpuppet /path/to/app_to_port.rb时没有任何回应,这让我相信代码在语法上是正确的。
为了logging,我正在阅读http://docs.puppetlabs.com/guides/custom_functions.html和https://stackoverflow.com/questions/948135/how-to-write-a-switch-statement-in – 让我知道这一点。
目前env:ruby1.8.7和木偶2.7.19
Hiera包含在2.7中的Puppet 3.0中,您需要将其单独安装到您的主人。
因为听起来你想要映射是全局的,所以你需要把它放在适用于所有系统的Hiera文件中。 所以在你的hiera.yaml ,你会想要的东西..
:backends: - yaml :hierarchy: - common :yaml: :datadir: /etc/puppet/hieradata
然后在/etc/puppet/hieradata/common.yaml中设置你的端口映射:
port_app_1: "27960" port_app_2: "27961"
有了这个,你可以在你的虚拟主机configuration中查找它。
$vhost_port = hiera("port_${name}")