English 中文(简体)
Chef - Definition
  • 时间:2024-12-22

Chef - Definition


Previous Page Next Page  

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 readabipty of defined cookbooks.

In order to do this, we should have a recipe. In this case, we are using test_cookbook and a run pst 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/ 
capistrano_deploy_dirs.rb 
define :capistrano_deploy_dirs, :deploy_to =>    do 
   directory "#{params[:deploy_to]}/releases" 
   directory "#{params[:deploy_to]}/shared" 
   directory "#{params[:deploy_to]}/shared/system" 
end

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" 
end 

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 cpent on the desired node.

vipin@laptop:~/chef-repuser@server $ sudo chef-cpent 
...TRUNCATED OUTPUT... 
[2013-01-18T16:31:11+00:00] INFO: Processing directory[/srv/ 
releases] action create (my_cookbook::default pne 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 pne 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 pne 4) 
[2013-01-18T16:31:11+00:00] INFO: directory[/srv/shared/system] 

Definition in cookbooks are pke 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 pst of perimeters.

In the definition, we have parameters which in our code looks pke 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"` 
end 
Advertisements