knife commands 1

Getting Knife Version

knife --version

Create Cookbook

knife cookbook create <cookbookName>

Getting List of all the client nodes

knife client list

Server Bootstrap

knife bootstrap <hostname/ipaddr> -x <username> -P <password> -N <nodeName>

Server Bootstrap with Runlist

knife bootsrap <hostname> -x root -P <password> -N module3 -r "receipe[apache]"

Server Bootstrap with Sudo with providing user pem file as ssh identity with , and and additional passing to the node

knife bootstrap <hostname> --sudo -x <user> -i <SSH Itentityfile> --node-name <nodename> --run-list <runlist> -E <Environment> --no-host-key-verify -j <json_attribute>

Add Receipe to RunList for Node

knife node run_list add module2 "receipe[apache]"

Ohai Command

ohai

Knife Node Show

knife node show <nodename>
know node show <nodename> -a <keyName>
knife node show module2 -a apache
knife node show module2

Knife Remove Item from run_list

knife node run_list remove module2 "receipe[apache]"

*** Kitchen Commands ***

kitchen list
kitchen create
kitchen login <InstanceName>

Knife Configure

Knife Configure command is used to create knife.rb and client.rb so that they can distribute to workstation and nodes.

Configure client.rb

knife configure client <directory>

Configure knife.rb

knife configure

 

What else

Knife: It provides an interface between a local chef repo and chef server
Local chef repo can be downloaded (Starter Kit)
Knife lets you manage:
– Roles and nodes
– Cookbooks and recipes
– Stores of JSON data (data bags), including encrypted data
– Environments
– Cloud Resources including provisioning
– installation of chef on management workstations
– Searching of indexed data on the chef server

Knife configuration File (http://docs.opscode.com/config_rb_knife.html)
– Default Location
* ~/.chef/knife.rb
– project specific configuration
* .chef/knife.rb of the current directory [project dir]

Knife Commands:

$> knife –version # Show the chef version installed

$> knife client list # Read the chef_server_url from knife.rb
# HTTP GET to #{chef_server_url}/clients and displays the result

$> knife bootstrap # Bootstrap or initialize a node instance
# Copies authentication data (validation.pem) and knife.rb to node instance from chef-repo and then
# Runs: bash -c ‘install chef configure client run chef’
# knife bootstrap uvo1tmqfn67ii0vhzy4.vm.cld.sr –sudo -x opscode -P opscode -N “target1”
# knife bootstrap <servername> –sudo -x opscode -P opscode -N “target1” -r “recipe[apt],recipe[apache]”
# By default chef uses FQDN as node name

$> knife cookbook create apache
# Create a new cookbook for e.g. Apache

$> knife cookbook upload apache
# Uploads apache cookbook to chef server

$> knife node list # Lists all the nodes available in your organization

$> knife node show <nodename>
# Returns brief description of node nodename

$> sudo ohai |less # Returns system (node) information in JSON format

$> knife node show <nodename> -l -Fj
# Returns long description of node nodename
# -Fj returns output in json format

$> knife node show <nodename> -a fqdn
# Reutn only single attribute value for eg FQDN of the node

$> knife search node “*:*” -a fqdn
# Returns FQDN for all node matchng the pattern (Solr query syntax)
# eg. knife search node “platform:ubuntu” -a fqdn

$> knife cookbook site download <cookbookname>
# Downloads cookbookname cookbook from opscode community site and stores in local chef repo

 

Chef Dry Run

chef-client -Fmin --why-run

List Facts

ohai

Bootstrap Chef client

knife bootstrap <FQDN/IP>

Change Chef Run List

knife node run_list <add|remove> <node> <cookbook>::<recipe>

Runlist Status

knife status --run-list
knife status "role:webserver" --run-list

Nodes and Roles

List Node Info

knife node show <node>

List Nodes per Role

knife search node 'roles:<role name>'

Load role from file

knife role from file <file> [<file> [...]]

Data Bags

Load data bag from file

knife data bag from file <data bag name> <file>

knife + SSH

knife ssh -a ipaddress name:server1 "chef-client"

you can also use patterns:

knife ssh -a ipaddress name:www* "uptime"

Debugging

Inheritance

Debugging Attribute Inheritance

# Invoke chef shell in attribute mode
chef-shell -z
chef > attributes
chef:attributes >

# Query attributes examples
chef:attributes > default["authorized_keys"]
[...]
chef:attributes > node["packages"]
[...]

Editing Files

using a Script resource.

bash "some_commands" do
    user "root"
    cwd "/tmp"
    code <<-EOT
       echo "alias rm='rm -i'" >> /root/.bashrc
    EOT
end

Misc

assyrian technical blog