7

我有节点的配方和属性文件。例如 localhost 和 linode。我试图让属性文件在默认或其他属性之前先加载(并设置主机名等)。例子:

属性/localhost.rb:

default[:hostname] = "localhost"
default[:nginx][:hostname] = 'mbdev-localhost'

include_attribute 'mbdev::common'

属性/common.rb

default[:nginx][:website1][:url] = "subdomain." + default[:nginx][:hostname]

食谱/localhost.rb

include_recipe 'mbdev::default'

运行列表:

'mbdev::localhost'

但是,似乎 include_attribute 使“通用”属性首先加载。所以 nginx-hostname 还没有设置...

我得到的顺序是:1)加载属性/default.rb 2)加载属性/common.rb 3)关于+的错误

如何让 localhost.rb 在 common.rb 之前加载?

4

2 回答 2

12

默认情况下,属性文件按字母顺序加载。这过去在所有地方都不是完全一致的,但在CHEF-2903中得到了修复。

因此,您attributes/common.rb的加载之前attributes/localhost.rb只是因为它按字母顺序较早。该规则的一个例外是attributes/default.rb它总是在食谱中的任何其他属性文件之前加载。

一般属性文件的加载顺序如下:

  1. 按字母顺序加载所有食谱依赖项的属性
  2. 加载attributes/default.rb(是否存在)
  3. 按文件名的字母顺序加载任何其他属性文件

您可以先加载属性文件,然后再使用include_attribute.

此逻辑在 Chef 中是硬编码的,无法更改。不过,您可以执行一些解决方法:

  • 您可以以加载顺序不再重要的方式编写属性文件
  • 您可以按照与上述逻辑一致的方式命名食谱/属性
  • 您可以强制再次加载属性文件:

    node.from_file(run_context.resolve_attribute("cookcook_name", "attribute_file"))
    
于 2013-10-28T11:45:16.267 回答
1

为什么不使用override_attribute?这就是它们存在的原因:-) 请参阅属性优先级

于 2013-10-28T08:04:09.413 回答