jtimberman's Code Blog

Chef, Ops, Ruby, Linux/Unix. Opinions are mine, not my employer's (CHEF).

Display Chef Server Search Indexes

Anyone who runs a Chef Server or uses Hosted Chef has a wealth of search indexes available to them for data stored on the server. However, there isn’t a direct subcommand for displaying the search indexes. The following are automatically created on the server or Platform organization at creation and we know about them already:

  • node
  • role
  • client

Also, all data bags are created as a search index for the items they contain. All of these indexes are available through the API, and can be retrieved with Knife.

This uses knife’s “exec” sub-command. The subcommand takes a script argument, which is a Ruby script. It is executed in the context of the knife configuration, so if you have a .chef/knife.rb, it will be able to connect to a Chef Server. Let’s look at the contents of this script, it’s pretty simple.

The ‘p’ simply prints output of the argument. Then I’m calling the “get” method on the “api” object. This is a preconfigured object that knows how to talk to the Chef Server API through the aforementioned configuration. This takes an argument, which I pass the search API endpoint. The results that come back from the API are a Ruby hash, so I print out just the keys by calling the “keys” method. As we can see, I have a number of data bags in my environment, including DNS entries, users and Samba (shares and permissions for the curious).