2017-04-20 56 views
0

我想要执行一些类只有当特定的rpm版本不存在。根据条件在傀儡执行类

例如:

class base{ 
    if specified_rpm_absent { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

回答

2

你可以做的是定义一个custom fact返回true或false取决于这个RPM的存在或不存在,然后在条件逻辑使用它,即

事实上代码:

Facter.add(:specified_rpm_absent) do 
    setcode do 
    # Some Ruby code to return true or false depending on RPM package 
    # Facter::Core::Execution.exec() can be used to execute a shell 
    # command. 
    end 
end 

木偶4

class base { 
    if $facts['specified_rpm_absent'] { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

木偶3

class base { 
    if $::specified_rpm_absent { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

的OP辩称以下,最好是在这里使用的傀儡功能,并且功能也让争论。

问题是Functions execute on the Puppet master. They do not execute on the Puppet agent. Hence they only have access to the commands and data available on the Puppet master host

但是,如果使用Masterless Puppet,但不受Puppet支持,函数可用于此目的,此用例在Jussi Heinonen的“学习木偶”(2015)中描述。

我不会推荐这种方法的几个原因:

  • 它不是由木偶支持,因此也不能保证木偶的未来版本将不会使这个不可能的。
  • 该代码将不可移植。也就是说,代码不能在Puppet Forge上共享,并且不能迁移到传统的主/伪设置。
  • 这不是惯用的,会混淆认识木偶的人,即违反。

最后,应该指出的是,根据是否安装RPM来做出决定的设计可能存在更根本性的错误。为什么傀儡不知道RPM是否已安装?

+0

任何其他不需要自定义事实的解决方案? –

+0

如果您的要求是仅在特定rpm版本不存在的情况下执行类,那么通常使用自定义事实来实现。我想不出还有其他办法可以做到。 –

+0

谢谢。需要了解自定义事实! –