木偶:testing资源是否定义,或创build它

我一直在试图找出一种方法来testing资源是否已经在另一个文件中定义,如果不是创build它? 一个简单的例子:

if File[$local_container] { alert("Testing - It existed $local_container") } else { file{ "$local_container": ensure => directory, } } 

但是, File[$local_container]似乎总是评估为真。 有没有办法做到这一点?

这样做的更好的方法是使用puppetlabs stdlib中的ensure_resource函数

它需要一个资源types,标题和描述资源作为参数的属性列表。

假设你有testing用例,只在资源不存在的情况下创build资源 –

 ensure_resource('package', 'test-pkg', {'ensure' => 'present'}) 

你的意思是“testing一个资源是否已经定义 ”? 如果你定义了一个资源(例如file {}等),Puppet将会创build你所描述的内容(假设你确实已经确认ensure => present了)。

要检查资源是否已经在目录中定义或不是:

 mark-draytons-macbook:~ mark$ cat test.pp file { "/tmp/foo": ensure => present } if defined(File["/tmp/foo"]) { alert("/tmp/foo is defined") } else { alert("/tmp/foo is not defined") } if defined(File["/tmp/bar"]) { alert("/tmp/bar is defined") } else { alert("/tmp/bar is not defined") } mark-draytons-macbook:~ mark$ puppet test.pp alert: Scope(Class[main]): /tmp/foo is defined alert: Scope(Class[main]): /tmp/bar is not defined notice: //File[/tmp/foo]/ensure: created 

注意: defined() 依赖于parsing顺序 。

要么….

 unless File["${local_container}"] { file{ "${local_container}": ensure => directory, } } 

请注意那些引号和大括号。

只是,

 file{ "$local_container": ensure => directory, replace => false, }