我一直在想办法测试一个资源是否已经在另一个文件中定义,如果没有创建它?一个简单的例子:
if File[$local_container] {
alert("Testing - It existed $local_container")
} else {
file{ "$local_container":
ensure => directory,
}
}
Run Code Online (Sandbox Code Playgroud)
但是 -File[$local_container]
似乎总是评估为真。有没有办法做到这一点?
mar*_*ton 15
您的意思是“测试资源是否已定义”?如果您定义了一个资源(即,file {}
等),Puppet 将创建您所描述的内容,如果尚不存在(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
Run Code Online (Sandbox Code Playgroud)
注:defined()
是依赖解析顺序。
小智 10
更好的方法是使用 puppetlabs stdlib 中的 ensure_resource 函数
它采用资源类型、标题和描述资源的属性列表作为参数。
假设您有测试用例,仅在资源不存在时才创建该资源-
ensure_resource('package', 'test-pkg', {'ensure' => 'present'})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29000 次 |
最近记录: |