Chef – Useful knife commands


Bootstrap a chef-client system:
* This will login to and login as jtran and run “chef-client” command with sudo and apply the recipe[ntp]
* you will be prompted for a password otherwise pass flag -P <PASSWORD>
$ knife bootstrap -x jtran –sudo -r ‘recipe[ntp]’
Boostrap with a template file ( each OS needs their own bootstrap script )
$ knife bootstrap -x jtran –sudo -r ‘recipe[ntp]’ –template-file ~/chef-repo/bootstrap/precise.rb
Bootstrap with proxy
$ knife bootstrap –bootstrap-proxy http://proxyhost:3128 -x jtran –sudo  -r ‘recipe[ntp]’

Common Error:
Knife bootstrap: ERROR: TypeError: can’t convert false into String
This happens when knife can’t find the .chef directory in it’s current working directory, you should also be able to move to your ~/chef-repo directory to get around this error.


For help type:
knife ssh –help

-a = attribute that’s not FQDN ( example is ipaddress )
-x = username to login with
-P = password
-E = environment

This will ssh with the attribute: ipaddress ( default is to use hostname ) with user “root” and password “none” and execute for all system with role “webserver”
$ knife ssh -a ipaddress -x root -P none ‘role:webserver’ ‘uptime’

This is an example of a wildcard match for hosts in the chef-server database
$ knife ssh -a ipaddress -x root -P none ‘name:host1*’ ‘uptime’
‘name:host1*’  = query names that match host1*
‘uptime’ = command to run

The following would run chef client on all of your nodes
$ knife ssh ‘name:*’ ‘sudo chef-client’

The following would run chef client on all the nodes with the role “webapp”
$ knife ssh ‘role:webapp’ ‘sudo chef-client’

ssh to single node:
$ knife ssh ‘name:webserver01’ ‘sudo chef-client’

Run chef client on all nodes with role  “webapp” in environment “dev”
$ knife ssh -E dev ‘role:webapp’ ‘sudo chef-client’

Node Information

Get all the attributes of a node
$ knife node show nodename -a node

Get all the attributes of a certain attribute group
* example is you have attributes in your attributes/default.rb like default[:whynot][:zoidberg] = “woopwoop”
$ knife node show nodename -a whynot


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s