Reputation: 1396
Over time we have developed multiple cookbooks and uploaded to our chef server. These cookbooks are used by multiple teams. Is there a way to check for a given cookbook which other cookbooks depend on it or which nodes use it in their run list? If it is not being used I should be able delete the cookbook from chef server. This will help us to clean up and not let new users use a deprecated cookbook.
Note: I want to run it against chef server (I even have Berkshelf api server) and not on local copies.
Thanks
Regards Amod
Upvotes: 1
Views: 2171
Reputation: 1
Install Knife Audit
sudo yum install gem -y
gem install knife-audit
Bash Script to delete unused cookbooks:-
deleteUnusedCookbook.sh
inside chef-repo directory. Now paste the following code in deleteUnusedCookbook.sh
#!/bin/bash
knife audit | tail -n +2 | awk '{ if($2 == 0) print $1;}'>unusedCookbook.txt
file=unusedCookbook.txt
for i in `cat $file`
do
knife cookbook delete "$i" -y
done
Here unusedCookbook.txt will contain the name of all unused cookbook on chef server. As a result In the script $2==0 will give the list of all unused cookbook. To get all used cookbook names change it to $2!=0 but please test it once without deleting otherwise it will delete all cookbooks which are currently in use.
chmod +x deleteUnusedCookbook.sh
./deleteUnusedCookbook.sh
Upvotes: 0
Reputation: 33
https://github.com/jbz/knife-audit
Install helper cookbook
"The helper cookbook (knife_audit) consists of a single recipe (default) with a single resource in it - a ruby_block which saves node.run_state.seen_recipes to the attribute node[:knife_audit][:seen_recipes]. This preserves the complete runlist information from seen_recipes, which chef-client does not save to the chef server after constructing it in the compile phase."
Ensure helper cookbook is run on all nodes to populate the attribute.
Only then will a knife-audit -a find everything inside nested dependencies.
Upvotes: 3