我正在尝试Puppet,它似乎是好的。
我的问题很简单,但我无法回答。
我有一个文件“node.pp”:
node basenode { include "dns" # include "ntp" } node 'myserver.domain.com' inherits basenode { $type_server = "client" include "ntp" }
我想声明我的variables“$ type_server”而不是在每个服务器上声明“include ntp”。 我想“basenode”包含“ntp”,并在我的服务器的节点中使用“$ type_server”的值。
该variables用于NTP的清单中。
提前致谢。
不要使用variables – 就像Shane所说的那样,Puppet中的variables范围是FUBAR,试图使用variables只会导致疼痛,痛苦,恶心,头痛以及可能的心脏病。
相反,使用定义的types。 这是他们devise的。 所以,不要使用类,而是包含在任何地方,使用一个定义的types并将它传递给它需要的数据:
define ntp_server($type_server) { # Do all the things you'd normally do, using $type_server as needed } node 'myserver.domain.com' { ntp_server { ntp: type_server => "client" } }
傀儡的范围有点不直观。 这是一种线性评估行为 – 因为inheritance是在variables被定义之前发生的,并且包含在inheritance类中的类是立即求值的,所以在需要的时候设置variables。
不要inheritance,只要在每个节点的底部加上“base”就可以了,这样在设置了必要的variables后就可以进行评估。 这不是一个非常面向对象的做事方式,但是这个范围界定行为并没有留下太多的select。
node 'myserver.domain.com' { $type_server = "client" include basenode }
我知道这已经很老了,但由于我遇到了麻烦,find了这个线索,可能对其他人有用。
而不是使用定义的types或节点内的节点 – 这听起来不是很好 – 我会build议使用参数化的类 。 通过使用它,作者可以继续正常使用他的类,并且这些类只会稍微修改以接收variables(它们已经使用)作为参数。
所以,作为例子,而不是:
class foo { file { 'abc': path => "some/path" content => $content } }
它可以有:
class foo($content) { file { 'abc': path => "some/path" content => $content } }
像这样使用它:
node 'myserver.domain.com' { class { 'foo': $content => "something pretty smart" } }