[ Content | View menu ]

Viewing Chef Node Attributes with Knife

Mark Mzyk | February 5, 2013

This is a simple post to list all the ways you can view Chef node attributes with knife, even nested attributes, which is harder than it feels like it should be.

A lot of this information can be found at docs.opscode.com, but as of this writing the examples for knife node show don’t always go into enough detail.

knife raw was a part of the knife-essentials gem but as of Chef 11 has been integrated into standard knife. If you aren’t running Chef 11, you can install the knife-essentials gem.

 

Show basic info about the node, truncated and nicely formated:

knife node show <node_name>

Show all the information about a node, nicely formated:

knife node show -l <node_name>

 

Three ways to list node information, in the raw json form (note the -l or –long option is needed to return all node data for knife node show so that all node attribute data is included)

knife node show -l -F json <node_name>

knife node show -l --format=json <node_name>

knife raw /nodes/<node_name>

 

How to list a single node attribute:

knife node show <node_name> -a <attribute_name>

attribute_name will be something like kernel or platform.

 

However, this doesn’t work for nested attributes, such as node[kernel][machine], as knife node show doesn’t understand nested attributes.

 

If you don’t want to use the raw json and grep, you can find a nested attribute by doing a search:

knife search node <query_to_run> -a <main_attribute>.<nested_attribute>

An example:

knife search node name:<node_name> -a kernel.machine

 

Hopefully this is useful information to others. Information on using search to find nested attributes was originally found on this log of the #chef irc channel.

Filed in: Tools.

2 Comments

  1. Comment by Steven Danna:

    Nice post Mark!

    > However, this doesn’t work for nested attributes, such as node[kernel][machine], as knife node show doesn’t understand nested attributes.

    `knife show` does support nested attribute in the same way that knife search does:

    knife node show op-3033304d3237 -a kernel.machine
    op-3033304d3237:
    kernel.machine: x86_64

    Another nifty tool is `knife exec` which is often what I reach for when I need to manipulate some node data in an odd way. For example, if I wanted to show all the users on a node using zsh as their shell:

    knife exec -E ‘nodes.show(“op-3033304d3237″)[“etc”][“passwd”].each {|k,v| puts k if v[“shell”] =~ /zsh/}’

    February 9, 2013 @ 00:08
  2. Comment by Mark Mzyk:

    Thanks Steven. I’m not sure how I missed that knife node show could handle nested attributes. I thought it odd that it appeared not to, but I see I was mistaken.

    Thanks for the tip on knife exec. I knew it existed but need to learn more about it and see what other uses it has.

    February 9, 2013 @ 00:28