Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How print or debug Chef attributes

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?

like image 854
Cherry Avatar asked Dec 12 '14 10:12

Cherry


People also ask

How do you print a variable in chef?

You can use node. debug_value to show a single attribute. This will print out the value for that attribute at each level.

How do you debug a chef recipe?

Use the breakpoint resource to add breakpoints to recipes. Run the chef-shell in chef-client mode, and then use those breakpoints to debug recipes. Breakpoints are ignored by the chef-client during an actual chef-client run.

What are chef attributes?

An attribute is a specific detail about a node. Attributes are used by the chef-client to understand: The current state of the node. What the state of the node was at the end of the previous chef-client run.


1 Answers

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.

like image 95
keen Avatar answered Sep 30 '22 18:09

keen