什么时候在 Chef 客户端 运行 期间设置默认节点属性?

When are default node attributes set during a chef client run?

我编辑了示例代码来澄清我的问题。我认为答案现在更有意义了,因为我的第一个食谱确实在 ruby_block.

客户端 运行 期间默认节点属性何时可用?让我澄清一下。我的例子很简单,但我想做的比这更复杂,但概念是相似的。

我引导了一个节点,运行 有一个空的 运行 列表。到目前为止一切顺利。

现在我将 hello_world 食谱添加到节点的 运行 列表中。

knife node run_list add node_name "recipe[hello_world]"

在我的 hello_world 食谱中,我在 attributes/default.rb

中定义了以下默认属性
default.hello_world.location = ''

我有一个 recipes/default.rb 可以做到这一点

include_recipe "hello_world::set_location"
include_recipe "hello_world::show_location"

recipes/set_location.rb 确实

ruby_block "Set location" do
  block do
    node.set.hello_world.location = "New York!"
  end
end

和 recipes/show_location.rb 使用日志资源

执行此操作
log "Hello #{node.hello_world.location}"

我应该在 chef-client 中看到这个 运行...

Hello New York!

...或者只是这个,因为食谱还没有完全 运行 因此它的属性还没有与 Chef 服务器同步?

Hello

我得到的是后者,也就是只有"Hello"没有"New York!"

如何让我的食谱在其客户端 运行 之后记录 "Hello New York!"?

也就是说,一般来说,如何在一个菜谱中设置节点属性,以便我可以在主厨客户端 运行 期间在同一食谱的不同菜谱中使用它们?

厨师的加载描述here and the attribute precedence is described here

在编译日志资源之前更新属性值时,您应该会得到预期的结果,因为 node.set 不在 ruby_block.

我怀疑使用方法语法是问题所在,更喜欢使用散列访问。

即:

node.set['hello_world']['location'] = "New York!"

log "Hello #{node['hello_world']['location']}"

但是因为它不是真实的代码(如果我理解正确的话)我不能发誓这是真实的案例,没有真实的案例我不能确定你为什么会得到这个结果。

您可以在其他上下文中使用 lazy evaluation(属性在 ruby_block 中更新)