Installing Ruby gems and using them in recipes
Recipes are plain Ruby files. It is possible to use all of Ruby's language features inside your recipes. Most of the time, the built-in Ruby functionality is enough but sometimes you might want to use additional Ruby gems. Connecting to an external application via an API or accessing a MySQL database from within your recipe is an example of where you will need Ruby gems inside your recipes.
Chef lets you install Ruby gems from within a recipe, so that you can use them later.
Getting ready
Make sure you have a cookbook called my_cookbook
and that the run_list
of your node includes my_cookbook
, as described in Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.
How to do it...
Let's see how we can use the ipaddress
gem in our recipe:
Edit the default recipe of your cookbook, installing a gem to be used inside the recipe:
mma@laptop:~/chef-repo $ subl
cookbooks/my_cookbook/recipes/default.rb chef_gem 'ipaddress' require...