Inspecting files on your Chef server with knife
Sometimes, you may want to peek into the files stored on your Chef server. You might not be sure about an implementation detail of that specific cookbook version, which is currently installed on your Chef server, and need to look it up. Knife can help you out by letting you show various aspects of the files stored on your Chef server.
Getting ready
Make sure that you have the iptables
cookbook installed locally and uploaded on your Chef server.
- Install the iptables community cookbook by executing the following command and code lines:
mma@laptop:~/chef-repo $ knife cookbook site install iptables
Installing iptables to /Users/mma/work/chef-repo/cookbooks ...TRUNCATED OUTPUT...
Tip
Take a look at the following error:
ERROR: IOError: Cannot open or read ../chef-repo/cookbooks/iptables/metadata.rb!
If you get the preceding error, your cookbook only has a
metadata.json
file. Make sure that you delete it and create a validmetadata.rb
, file instead. - Upload the
iptables
cookbook on your Chef server by executing the following given command and code lines:mma@laptop:~/chef-repo $ knife cookbook upload iptables
Uploading iptables [0.14.0] Uploaded 1 cookbook.
How to do it...
Let's find out how knife can help you to look into a cookbook stored in your Chef server:
- First, you want to find out the current version of the cookbook you're interested in. In our case, we're interested in the
iptables
cookbook:mma@laptop:~/work/chef_helpster $ knife cookbook show iptables iptables 0.14.0
- Then, you can look up the definitions of the
iptables
cookbook, using the version number that you found out in the previous step:mma@laptop:~/chef-repo $ knife cookbook show iptables 0.14.0 definitions
checksum: 45c0b77ff10d7177627694827ce47340 name: iptables_rule.rb path: definitions/iptables_rule.rb specificity: default url: https://s3.amazonaws.com/opscode-platform...
- Now, you can even show the contents of the
iptables_rule.rb
definition file, as stored on the server:mma@laptop:~/chef-repo $ knife cookbook show iptables 0.14.0 definitions iptables_rule.rb
# # Cookbook Name:: iptables # Definition:: iptables_rule # # define :iptables_rule, :enable => true, :source => nil, :variables => {} do ...TRUNCATED OUTPUT... end
How it works...
The knife cookbook show
subcommand helps you understand what exactly is stored on the Chef server. It lets you drill down into specific sections of your cookbooks and see the exact content of the files stored in your Chef server.
There's more...
Since Chef 11, you can pass patterns to the knife show
command to tell it what exactly you want to see. Showing the contents of the iptables_rule
definition can be done as follows, in addition to the way we described previously:
mma@laptop:~/work/chef_helpster $ knife show cookbooks/iptables/definitions/*
cookbooks/iptables/definitions/iptables_rule.rb: # # Cookbook Name:: iptables # Definition:: iptables_rule # # define :iptables_rule, :enable => true, :source => nil, :variables => {} do ...TRUNCATED OUTPUT... end
See also
- To find some more examples on knife show, visit http://docs.chef.io/knife_show.html