20

I created a Chef cookbook with attributes, then tried to boostrap a code to node and pass additional attributes in addition and/or override the defaults.

Is it possible to print an attribute tree to see what attributes are loaded, and which are overridden?

Thaillie
  • 1,362
  • 3
  • 17
  • 31
Cherry
  • 31,309
  • 66
  • 224
  • 364
  • 7
    @Tensibai Question is fair, Cherry is attempting to troubleshoot a chef bootstrap. The links you give are elementary ones I would find it hard to find the answer were I a beginner in chef. This is not a new SO user, so if you don't like the question simply downvote it please. – Mark O'Connor Dec 13 '14 at 09:36
  • 4 questions on the same subject is quite a spam IMO – Tensibai Dec 13 '14 at 10:11
  • After reviewing there's only one question left now, my first comment has less interest, but the links around attributes precedence are still valid. So I'm in doubt about removing it. Will see Monday to rewrite that all @markoconnor ( unsure of the 'poke' as I'm on phone without completion) – Tensibai Dec 13 '14 at 13:41

4 Answers4

18

To get the entire attribute tree from inside a converged Chef, as opposed to via knife from Chef Server, which is useless in a solo environment, in a useful form look at node.to_hash. More information is in "Chef::Node".

To get a pretty printed log you can use Chef's JSON library's pretty printer:

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 don't actually know the name/path of the attribute, or you need to loop over a number of attributes, this could be your friend.

You'll get a huge result that looks like this highly trimmed example:

{
  "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]"
  ]
}

"How do you create pretty json in CHEF (ruby)" had the pretty printer pointer.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
keen
  • 816
  • 10
  • 11
8

You can use node.debug_value to 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.

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.

Tejay Cardon
  • 4,193
  • 2
  • 16
  • 31
  • 3
    To expand on @Tejay's answer with some example - For `node['minecraft']['properties']` use `node.debug_value('minecraft', 'properties')` See this for more detail - http://jtimberman.housepub.org/blog/2014/09/02/chef-node-dot-debug-value/ – keen Sep 14 '15 at 21:12
2

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

output = node.to_yaml
file '/var/node.yaml' do
  content output
end
wisbucky
  • 33,218
  • 10
  • 150
  • 101
1

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

knife node edit <hostname-here> -a
Deepan Prabhu Babu
  • 862
  • 11
  • 18