简体   繁体   English

食谱如何根据是否已安装软件包来分配Chef属性?

[英]How can a cookbook assign a Chef attribute based on whether a package is installed?

I have a specific Chef attribute which I want to vary based on whether a package is installed. 我有一个特定的Chef属性,我想根据是否安装软件包来改变它。 For example: 例如:

if package X is installed
  default['attr1']['attr2'] = 'value1'
else
  default['attr1']['attr2'] = 'value2'
end

This attribute is then later used to set certain values in template files that are laid down. 然后,此属性随后可用于在放置的模板文件中设置某些值。

After looking online for the best way to do this, I am finding that it is definitely not common practice. 在网上寻找实现此目的的最佳方法之后,我发现这绝对不是常见的做法。 This makes me wonder if I am taking the wrong approach to my problem as well. 这使我想知道我是否对问题也采取了错误的方法。

So my full question here would be, what is the best way to determine whether a package is installed in a Chef attributes file. 因此,我的全部问题是,确定Chef属性文件中是否安装软件包的最佳方法是什么。 Is it not recommended that you vary an attribute based on the current config of the system? 不建议您根据系统的当前配置更改属性吗? If not, then why? 如果没有,那为什么呢?

Ohai already does this for you at the start of the run via node['packages'] . Ohai在运行开始时已经通过node['packages']为您完成了此操作。 In general Chef code should be describing the desired state of the system, not reacting to the current state. 通常,Chef代码应该描述系统的期望状态,而不会对当前状态做出反应。 Way down in the depths of Chef's own code it has to do a test-and-repair loop, but your code generally shouldn't need to do that. 深入Chef自己的代码中,它必须执行测试和修复循环,但是您的代码通常不需要这样做。 See https://coderanger.net/thinking/ for more details. 有关更多详细信息,请参见https://coderanger.net/thinking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM