Chef Blogs

Finding recently created hosts with Chef

Bryan McLellan | Posted on

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.

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

i-b25c8ad0

i-1e5c8a7c
[/sourcecode]

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.