Finding recently created hosts with Chef

When you utilize cloud resources as a commodity, it can sometimes be hard to differentiate between individual systems. While testing some Chef installations on EC2 instances I asked myself, “which of these instances registered with my Chef server did I create a few hours ago?”

I considered searching through nodes for attributes that ought to be unique to these test instances, like the run_list. But, then I remembered that the time of the last Ohai run is stored as an Unix time in the automatic attributes of a Chef node run.

$ date +%s
$ date –date="-5 hours" +%s
$ knife search node ‘ohai_time:[1324408398 TO 1324426395]’ -i
2 items found



Above, I generated two time values using the date command, one for the current time and one for five hours ago. Then I used these values to search through my nodes for those who have had a Chef run in the last five hours using a range search with knife. This returned two nodes, which was exactly what I was looking for.

Another time-saving feature of Chef that you didn’t know you even wanted until you find yourself needing it.

Bryan McLellan