Chef - Definition

Definition can be defined as a logical method of grouping resources, which are used again and again. In this flow, we group the resources and give them a name to regain readability of defined cookbooks.

In order to do this, we should have a recipe. In this case, we are using test_cookbook and a run list of nodes, which includes the cookbook.

Creating a Definition

Step 1 − Create a new definition file in the cookbooks definition folder.

vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/definitions/ 
define :capistrano_deploy_dirs, :deploy_to => '' do 
   directory "#{params[:deploy_to]}/releases" 
   directory "#{params[:deploy_to]}/shared" 
   directory "#{params[:deploy_to]}/shared/system" 

Step 2 − Use a definition inside the cookbooks default recipe.

vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb 
capistrano_deploy_dirs do 
   deploy_to "/srv" 

Step 3 − Upload the cookbook to the chef server.

vipin@laptop:~/chef-repo $ knife cookbook upload test_cookbook 
Uploading test_cookbook [0.1.0] 

Step 4 − Run the Chef client on the desired node.

vipin@laptop:~/chef-repuser@server $ sudo chef-client 
[2013-01-18T16:31:11+00:00] INFO: Processing directory[/srv/ 
releases] action create (my_cookbook::default line 2) 
[2013-01-18T16:31:11+00:00] INFO: directory[/srv/releases] created 
directory /srv/releases 
[2013-01-18T16:31:11+00:00] INFO: Processing directory[/srv/ 
shared] action create (my_cookbook::default line 3) 
[2013-01-18T16:31:11+00:00] INFO: directory[/srv/shared] created 
directory /srv/shared 
[2013-01-18T16:31:11+00:00] INFO: Processing directory[/srv/ 
shared/system] action create (my_cookbook::default line 4) 
[2013-01-18T16:31:11+00:00] INFO: directory[/srv/shared/system] 

Definition in cookbooks are like micros, which group the resources and give them a name. A definition has a name by which one can tell them from which can be called inside the recipe and it has a list of perimeters.

In the definition, we have parameters which in our code looks like the following.

directory "#{params[:deploy_to]}/releases" 
directory "#{params[:deploy_to]}/shared" 
directory "#{params[:deploy_to]}/shared/system” 

It can be used inside the default recipe as follows.

capistrano_deploy_dirs do 
   deploy_to "/srv"` 
Kickstart Your Career

Get certified by completing the course

Get Started