Photo credit: Daily Ampersand

Photo credit: Daily Ampersand

Knife is the command line interface between your local Chef repository and a Chef-controlled node or Chef server.  Use it instead of the Chef server GUI--it's much faster. 



Part 1: Bootstrapping a new instance

To set up a new EC2 instance running a REST server (let's call it PinkTango), run the following command:

knife ec2 server create --run-list recipe[recipe_name::deploy_pink_tango]

This command will automatically deploy the latest version of master (default). Upon completion the node will be registered with the chef server. This command assumes you have configured some of the knife-ec2 switches in your .knife configuration file located in your local chef repository.

Part 2: Passing json attributes when bootstrapping a new instance 

Sometimes when deploying a new node, you want to specify different attributes than the defaults set for the recipe/cookbook. You can pass node json attributes when bootstrapping a node at the command line.

To set up a new EC2 instance running PinkTango run the following command. 

knife ec2 server create --run-list recipe[recipe_name::deploy_pink_tango] -N name_to_give_ec2_and_chef_node -j '{ "key": "value"}'

Then, set the node attribute called recipe_name:pink_tango_domain to the specified value:

knife ec2 server create --run-list recipe[recipe_name::deploy_atomic_tangerine] -N AT01_test -j '{ "recipe_name": { "atomic_tangerine_domain": "other.json_attribute." } }'

Part 3: Performing a master branch code deploy

(These steps assume the instance exists and is registered with the chef server.)

Run knife ssh "name:node_name" "sudo chef-client"

The argument after the name refers to the chef node name. For additional query syntax, see the opscode docs.

Part 4: Performing a non-master branch code deploy using json attributes

(These steps assume the instance exists and is registered with the chef server.) 

There is a node attribute called recipe_name:pink_tango_deploy_branch that will allow you to specify a branch or commit number. 

  • Step 1: Edit/Add the recipe_name:pink_tango_deploy_branch value to the node's json attribute list:

knife node edit node_name (i.e. i-7dd0761f) 

Here is a sample. I'm going to deploy the service-locator branch:

{

"name": "i-7dd0761f",

"chef_environment": "_default",

"normal": {

"tags": [

],

"rvm": {

"install_pkgs": [

"sed",

"grep",

"tar",

"gzip",

"bzip2",

"bash",

"curl",

"git-core"

]

},

"recipe_name": {

"deploy_branch": "service-locator"

}

},

"run_list": [

"recipe[recipe_name::deploy_pink_tango]"

]

}
  • Step 2:  Run knife ssh "name:node_name" "sudo chef-client"

You can also edit node json attributes directly from Chef server web console.

 

Comment