检查一个类是否存在

14

有没有办法在清单文件中检查特定类是否存在?

我想做这样的事情:

class foo {
    if exists( Class["foo::${lsbdistcodename}"] ) {
        include foo::${lsbdistcodename}
    }
}

所以我可以轻松地添加分发/版本特定的类,然后它们会自动包含。
1个回答

15
您应该使用defined语句而非exists语句。 以下代码片段可以正常工作:
class foo {
    if defined( "foo::${lsbdistcodename}") {
            notify {'defined':}             
            include "foo::${lsbdistcodename}"
    }
}

class foo::precise {
    notify{'precise':}
}

假设您正在运行Puppet版本> 2.6.0

1
好的,谢谢你,但这不会起作用,因为类还没有被定义。这就是我问题的全部所在。我想检查类是否存在并且可以包含它。你发布的代码实际上根本不会做任何事情。如果类未被包含,则条件为假,如果已经包含,则include将不会再次包含它... - Michael Krupp
不适用于我。我正在运行 Puppet 2.7.18:如果定义了 Class['common'] ,则会输出 'yes',但是if defined(Class['common']) { notice('yes') }将不会输出任何内容。而include common if defined(Class['common']) { notice('yes') }将输出 'yes'。 - Michael Krupp
12
好的,看起来 defined("class") 检查一个类是否存在,而 defined(Class["class"]) 则检查它是否被加载。 谢谢你的帮助! - Michael Krupp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接