java 如何打印或调试厨师属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27441190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:48:49  来源:igfitidea点击:

How print or debug chef attributes?

javarubyattributeschef

提问by Cherry

I have created chef cookbook with attributes. Then I tried to boostrap a code to node and pass additional attributes in addidition and/or override the defaults. Is it possible to print an attribute tree to see what attributes are loaded, which are overrided?

我创建了带有属性的厨师食谱。然后我尝试将代码提升到节点并传递附加属性和/或覆盖默认值。是否可以打印属性树以查看加载了哪些属性,哪些属性被覆盖?

回答by keen

To get the entire attribute tree (from inside a converged chef, as opposed to via knife from chef server - which is useless in, say, a solo environment...) in a useful form:

以一种有用的形式获取整个属性树(从融合的厨师内部,而不是通过厨师服务器的刀 - 这在例如单人环境中是无用的......):

You're looking for node.to_hash- see http://www.rubydoc.info/gems/chef/Chef%2FNode%3Ato_hash

您正在寻找node.to_hash- 请参阅http://www.rubydoc.info/gems/chef/Chef%2FNode%3Ato_hash

A few other options in there too - http://www.rubydoc.info/gems/chef/Chef/Node

还有其他一些选项 - http://www.rubydoc.info/gems/chef/Chef/Node

to get a pretty printed log you can use chef's json libs pretty printer:

要获得漂亮的打印日志,您可以使用厨师的 json libs 漂亮打印机:

output="#{Chef::JSONCompat.to_json_pretty(node.to_hash)}"
log output

or write a file local to your client:

或写入本地文件到您的客户端:

output="#{Chef::JSONCompat.to_json_pretty(node.to_hash)}"
file '/tmp/node.json' do
  content output
end

Note that this is the converged node, so you won't get the default/override/etc levels you can get with node.debug_value- but if you dont actually know the name/path of the attribute, or you need to loop over a number of attributes, this could be your friend.

请注意,这是聚合节点,因此您不会获得可以使用的默认/覆盖/等级别node.debug_value- 但如果您实际上不知道属性的名称/路径,或者您需要遍历多个属性,这可能是你的朋友。

You'll get a huge result that looks like (highly trimed!)

你会得到一个巨大的结果,看起来像(高度修剪!)

{
  "chef_type": "node",
  "name": "node.example.com",
  "chef_environment": "_default",
  "build-essential": {
    "compile_time": false
  },
  "homebrew": {
    "owner": null,
    "auto-update": true,
  ...
  },
  "recipe": [
    "example"
  ],
  "run_list": [
    "recipe[example]"
  ]
}

Thanks to this answerfor the pretty printer pointer

感谢这个漂亮的打印机指针的答案

回答by Tejay Cardon

You can use node.debug_valueto show a single attribute. This will print out the value for that attribute at each level. However, doing this at each level for each attribute is harder (I'm not sure of a way to do it). Furthermore, because of the massive volume of attributes from ohai, I'm not sure you'd even want to do it.

您可以使用node.debug_value来显示单个属性。这将在每个级别打印出该属性的值。但是,在每个级别为每个属性执行此操作更难(我不确定有什么方法可以做到)。此外,由于 ohai 的大量属性,我不确定您是否愿意这样做。

If your chef run is completing correctly, you can do a knife node show -l <nodename>(that's a lower case L). That will show you the actual value, but it provides a huge volume of data, and doesn't tell you which values are default, normal, override, etc.

如果您的厨师运行正确完成,您可以做一个knife node show -l <nodename>(这是一个小写的 L)。这将向您显示实际值,但它提供了大量数据,并且不会告诉您哪些值是默认值、正常值、覆盖值等。

回答by wisbucky

Forking the answer by @keen, this produces a more human readable output in YAML format.

分叉@keen 的答案,这会以 YAML 格式生成更具人类可读性的输出。

output = node.to_yaml
file '/var/node.yaml' do
  content output
end

回答by Deepan Prabhu Babu

At times, it might be easy to read the variables off a node, after they are provisioned.

有时,在提供变量后,从节点读取变量可能很容易。

knife node edit <hostname-here> -a