Dec 21

Finding recently created hosts with Chef

By Bryan McLellan View Comments
Share and Enjoy:
  • Facebook
  • Twitter
  • Reddit
  • HackerNews
  • Slashdot

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
1324426395
$ date –date="-5 hours" +%s
1324408398
$ knife search node ‘ohai_time:[1324408398 TO 1324426395]‘ -i
2 items found

i-b25c8ad0

i-1e5c8a7c

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.

Share and Enjoy:
  • Facebook
  • Twitter
  • Reddit
  • HackerNews
  • Slashdot
  • http://profiles.google.com/john.m.alberts John Alberts

    As you pointed out, ohai_time tells you the last time Chef ran.  Usually when Chef is installed, either it is run as a service or a cron job so it runs at least once an hour, so I don’t see how this search would tell you anything except to give you a list of nodes that are checking in on time.

  • http://loftninjas.org Bryan McLellan

    You are correct, if all of you’re systems are running chef-client at intervals, it would not be a helpful trick for finding newly created instances.

    In my case, I have a separate organization for testing instances. These have a limited life and are used for automated or manual testing of Chef features. These instances do not run chef-client at intervals, so this particular technique works well there.

    For longer lived instances, you could look at uptime_seconds to see what systems have been running less than ‘days * 86400′.

blog comments powered by Disqus