以前在ServerFault上,它显示了如何testing是否定义了一个资源: Puppet:testing是否定义了资源,或创build它
if defined(File["/tmp/foo"]) { alert("/tmp/foo is defined") } else { alert("/tmp/foo is not defined") }
我想testing一个虚拟资源是否被定义或存在,然后通过收集器实现它,但它似乎不工作在相同的语法。
create_resource('@package',hiera_hash('packages',{}),{}) if !defined(@Package['foo']) { alert('Hiera screwed up, critical virtual package is missing') } # Realize the critical packages Package<| groups == critical |>
这让我: Error: Could not parse for environment production: Virtual (@) can only be applied to a Resource Expression at test.pp..
原因是我想通过create_resources和collectors来实现,而作为testing,如果实际上并没有创build一些关键的虚拟资源,就会抛出一个错误。
编辑2014/11/12 19:07 UTC其中一个答案build议这个,但它不工作,粘贴在这里,因为答案评论框太小。
class base::test1 { @package { 'cookietool': ensure => 'present', } realize(Package['cookietool']) }
base::test2具有相同的内容。
node testbox { include base::test1 include base::test2 }
这失败了以下错误:
Error: Failed to compile catalog for node testbox: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Package[cookietool] is already declared in file /var/lib/puppet/checkouts/environments/production/modules/base/manifests/test1.pp:2; cannot redeclare at /var/lib/puppet/checkouts/environments/production/modules/base/manifests/test2.pp:2 at /var/lib/puppet/checkouts/environments/production/modules/base/manifests/test2.pp:2:3 on node testbox
根据定义,虚拟资源在被realized之前不是“定义的”。
与标准资源一样,您只能申报一次虚拟资源,但可以多次realized – 这是使用虚拟资源的主要原因之一。
例
模块/包/ init.pp
class packages { @package { 'git': ensure => present, } }
模块/ GIT中/ init.pp
class git { realize Package['git'] }
模块/ MyModule的/ init.pp
class mymodule { realize Package['git'] }
舱单/ site.pp
node 'mynode' { include packages include git include mymodule }
这将编译并确保安装git包。
只需声明一个依赖于您正在实现的资源的资源。
notify { 'important resources are declared': loglevel => debug, require => [ Package['foo'], ... ], }
如果Package[foo]没有实现,目录将会失败。
不要在这个或几乎任何其他场景中使用它。 它是依赖于编译器端的评估顺序,其结果是不可靠的。