在类中访问节点variables并在条件中使用它

我正在尝试使用PuppetconfigurationApache虚拟主机,并尝试不同的方式,取得了一些成功。

我已经定义了一个节点如下:

node 'test1.cob' inherits serveurClient { $smcvhost = 'all' } 

serveurClient类包含apache类。 这可以正常工作Apache安装和所有的configuration得到正确应用,除了虚拟主机。

有关虚拟主机的configuration如下:

 class apache::config { File{ require => Class["apache::install"], notify => Class["apache::service"], ensure => present, owner => "www-data", group => "www-data", mode => 755 } ... if ( $smcvhost == 'belleville' ) or ( $smcvhost == 'all' ) { apache::smcvhost{'belleville': client => 'belleville', } } ... } 

apache :: smcvhost定义正常工作,因为如果我直接在没有条件的节点中指定它,虚拟主机被正确创build而没有错误。 如果我删除if语句,它也将被正确创build。 我只尝试过指定第二个条件,但没有成功。

当这个失败执行,我不会有任何错误。 傀儡报告只是忽略了这个configuration部分。

我在想,这是一个可变的范围问题,但从我读到的,这种做法似乎是正确的,我想木偶会给我一些错误,如果我试图评估一个不存在的variables。

傀儡不会警告你未设置的variables(除了模板,好奇地),并且,当你设置一个variables时,只会影响到指定的东西。 所以:

 node 'test1.cob' inherits serveurClient { # Nothing above this line knows $smcvhost $smcvhost = 'all' # Stuff from this line until "}" knows $smcvhost } # Stuff from this point on don't know $smcvhost 

如果你看看最新版本的Puppet的发行说明,你会看到dynamic范围正在退休。 我的build议是尽量避免使用variables来传递信息 – 使用参数,例如定义或带参数的类。

代替:

节点'test1.cob'inheritanceserveurClient {$ smcvhost ='all'}

如果你这样做:

节点'test1.cob'{$ smcvhost ='all'include serveurClient}

那么你的variables将存在于serveurClient作用域中,这是我假设你的apache类被引用的地方。