Chef Cookbooks Part-II

This is our fourth post on Chef Series, we are done with Simple Chef-workstation model, Chef objects and recipes, default chef cookbooks. If you have missed reading it, kindly check:
Start Automation with Chef?
Chef objects and Recipes?
Chef Cookbooks?

Chef Automation

As we have seen how to use default.rb recipe, but we cannot use default.rb recipe with different environments for all applications/codes unless its unique across our whole environment. Lets see how to create, use and run different recipes:

1.) Create/Go to application cookbook directory, and select recipe directory.

2.) Create a recipe file with .rb extension and write your specific code.

3.) Apply the recipe (we will see the run in localmode )

Demonstration: Lets assume we have to create a recipe for user creation for some application.

Go to the recipe directory inside cookbook directory.

cd /home/cookbooks/thinknyx/recipes/

Create a recipe file (say users.rb) inside recipe directory.

vi users.rb

Write a code for the user creation and run it.

vi users.rb
user ‘thinknyxuser’ do
action :create
end

chef-client –local-mode /home/cookbooks/thinknyx/recipes/users.rb
[2016-09-10T01:46:21-07:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.13.37
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2016-09-10T01:46:27-07:00] WARN: Node thinknyx.chef.com has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/cookbooks/thinknyx/recipes/users.rb
* user[thinknyxuser] action create
– create user thinknyxuser

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 05 seconds

id -a thinknyxuser
uid=503(thinknyxuser) gid=503(thinknyxuser) groups=503(thinknyxuser)

Likewise you can create multiple recipes and use them accordingly.

In our next post we will present chef-client utility and introduction to run-list in chef.

Leave a Comment

Your email address will not be published.