Finding Those Empty Things on Chef With Knife

Quick tips on how to find unused roles and empty run lists


So after a time with a team dealing with chef and knife you end having a pretty vast chef repository. Git helps but it can get a little messy, use the little loops below to search for empty roles run lists or roles with no nodes. They work on chef 0.9, chef 0.10 apparently has some fancy search plugin to do the same (and more) but I haven’t tested it yet.

Also, they were originally one liners but I changed it for the sake of readability.

    for file in roles/*
    do 
      role=$(basename $file)
      echo -n "Role ${role%.rb}: "
      knife search node "role:${role%.rb}" -i |grep -v '^$' |wc -l
    done

You may not have a directory with roles, so you may need to use that:

    for file in $(knife role list|egrep -v '\[|\]'|tr -d ' ,"')
    do 
      role=$(basename $file)
      echo -n "Role ${role%.rb}: "
      knife search node "role:${role%.rb}" -i |grep -v '^$' |wc -l; 
    done

To search for those pesky empty run_lists (rare, but it happens, automatic cloud instaces, etc…)

    for node in $(knife node list |egrep -v '\[|\]' |tr -d ' ",')
    do 
      echo "Node $node:"
      knife node show -r $node
      echo "-----------"
    done

Yes, in this last case you will still have to do some manual work and actually find the empty run lists. Search for empty lines, the only ones present on the file will be the empty run lists.

That’s it, cya!

 
comments powered by Disqus