Chef客户端运行期间何时设置默认节点属性? [英] When are default node attributes set during a chef client run?

查看:80
本文介绍了Chef客户端运行期间何时设置默认节点属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I edited sample code to clarify my issue. I think the answers make more sense now since my first recipe is indeed inside a ruby_block.

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

When are default node attributes available during a client run? Let me clarify. My example is simplified, but what I'm trying to do is more complex than this, but the concept is similar.

我引导了一个结点,结点为空运行清单。

I bootstrapped a node, which ran with an empty run list. So far so good.

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

Now I add my hello_world cookbook to my node's run list.

knife node run_list add node_name "recipe[hello_world]"

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

In my hello_world cookbook, I have the following default attribute defined in attributes/default.rb

default.hello_world.location = ''

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

I have a recipes/default.rb that does this

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

recipes / set_location.rb确实

recipes/set_location.rb does

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

和配方/show_location.rb使用日志资源

and recipes/show_location.rb does this, using the log resource

log "Hello #{node.hello_world.location}"

我希望能在厨师客户端运行中看到它吗?

Should I expect to see this in the chef-client run...

Hello New York!

...或者简单地说,因为食谱尚未完全运行,因此其属性尚未已与Chef服务器同步?

...or simply this, since the cookbook has not completely run yet and therefore its attributes have not been sync'ed with the Chef server?

Hello

我正在获取后者,也就是说,只是你好而没有纽约!

I am getting the latter, that is, just "Hello" without "New York!"

我该如何让我的菜谱登录 Hello New York!客户运行之后?

How can I get my cookbook to log "Hello New York!" after its client run?

也就是说,通常如何在一个食谱中设置节点属性,以便在厨师期间可以在同一本食谱的不同食谱中使用它们-客户端运行?

推荐答案

此处,并且此处

在更新日志之前的属性值时,您应该具有预期的结果资源已编译,因为node.set不在ruby_block中。

You should have the expected result as you're updating the attribute value before the log resource is compiled, because the node.set is not in a ruby_block.

我怀疑使用方法语法会带来问题,更喜欢使用哈希访问。

I suspect the use of method syntax to be the problem, prefer using the hash access.

即:

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

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

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

But as it's not real code (if I understood correctly) I can't swear it's the real case, without a real case I can't be sure why you get this result.

您可以在其他情况下使用惰性评估(属性已更新在ruby_block中)

You may use lazy evaluation in other context (attribute updated within a ruby_block)

这篇关于Chef客户端运行期间何时设置默认节点属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆